【发布时间】:2021-07-03 09:18:47
【问题描述】:
我正在尝试对我在谷歌 Colab 上设计和训练的 ResNet 模型进行推理,可以找到笔记本的链接here。模型训练的图像维度为 (32, 32, 3)。训练后,我将模型保存为SavedModel 格式,以便我可以在我的机器上运行推理。我使用的代码是
import tensorflow as tf
import cv2 as cv
from resize import resize_to_fit
image = cv.imread('extracted_letter_images/001.png')
image_resized = resize_to_fit(image, 32, 32)
model = tf.keras.models.load_model('Model/CAPTCHA-Model')
model.predict(image_resized)
resize_to_fit 方法将图像大小调整为 32x32px。返回图像的形状也是 (32, 32, 3)。调用model.predict()函数时,显示如下错误信息
ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: (32, 32, 3)
我已尝试多次卸载并重新安装Tensorflow 和tf-nightly 均无济于事。我什至尝试过用这个扩展图像的尺寸
image_resized = np.expand_dims(image_resized, axis=0)
这导致图像具有尺寸 (1, 32, 32, 3)。进行上述更改后,将显示以下错误消息
2021-04-07 19:49:11.821261: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:180] None of the MLIR Optimization Passes are enabled (registered 2)
我感到困惑的是,调整大小的图像的尺寸和用于训练模型的图像的尺寸是相同的,但 model.predict() 似乎不起作用。
【问题讨论】:
-
'2021-04-07 19:49:11.821261: I ...' 不是错误,它只是信息性消息。您扩展维度起到了作用,因为 tensorflow predict 需要向量输入。
标签: python-3.x numpy tensorflow opencv deep-learning