input_shape = (22, 80, 112, 3)
model = Sequential()
model.add(Conv3D(8, (3, 3, 3), activation='relu', input_shape=input_shape, kernel_regularizer=regularizers.l2(0.001)))
model.add(MaxPooling3D((2, 2, 2)))
model.add(Conv3D(32, (3, 3, 3), activation='relu', kernel_regularizer=regularizers.l2(0.001)))
model.add(MaxPooling3D((2, 2, 2)))
model.add(Conv3D(256, (3, 3, 3), activation='relu', kernel_regularizer=regularizers.l2(0.001)))
model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(13, activation='softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# Convert the target labels to one-hot encoding
y_train_onehot = tf.keras.utils.to_categorical(y_train)
y_test_onehot = tf.keras.utils.to_categorical(y_test)
model.summary()
The above code gives the following result:
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv3d (Conv3D) (None, 20, 78, 110, 8) 656
max_pooling3d (MaxPooling3 (None, 10, 39, 55, 8) 0
D)
conv3d_1 (Conv3D) (None, 8, 37, 53, 32) 6944
max_pooling3d_1 (MaxPoolin (None, 4, 18, 26, 32) 0
g3D)
conv3d_2 (Conv3D) (None, 2, 16, 24, 256) 221440
flatten (Flatten) (None, 196608) 0
dense (Dense) (None, 1024) 201327616
dropout (Dropout) (None, 1024) 0
dense_1 (Dense) (None, 256) 262400
dropout_1 (Dropout) (None, 256) 0
dense_2 (Dense) (None, 64) 16448
dropout_2 (Dropout) (None, 64) 0
dense_3 (Dense) (None, 13) 845
=================================================================
Total params: 201836349 (769.94 MB)
Trainable params: 201836349 (769.94 MB)
Non-trainable params: 0 (0.00 Byte)
BUT!!!
history = model.fit(X_train, y_train_onehot, epochs=20, batch_size=16, validation_data=(X_test, y_test_onehot))
gives the following error:
Epoch 1/20
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[12], line 2
1 # Train the model and record the history of training
----> 2 history = model.fit(X_train, y_train_onehot, epochs=20, batch_size=13, validation_data=(X_test, y_test_onehot))
File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\utils\traceback_utils.py:70, in filter_traceback.<locals>.error_handler(*args, **kwargs)
67 filtered_tb = _process_traceback_frames(e.__traceback__)
68 # To get the full stack trace, call:
69 # `tf.debugging.disable_traceback_filtering()`
---> 70 raise e.with_traceback(filtered_tb) from None
71 finally:
72 del filtered_tb
File ~\AppData\Local\Temp\__autograph_generated_fileguwe7_ri.py:15, in outer_factory.<locals>.inner_factory.<locals>.tf__train_function(iterator)
13 try:
14 do_return = True
---> 15 retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
16 except:
17 do_return = False
ValueError: in user code:
File "C:\Users\SAACHI\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\engine\training.py", line 1401, in train_function *
return step_function(self, iterator)
File "C:\Users\SAACHI\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\engine\training.py", line 1384, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\Users\SAACHI\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\engine\training.py", line 1373, in run_step **
outputs = model.train_step(data)
File "C:\Users\SAACHI\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\engine\training.py", line 1151, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "C:\Users\SAACHI\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\engine\training.py", line 1209, in compute_loss
return self.compiled_loss(
File "C:\Users\SAACHI\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\engine\compile_utils.py", line 277, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "C:\Users\SAACHI\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\losses.py", line 143, in __call__
losses = call_fn(y_true, y_pred)
File "C:\Users\SAACHI\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\losses.py", line 270, in call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "C:\Users\SAACHI\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\losses.py", line 2221, in categorical_crossentropy
return backend.categorical_crossentropy(
File "C:\Users\SAACHI\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\backend.py", line 5573, in categorical_crossentropy
target.shape.assert_is_compatible_with(output.shape)
ValueError: Shapes (None, 1) and (None, 13) are incompatible
What do I do to fix this?