Here is the error provided after this cell:
Epoch 1/10
2023-11-07 10:50:42.834617: W tensorflow/core/framework/cpu_allocator_impl.cc:82] Allocation of 192675840 exceeds 10% of free system memory.
2023-11-07 10:50:43.002592: W tensorflow/core/framework/cpu_allocator_impl.cc:82] Allocation of 192675840 exceeds 10% of free system memory.
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
/workspaces/SI-GuidedProject-592631-1697551698/Project Development Phase/Model Build.ipynb Cell 11 line 1
----> 1 vgm.fit(x_train,epochs=10,validation_data=x_test)
File ~/.python/current/lib/python3.10/site-packages/keras/utils/traceback_utils.py:67, in filter_traceback.<locals>.error_handler(*args, **kwargs)
65 except Exception as e: # pylint: disable=broad-except
66 filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67 raise e.with_traceback(filtered_tb) from None
68 finally:
69 del filtered_tb
File ~/.python/current/lib/python3.10/site-packages/tensorflow/python/eager/execute.py:54, in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
52 try:
53 ctx.ensure_initialized()
---> 54 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
55 inputs, attrs, num_outputs)
56 except core._NotOkStatusException as e:
57 if name is not None:
InvalidArgumentError: Graph execution error:
Detected at node 'Equal' defined at (most recent call last):
File "/home/codespace/.python/current/lib/python3.10/runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
...
File "/home/codespace/.python/current/lib/python3.10/site-packages/keras/utils/metrics_utils.py", line 893, in sparse_categorical_matches
matches = tf.cast(tf.equal(y_true, y_pred), backend.floatx())
Node: 'Equal'
Incompatible shapes: [15,7,7] vs. [15]
[[{{node Equal}}]] [Op:__inference_train_function_2441]
from keras.preprocessing.image import ImageDataGenerator
from keras.applications.vgg19 import VGG19
from keras.layers import Dense, Flatten
train_datagen = ImageDataGenerator(rescale =1./255)
x_train = train_datagen.flow_from_directory(
'train data path',
target_size=(224,224),
class_mode = 'categorical',
batch_size = 15
)
vgm = VGG19(input_shape=(224, 224, 3),weights='imagenet',include_top=False)
for i in vgm.layers:
i.trainable = False
y=Flatten()(vgm.output)
op_layer = Dense(63,activation='softmax')(y)
vgm.summary()
Layer (type) Output Shape Param #
=================================================================
input_2 (InputLayer) [(None, 224, 224, 3)] 0
block1_conv1 (Conv2D) (None, 224, 224, 64) 1792
block1_conv2 (Conv2D) (None, 224, 224, 64) 36928
block1_pool (MaxPooling2D) (None, 112, 112, 64) 0
block2_conv1 (Conv2D) (None, 112, 112, 128) 73856
block2_conv2 (Conv2D) (None, 112, 112, 128) 147584
block2_pool (MaxPooling2D) (None, 56, 56, 128) 0
block3_conv1 (Conv2D) (None, 56, 56, 256) 295168
block3_conv2 (Conv2D) (None, 56, 56, 256) 590080
block3_conv3 (Conv2D) (None, 56, 56, 256) 590080
block3_conv4 (Conv2D) (None, 56, 56, 256) 590080
...
Total params: 20,024,384
Trainable params: 0
Non-trainable params: 20,024,384
vgm.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])
vgm.fit(x_train,epochs=1)
You haven't defined your Model
properly. Using the functional API, you need to define it something like this:
model = Model(inputs=input_layer, outputs=output_layer)
In your code, you haven't connected your backbone VGG
model to your other layers, such as op_layer
. When you make a forward pass with your model, the output is what you see in the traceback: TensorShape([15, 7, 7, 512])
, which is most likely not what you want if you're making a classification task.
I corrected everything for you and turned it into a reduced minimal example:
from keras.preprocessing.image import ImageDataGenerator
from keras.applications.vgg19 import VGG19
from keras.layers import Dense, Flatten
from keras import Model
train_datagen = ImageDataGenerator(rescale=1. / 255)
x_train = train_datagen.flow_from_directory(
'path/to/my/dataset',
target_size=(224, 224),
class_mode='categorical',
batch_size=15
)
number_of_classes = x_train.num_classes
vgm = VGG19(input_shape=(224, 224, 3), weights='imagenet', include_top=False)
for i in vgm.layers:
i.trainable = False
y = Flatten()(vgm.output)
op_layer = Dense(number_of_classes, activation='softmax')(y)
model = Model(inputs=vgm.inputs, outputs=op_layer)
model.compile(
optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy']
)
model.fit(x_train, epochs=1)