【发布时间】:2021-09-14 08:14:42
【问题描述】:
我正在尝试使用来自预训练模型 MobileNet V2 的基本模型构建图像分类器。 这是模型的代码:
img_size = (160, 160)
img_shape = img_size + (3,)
print(img_shape)
base_model = tf.keras.applications.MobileNetV2(input_shape=img_shape,
include_top=False,
weights='imagenet')
base_model.trainable = False
global_average_layer = tf.keras.layers.GlobalAveragePooling2D()
prediction_layer = tf.keras.layers.Dense(2, activation="softmax")
inputs = tf.keras.Input(shape=img_size + (3,))
print("shape",inputs.shape)
x = preprocess_input(inputs)
x = base_model(x, training=False)
x = global_average_layer(x)
x = tf.keras.layers.Dropout(0.2)(x)
outputs = prediction_layer(x)
model = tf.keras.Model(inputs, outputs)
base_learning_rate = 0.0001
model.compile(optimizer=tf.keras.optimizers.Adam(lr=base_learning_rate),
loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True))
但是当我尝试向它提供数据时,它会输出以下错误:
ValueError: Input 0 is incompatible with layer model_2: expected shape=(None, 160, 160, 3), found shape=(32, 160, 3)
我不明白为什么会出现这个错误,我重新调整了所有图像的形状,如果我打印它们的形状,输出是:
(160, 160, 3)
模型预期的形状相同。我错过了什么?
谢谢!
【问题讨论】:
-
模型需要批量维度。如果您使用 tf.data,则需要批量处理数据
标签: python tensorflow keras