How to solve this InvalidArgumentError: Graph execution error:

import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.utils import to_categorical

Load class names

classfile = ‘/content/drive/Shareddrives/Machine_Learning/bird_dataset/CUB_200_2011/birdspecies.names’
with open(classfile, ‘r’) as f:
class_names = f.read().splitlines()

#num_classes = len(class_names) # Update the number of classes

Define the paths to the training and validation datasets

train_data_dir = ‘/content/drive/Shareddrives/Machine_Learning/bird_dataset/CUB_200_2011’
validation_data_dir = ‘/content/drive/Shareddrives/Machine_Learning/bird_dataset/CUB_200_2011/images’

num_classes = len(os.listdir(train_data_dir))

Define the image dimensions and batch size

img_width, img_height = 224, 224
batch_size = 32

Create an instance of the ImageDataGenerator class for data augmentation

train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True
)

Load the training dataset

train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode=‘categorical’
)

Create an instance of the ImageDataGenerator class for validation data

validation_datagen = ImageDataGenerator(rescale=1./255)

Load the validation dataset

validation_generator = validation_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode=‘categorical’
)

Define the CNN model architecture

model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(32, (3, 3), activation=‘relu’, input_shape=(img_width, img_height, 3)),
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
tf.keras.layers.Conv2D(64, (3, 3), activation=‘relu’),
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
tf.keras.layers.Conv2D(128, (3, 3), activation=‘relu’),
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation=‘relu’),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(num_classes, activation=‘softmax’) # Use num_classes instead of hardcoding the number of classes
])

Compile the model

model.compile(optimizer=‘adam’, loss=‘categorical_crossentropy’, metrics=[‘accuracy’])

Train the model

model.fit(
train_generator,
steps_per_epoch=train_generator.samples // batch_size,
validation_data=validation_generator,
validation_steps=validation_generator.samples // batch_size,
epochs=10
)

Hi @Rinka_Wai, Could you please provide the full error log you got. Also ImageDataGenerator was deprecated prefer loading images with tf.keras.utils.image_dataset_from_directory. Thank You.

Below is the error log

InvalidArgumentError Traceback (most recent call last)
in <cell line: 67>()
65
66 # Train the model
—> 67 model.fit(
68 train_generator,
69 steps_per_epoch=train_generator.samples // batch_size,

1 frames
/usr/local/lib/python3.10/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
51 try:
52 ctx.ensure_initialized()
—> 53 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
54 inputs, attrs, num_outputs)
55 except core._NotOkStatusException as e:

InvalidArgumentError: Graph execution error:

