I am trying to make a CNN network to make predictions on images of mushrooms.
Sadly, I can't even begin to train my model, the fit() method always gives me errors.
There are 10 classes, the tf Datasets correctly found their names based on their subfolders.
With my current code, it says:
InvalidArgumentError: logits and labels must have the same first
dimension, got logits shape [12800,10] and labels shape [32]
Here's my code:
#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)
CodePudding user response:
I think you need to flatten before passing to the Dense
layer
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)
CodePudding user response:
what you need to do is to add a flatten layer in your model between the ReLU layer and the output layer.
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)
When you see model.fit throw an error due to the difference in logits and labels it is a good idea to print out the model summary
print(model.summary())
By looking at the summary it usually helps figure out what is wrong.