'How do I add preprocessing layers to EfficientDet using TensorFlow Lite Model Maker?

I'm using Tensorflow Lite Model Maker and want to add preprocessing keras layers.

spec = model_spec.get('efficientdet_lite0')

object_detector_model = object_detector.create(
    train_loader,
    model_spec=spec,
    do_train=False,
)

model = object_detector_model.model

# -- add preprocessing layers to model -- #

model.build(...)
model.compile(...)
model.fit(...)

I tried doing this, but it wont work:

inputs = tf.keras.Input(shape=(None, None, 3), name="input")
x = get_preprocess_layers()(inputs)
outputs = model(x)
model = tf.keras.Model(inputs, outputs)

model.build(...)
model.compile(...)
model.fit(...)

The model compiles and in the first epoch, I get this error:

File "C:\anaconda3\envs\tf-gpu\lib\site-packages\keras\engine\training.py", line 1057, in train_function  *
    for _ in tf.range(self._steps_per_execution):

ValueError: 'outputs' must be defined before the loop.

Without preprocessing the summary looks like:

 Layer (type)                Output Shape              Param #
=================================================================
 keras_layer (KerasLayer)    multiple                  15092016

 class_net/class-predict (Se  multiple                 6066
 parableConv2D)

 box_net/box-predict (Separa  multiple                 10116
 bleConv2D)

=================================================================
Total params: 15,108,198
Trainable params: 14,952,694
Non-trainable params: 155,504

With preprocessing it looks like:

________________________________________________________________
 Layer (type)                Output Shape              Param #
=================================================================
 input (InputLayer)          [(None, None, None, 3)]   0

 preprocess (Sequential)     (None, 640, 640, 3)       0

  (EfficientDetNetTrainHub)  ([(None, 80, 80, 18),     15108198
                              (None, 40, 40, 18),
                              (None, 20, 20, 18),
                              (None, 10, 10, 18),
                              (None, 5, 5, 18)],
                              [(None, 80, 80, 36),
                              (None, 40, 40, 36),
                              (None, 20, 20, 36),
                              (None, 10, 10, 36),
                              (None, 5, 5, 36)])

=================================================================
Total params: 15,108,198
Trainable params: 14,952,694
Non-trainable params: 155,504
_________________________________________________________________

Any ideas? It looks like Tensorflow Lite Model Maker is creating a model that is not allowing me to insert some preprocessing layers.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source