Keras/Tenserflow - 无法使 model.fit() 工作
Keras/Tenserflow - Cannot make model.fit() work
我正在尝试制作一个 CNN 网络来对蘑菇图像进行预测。
遗憾的是,我什至无法开始训练我的模型,fit() 方法总是给我错误。
有 10 个 类,tf 数据集根据其子文件夹正确找到了它们的名称。
用我当前的代码,它说:
InvalidArgumentError: logits and labels must have the same first
dimension, got logits shape [12800,10] and labels shape [32]
模型摘要:
Layer (type) Output Shape Param #
=================================================================
input_5 (InputLayer) [(None, 64, 64, 3)] 0
conv2d_4 (Conv2D) (None, 62, 62, 32) 896
max_pooling2d_2 (MaxPooling (None, 20, 20, 32) 0
2D)
re_lu_2 (ReLU) (None, 20, 20, 32) 0
dense_2 (Dense) (None, 20, 20, 10) 330
=================================================================
这是我的代码:
#Data loading
train_set = keras.preprocessing.image_dataset_from_directory(
data_path,
labels="inferred",
label_mode="int",
batch_size=32,
image_size=(64, 64),
shuffle=True,
seed=1446,
validation_split = 0.2,
subset="training")
validation_set = keras.preprocessing.image_dataset_from_directory(
data_path,
labels="inferred",
label_mode="int",
batch_size=32,
image_size=(64, 64),
shuffle=True,
seed=1446,
validation_split = 0.2,
subset="validation")
#Constructing layers
input_layer = keras.Input(shape=(64, 64, 3))
x = layers.Conv2D(filters=32, kernel_size=(3, 3), activation="relu")(input_layer)
x = layers.MaxPooling2D(pool_size=(3, 3))(x)
x = keras.layers.ReLU()(x)
output = layers.Dense(10, activation="softmax")(x)
#Making and fitting the model
model = keras.Model(inputs=input_layer, outputs=output)
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=['accuracy'])
model.fit(train_set, epochs=5, validation_data=validation_set)
我认为你需要在传递到Dense
层之前展平
input_layer = keras.Input(shape=(64, 64, 3))
x = layers.Conv2D(filters=32, kernel_size=(3, 3), activation="relu")(input_layer)
x = layers.MaxPooling2D(pool_size=(3, 3))(x)
x = keras.layers.ReLU()(x)
x = keras.layers.Flatten()(x) # try adding this
output = layers.Dense(10, activation="softmax")(x)
您需要做的是在您的模型中的 ReLU 层和输出层之间添加一个展平层。
input_layer = keras.Input(shape=(64, 64, 3))
x = layers.Conv2D(filters=32, kernel_size=(3, 3), activation="relu")(input_layer)
x = layers.MaxPooling2D(pool_size=(3, 3))(x)
x = keras.layers.ReLU()(x)
x = keras.layers.Flatten()(x)
output = layers.Dense(10, activation="softmax")(x)
当您看到 model.fit 由于 logits 和标签的差异而抛出错误时,最好打印出模型摘要
print(model.summary())
查看摘要通常有助于找出问题所在。
我正在尝试制作一个 CNN 网络来对蘑菇图像进行预测。
遗憾的是,我什至无法开始训练我的模型,fit() 方法总是给我错误。
有 10 个 类,tf 数据集根据其子文件夹正确找到了它们的名称。
用我当前的代码,它说:
InvalidArgumentError: logits and labels must have the same first
dimension, got logits shape [12800,10] and labels shape [32]
模型摘要:
Layer (type) Output Shape Param #
=================================================================
input_5 (InputLayer) [(None, 64, 64, 3)] 0
conv2d_4 (Conv2D) (None, 62, 62, 32) 896
max_pooling2d_2 (MaxPooling (None, 20, 20, 32) 0
2D)
re_lu_2 (ReLU) (None, 20, 20, 32) 0
dense_2 (Dense) (None, 20, 20, 10) 330
=================================================================
这是我的代码:
#Data loading
train_set = keras.preprocessing.image_dataset_from_directory(
data_path,
labels="inferred",
label_mode="int",
batch_size=32,
image_size=(64, 64),
shuffle=True,
seed=1446,
validation_split = 0.2,
subset="training")
validation_set = keras.preprocessing.image_dataset_from_directory(
data_path,
labels="inferred",
label_mode="int",
batch_size=32,
image_size=(64, 64),
shuffle=True,
seed=1446,
validation_split = 0.2,
subset="validation")
#Constructing layers
input_layer = keras.Input(shape=(64, 64, 3))
x = layers.Conv2D(filters=32, kernel_size=(3, 3), activation="relu")(input_layer)
x = layers.MaxPooling2D(pool_size=(3, 3))(x)
x = keras.layers.ReLU()(x)
output = layers.Dense(10, activation="softmax")(x)
#Making and fitting the model
model = keras.Model(inputs=input_layer, outputs=output)
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=['accuracy'])
model.fit(train_set, epochs=5, validation_data=validation_set)
我认为你需要在传递到Dense
层之前展平
input_layer = keras.Input(shape=(64, 64, 3))
x = layers.Conv2D(filters=32, kernel_size=(3, 3), activation="relu")(input_layer)
x = layers.MaxPooling2D(pool_size=(3, 3))(x)
x = keras.layers.ReLU()(x)
x = keras.layers.Flatten()(x) # try adding this
output = layers.Dense(10, activation="softmax")(x)
您需要做的是在您的模型中的 ReLU 层和输出层之间添加一个展平层。
input_layer = keras.Input(shape=(64, 64, 3))
x = layers.Conv2D(filters=32, kernel_size=(3, 3), activation="relu")(input_layer)
x = layers.MaxPooling2D(pool_size=(3, 3))(x)
x = keras.layers.ReLU()(x)
x = keras.layers.Flatten()(x)
output = layers.Dense(10, activation="softmax")(x)
当您看到 model.fit 由于 logits 和标签的差异而抛出错误时,最好打印出模型摘要
print(model.summary())
查看摘要通常有助于找出问题所在。