I am beginner with Deep learning CNN models, I am trying to classify the image boats categories below, which is a folder that contain these file names:
dataset_path = 'boat_type_classification_dataset'
# Load the dataset and split it into training (80%) and validation (20%)
train_dataset = tf.keras.preprocessing.image_dataset_from_directory(
dataset_path,
validation_split=0.2,
subset="training",
seed=43,
image_size=(128, 128),
batch_size=32,
shuffle=True
)
val_dataset = tf.keras.preprocessing.image_dataset_from_directory(
dataset_path,
validation_split=0.2,
subset="validation",
seed=43,
image_size=(128, 128),
batch_size=32,
shuffle=True
)
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 126, 126, 32) 896
max_pooling2d (MaxPooling2 (None, 63, 63, 32) 0
D)
conv2d_1 (Conv2D) (None, 61, 61, 32) 9248
max_pooling2d_1 (MaxPoolin (None, 30, 30, 32) 0
g2D)
global_average_pooling2d ( (None, 32) 0
GlobalAveragePooling2D)
dense (Dense) (None, 128) 4224
dense_1 (Dense) (None, 128) 16512
dense_2 (Dense) (None, 9) 1161
=================================================================
Total params: 32041 (125.16 KB)
Trainable params: 32041 (125.16 KB)
Non-trainable params: 0 (0.00 Byte)
I am getting this error while fitting the model
ValueError: Shapes (None, 9) and (None, 1) are incompatible
Note: I checked the labels of the categories which are integers
model.compile(
optimizer=tf.keras.optimizers.Adam(), # Adam optimizer
loss = 'sparse_categorical_crossentropy', # For integer labels
metrics=[
tf.keras.metrics.SparseCategoricalAccuracy(name='accuracy'), # Use Sparse for integer labels
tf.keras.metrics.Precision(name='precision'),
tf.keras.metrics.Recall(name='recall')
]
)
try:
history = model.fit(
train_dataset,
validation_data=val_dataset,
epochs=20
)
except Exception as e:
print("Error occurred:", e)