【发布时间】:2020-04-27 13:06:52
【问题描述】:
我正在为图像分类模型使用以下配置:
model = keras.Sequential([
keras.layers.Flatten(input_shape=(100, 100, 3)),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(10)
])
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
如果我打印 model.inputs 则返回
[<tf.Tensor 'flatten_input:0' shape=(None, 100, 100, 3) dtype=float32>]
如果我将张量图像传递给此模型,则它不起作用。所以我的问题是我应该对我的模型做些什么改变,以便它接受张量图像
我正在使用以下代码传递图像:
image = np.asarray(image)
# The input needs to be a tensor, convert it using `tf.convert_to_tensor`.
input_tensor = tf.convert_to_tensor(image)
# The model expects a batch of images, so add an axis with `tf.newaxis`.
input_tensor = input_tensor[tf.newaxis,...]
# Run inference
output_dict = model(input_tensor)
如果我通过张量图像,我会得到以下错误
WARNING:tensorflow:Model was constructed with shape (None, 100, 100, 3) for input Tensor("flatten_input:0", shape=(None, 100, 100, 3), dtype=float32), but it was called on an input with incompatible shape (1, 886, 685, 3).
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
我只是想知道我应该将哪些 Keras 层和输入参数更新到模型,以便它可以接受张量图像作为输入。 任何帮助,将不胜感激。谢谢!
【问题讨论】:
-
该消息是警告,而不是错误,并且清楚地表明图像应该是(100、100、3),但您的图像是(886、865、3)。您需要将图像大小调整为 100x100
-
啊!谢谢!我会更新并再次检查。
标签: python tensorflow keras tensorflow2.0 keras-layer