pythontensorflowdeep-learningimage-resizing

While predicting on trained model I've getting an Image shape error


I use the deeptrack library (that also uses tensorflow) to train a model dealing with cell counting using UNet.

This is the code defines the UNet model using deeptrack (dt) library:

model = dt.models.unet(
    (256, 256, 1), 
    conv_layers_dimensions=[8, 16, 32],
    base_conv_layers_dimensions=[32, 32], 
    loss=dt.losses.weighted_crossentropy((10, 1)),
    output_activation="sigmoid"
)

And this is the summary of the model I trained:

Model: "model_2"

 Layer (type)                   Output Shape         Param #     Connected to                     
==================================================================================================
 input_3 (InputLayer)           [(None, 256, 256, 1  0           []                               
                                )]                                                                
                                                                                                  
 conv2d_22 (Conv2D)             (None, 256, 256, 8)  80          ['input_3[0][0]']                
                                                                                                  
 activation_20 (Activation)     (None, 256, 256, 8)  0           ['conv2d_22[0][0]']              
                                                                                                  
 max_pooling2d_6 (MaxPooling2D)  (None, 128, 128, 8)  0          ['activation_20[0][0]']          
                                                                                                  
 ... 
 # (not relevant for the question) 
 ...                                                         
                                                                                                  
 conv2d_32 (Conv2D)             (None, 256, 256, 1)  145         ['activation_29[0][0]']          
                                                                                                  
==================================================================================================
Total params: 58,977
Trainable params: 58,977
Non-trainable params: 0

And when I try to make a prediction with the model I trained, with a 256X256 image (both color and grayscale) - I encounter the following error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-78c33765d4d3> in <module>()
    138     model = tf.keras.models.load_model('model7.h5', compile=False)
--> 139     prediction = model.predict([img])
    140 
    141     plt.figure(figsize=(15, 5))

1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
   1145           except Exception as e:  # pylint:disable=broad-except
   1146             if hasattr(e, "ag_error_metadata"):
-> 1147               raise e.ag_error_metadata.to_exception(e)
   1148             else:
   1149               raise

ValueError: in user code:

    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1801, in predict_function  *
        return step_function(self, iterator)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 264, in assert_input_compatibility
        raise ValueError(f'Input {input_index} of layer "{layer_name}" is '

    ValueError: Input 0 of layer "model" is incompatible with the layer: expected shape=(None, 256, 256, 1), found shape=(32, 256, 3)

I couldn't understand why the error message shows image dimensions of 32X256 when in practice it is 256X256?

How can I overcome the above problem?


Solution

  • You need to add a batch dimension to your image, try:

    prediction = model.predict(img[None, ...])