python - 在训练模型上进行预测时,我遇到了图像形状错误

标签 python tensorflow deep-learning image-resizing

我使用 deeptrack 库(也使用 tensorflow )来训练使用 UNet 处理细胞计数的模型。

这是使用 deeptrack (dt) 库定义 UNet 模型的代码:

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"
)

这是我训练的模型的摘要:

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

当我尝试使用我训练的模型和 256X256 图像(彩色和灰度)进行预测时,我遇到以下错误:

---------------------------------------------------------------------------
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)

我不明白为什么错误消息显示图像尺寸为 32X256,而实际上它是 256X256?

如何克服上述问题?

最佳答案

您需要向图像添加批量尺寸,请尝试:

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

关于python - 在训练模型上进行预测时,我遇到了图像形状错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73316883/

相关文章:

python - 无法以非 root 用户身份导入 pip 安装的包

python - 如何使用 pytest 在 Django 中测试重定向?

python - 自定义 Sklearn Transformer 单独工作,在管道中使用时抛出错误

TensorFlow: `tf.data.Dataset.from_generator()` 不适用于 Python 3.x 上的字符串

python - tensorflow 中边界框的坐标

python - 尝试重命名 tf.keras 上的预训练模型时出错

python - Selenium Python 脚本中的错误

python - '库未加载 : @rpath/libcudart. 7.5.dylib' Mac 上的 TensorFlow 错误

keras - 如何使用 VGG-16 中的预训练特征作为 Keras 中 GlobalAveragePooling2D() 层的输入

machine-learning - 使用模型的概率作为另一个模型的特征