Detected at node ‘categorical_crossentropy/softmax_cross_entropy_with_logits’ defined at (most recent call last):
File “/usr/lib/python3.10/runpy.py”, line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File “/usr/lib/python3.10/runpy.py”, line 86, in _run_code
exec(code, run_globals)
File “/usr/local/lib/python3.10/dist-packages/colab_kernel_launcher.py”, line 37, in
ColabKernelApp.launch_instance()
File “/usr/local/lib/python3.10/dist-packages/traitlets/config/application.py”, line 992, in launch_instance
app.start()
File “/usr/local/lib/python3.10/dist-packages/ipykernel/kernelapp.py”, line 619, in start
self.io_loop.start()
File “/usr/local/lib/python3.10/dist-packages/tornado/platform/asyncio.py”, line 195, in start
self.asyncio_loop.run_forever()
File “/usr/lib/python3.10/asyncio/base_events.py”, line 603, in run_forever
self._run_once()
File “/usr/lib/python3.10/asyncio/base_events.py”, line 1909, in _run_once
handle._run()
File “/usr/lib/python3.10/asyncio/events.py”, line 80, in _run
self._context.run(self._callback, *self._args)
File “/usr/local/lib/python3.10/dist-packages/tornado/ioloop.py”, line 685, in
lambda f: self._run_callback(functools.partial(callback, future))
File “/usr/local/lib/python3.10/dist-packages/tornado/ioloop.py”, line 738, in _run_callback
ret = callback()
File “/usr/local/lib/python3.10/dist-packages/tornado/gen.py”, line 825, in inner
self.ctx_run(self.run)
File “/usr/local/lib/python3.10/dist-packages/tornado/gen.py”, line 786, in run
yielded = self.gen.send(value)
File “/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py”, line 361, in process_one
yield gen.maybe_future(dispatch(*args))
File “/usr/local/lib/python3.10/dist-packages/tornado/gen.py”, line 234, in wrapper
yielded = ctx_run(next, result)
File “/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py”, line 261, in dispatch_shell
yield gen.maybe_future(handler(stream, idents, msg))
File “/usr/local/lib/python3.10/dist-packages/tornado/gen.py”, line 234, in wrapper
yielded = ctx_run(next, result)
File “/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py”, line 539, in execute_request
self.do_execute(
File “/usr/local/lib/python3.10/dist-packages/tornado/gen.py”, line 234, in wrapper
yielded = ctx_run(next, result)
File “/usr/local/lib/python3.10/dist-packages/ipykernel/ipkernel.py”, line 302, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File “/usr/local/lib/python3.10/dist-packages/ipykernel/zmqshell.py”, line 539, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File “/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py”, line 2975, in run_cell
result = self._run_cell(
File “/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py”, line 3030, in _run_cell
return runner(coro)
File “/usr/local/lib/python3.10/dist-packages/IPython/core/async_helpers.py”, line 78, in pseudo_sync_runner
coro.send(None)
File “/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py”, line 3257, in run_cell_async
has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
File “/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py”, line 3473, in run_ast_nodes
if (await self.run_code(code, result, async
=asy)):
File “/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py”, line 3553, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File “”, line 67, in <cell line: 67>
model.fit(
File “/usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py”, line 65, in error_handler
return fn(*args, **kwargs)
File “/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py”, line 1742, in fit
tmp_logs = self.train_function(iterator)
File “/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py”, line 1338, in train_function
return step_function(self, iterator)
File “/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py”, line 1322, in step_function
outputs = model.distribute_strategy.run(run_step, args=(data,))
File “/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py”, line 1303, in run_step
outputs = model.train_step(data)
File “/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py”, line 1081, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File “/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py”, line 1139, in compute_loss
return self.compiled_loss(
File “/usr/local/lib/python3.10/dist-packages/keras/src/engine/compile_utils.py”, line 265, in call
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File “/usr/local/lib/python3.10/dist-packages/keras/src/losses.py”, line 142, in call
losses = call_fn(y_true, y_pred)
File “/usr/local/lib/python3.10/dist-packages/keras/src/losses.py”, line 268, in call
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File “/usr/local/lib/python3.10/dist-packages/keras/src/losses.py”, line 2122, in categorical_crossentropy
return backend.categorical_crossentropy(
File “/usr/local/lib/python3.10/dist-packages/keras/src/backend.py”, line 5566, in categorical_crossentropy
return tf.nn.softmax_cross_entropy_with_logits(
Node: ‘categorical_crossentropy/softmax_cross_entropy_with_logits’
logits and labels must be broadcastable: logits_size=[32,15] labels_size=[32,5]
[[{{node categorical_crossentropy/softmax_cross_entropy_with_logits}}]] [Op:__inference_train_function_6636]

Hi @Rinka_Wai, For these errors I can see that there is mismatch between logits (the raw outputs of a model before they are transformed into probabilities) and labels (actual output values for each input sequence).

The loss is calculated on the model outputs and the ground truth passed, based upon the difference between the predicted and actual values the weights are updated in back propagation.

During this calculation the model gives 15 results but in the ground truth there are only 5 values, due to this you are facing the error. Make sure that the model’s raw outputs and labels have the same shape.

Thank You.

1 Like

class stop(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
if(logs.get(‘accuracy’)>0.96):
print(“\nAccuracy has reached 0.96”)
self.model.stop_training=True
callbacks=stop()

train models with model.fit

train_models = model.fit(
train_generator,
batch_size=15,
epochs=10,
validation_data=validation_generator,
validation_steps=5,
callbacks=[callbacks])
File “/usr/local/lib/python3.10/dist-packages/tornado/gen.py”, line 234, in wrapper

File “/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py”, line 261, in dispatch_shell

File “/usr/local/lib/python3.10/dist-packages/tornado/gen.py”, line 234, in wrapper

File “/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py”, line 539, in execute_request

File “/usr/local/lib/python3.10/dist-packages/tornado/gen.py”, line 234, in wrapper

File “/usr/local/lib/python3.10/dist-packages/ipykernel/ipkernel.py”, line 302, in do_execute

File “/usr/local/lib/python3.10/dist-packages/ipykernel/zmqshell.py”, line 539, in run_cell

File “/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py”, line 2975, in run_cell

File “/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py”, line 3030, in _run_cell

File “/usr/local/lib/python3.10/dist-packages/IPython/core/async_helpers.py”, line 78, in _pseudo_sync_runner

File “/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py”, line 3257, in run_cell_async

File “/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py”, line 3473, in run_ast_nodes

File “/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py”, line 3553, in run_code

File “”, line 8, in <cell line: 8>

File “/usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py”, line 65, in error_handler

File “/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py”, line 1807, in fit

File “/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py”, line 1401, in train_function

File “/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py”, line 1384, in step_function

File “/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py”, line 1373, in run_step

File “/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py”, line 1151, in train_step

File “/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py”, line 1209, in compute_loss

File “/usr/local/lib/python3.10/dist-packages/keras/src/engine/compile_utils.py”, line 277, in call

File “/usr/local/lib/python3.10/dist-packages/keras/src/losses.py”, line 143, in call

File “/usr/local/lib/python3.10/dist-packages/keras/src/losses.py”, line 270, in call

File “/usr/local/lib/python3.10/dist-packages/keras/src/losses.py”, line 2221, in categorical_crossentropy

File “/usr/local/lib/python3.10/dist-packages/keras/src/backend.py”, line 5579, in categorical_crossentropy
logits and labels must be broadcastable: logits_size=[38,4] labels_size=[38,3]
[[{{node categorical_crossentropy/softmax_cross_entropy_with_logits}}]] [Op:__inference_train_function_1986]