为什么我得到输入形状错误后,我指定的输入形状?



我正在尝试训练一个CNN在蟒蛇,木星笔记本。TensorFlow的版本是1.14。我正在试验mobilenet_v2。下面是我的代码:

from tensorflow.keras.models import Model
base_model=tf.keras.applications.mobilenet_v2.MobileNetV2(include_top=False,weights=None,input_shape=(150,150,3))
x = base_model.output
x = tf.keras.layers.GlobalAveragePooling2D()(x)
x = tf.keras.layers.Dense(1024, activation='relu')(x)
predictions = tf.keras.layers.Dense(9,activation='softmax')(x)
model = Model(inputs = base_model.input,outputs=predictions)
model.compile(loss='categorical_crossentropy',metrics=["accuracy"],optimizer = tf.keras.optimizers.Adam())
history = model.fit(train_data,
epochs=5,steps_per_epoch=len(train_data),
validation_steps=0.2*len(train_data))

图片的输入形状为150x150x3,我再次检查了输入的图片大小,确保正确。imagedata中随机图像的图像大小

在我拟合模型后,我得到一个错误说(检查输入时出现错误:期望input_8具有形状(150,150,3),但得到形状(256,256,3)的数组))错误信息截图

这是模型摘要的截图;输入层有正确的形状,所以我不确定256来自哪里。模型的前几层

ps:我也试图建立一个自定义模型只有几个层,但同样的错误仍然发生:

custom_model = tf.keras.Sequential([
tf.keras.layers.Conv2D(10,10,activation='relu',input_shape=(150,150,3)),
tf.keras.layers.MaxPool2D(),
tf.keras.layers.Conv2D(10,10,activation='relu'),
tf.keras.layers.MaxPool2D(),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(9,activation='softmax')
])
custom_model.summary()
custom_model.compile(loss="categorical_crossentropy",
optimizer=tf.keras.optimizers.Adam(),
metrics=["accuracy"])
custom_model_history = custom_model.fit(train_data,
epochs=5,
steps_per_epoch=len(train_data),
validation_data=val_data,
validation_steps=len(val_data))

这是自定义模型的总结下面是错误信息:来自自定义模型的错误信息

请看看你的火车,测试分裂(看看这个例子)

num_classes = 10
input_shape = (28, 28, 1)

加载数据并将其分割为训练集和测试集

(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()

将图像缩放到[0,1]范围

x_train = x_train.astype("float32") / 255
x_test = x_test.astype("float32") / 255
# Make sure images have shape (28, 28, 1)
x_train = np.expand_dims(x_train, -1)
x_test = np.expand_dims(x_test, -1)
print("x_train shape:", x_train.shape)
print(x_train.shape[0], "train samples")
print(x_test.shape[0], "test samples")

将类向量转换为二进制类矩阵

y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

在训练时,你必须在训练数据中同时提到x和y

我想你没有提到x_train和y_train,而不是你只提到x_train

batch_size = 128
epochs = 15
custom_model_history = custom_model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.1)

相关内容

  • 没有找到相关文章

最新更新