Solving InvalidArgumentError: Graph execution error:

import tensorflow as tf
import pandas as pd
from google.colab import drive
drive.mount('/content/drive')

import os
import numpy as np
import cv2
import matplotlib.pyplot as plt
from random import randint
from sklearn.model_selection import train_test_split
import keras

from tensorflow.keras.losses import CategoricalCrossentropy
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.image import resize

image_path=("/content/drive/MyDrive/Colab Notebooks/data/Images/")
label_path= pd.read_csv("/content/drive/MyDrive/Colab Notebooks/data/metadata.csv")

label_data=pd.DataFrame(label_path)
label_data

class_names = ('clean', 'dirty')
num_classes = len(class_names)

img_size = (128, 128, 3)

print(f'{num_classes} classes: {class_names}\nimage size: {img_size}')


labels = []
images = []
for image in label_data.iloc:
    images.append(np.asarray(cv2.resize(cv2.imread(image_path + image[0], cv2.IMREAD_COLOR), img_size[0:2])[:, :, ::-1]))

    # labels will be in the form of a vector: [0, 1] or [1, 0]
    label = np.zeros(num_classes)
    label[image[1]] = 1
    labels.append(label)

labels = np.asarray(labels)
images = np.asarray(images)

print(f'\nlabels shape: {labels.shape}')
print(f'images shape: {images.shape}')


#train test split
X_train, X_test, Y_train, Y_test=train_test_split(images, labels, test_size=0.3, random_state=42)
print(X_train.shape, "\n", Y_train.shape, "\n",  X_test.shape, "\n" , Y_test.shape)
#Output: (165, 128, 128, 3) 
 (165, 2) 
 (72, 128, 128, 3) 
 (72, 2)

train_image_generator=tf.keras.preprocessing.image.ImageDataGenerator(shear_range=0.5, zoom_range=0.2, vertical_flip=True, horizontal_flip=True)
train_image_generator= train_image_generator.flow(X_train, Y_train)

test_image_generator=tf.keras.preprocessing.image.ImageDataGenerator(shear_range=0.5, zoom_range=0.2, vertical_flip=True, horizontal_flip=True)
test_image_generator= test_image_generator.flow(X_test, Y_test)


base_model=tf.keras.applications.MobileNetV2(weights="imagenet", include_top=False, input_shape=(224, 224, 3))
output=base_model.output
output = tf.keras.layers.GlobalAveragePooling2D()(output)
output = tf.keras.layers.Dense(1024, activation="relu")(output)
output = tf.keras.layers.Dense(512, activation="relu")(output)
output = tf.keras.layers.Dense(3, activation="softmax")(output)
model = tf.keras.Model(inputs=base_model.input, outputs=output)

print(X_train.shape, "\n", Y_train.shape, "\n",  X_test.shape, "\n" , Y_test.shape)
model.compile(
  optimizer='adam',
  loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True, label_smoothing=0.1),
  metrics=['accuracy'])

checkpoint_callback = ModelCheckpoint('cnn_model/model{epoch:02d}')

train_size = X_train.shape[0]
valid_size = X_test.shape[0]
steps_per_epoch = train_size // 200
validation_steps = valid_size // 200

hist = model.fit(train_image_generator, batch_size=200, epochs=20, verbose=1, callbacks=[checkpoint_callback],
                         validation_data=test_image_generator,shuffle = True)
#output:
(165, 128, 128, 3) 
 (165, 2) 
 (72, 128, 128, 3) 
 (72, 2)
Epoch 1/20
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-62-9476c3b0457e> in <cell line: 14>()
     12 validation_steps = valid_size // 200
     13 
---> 14 hist = model.fit(train_image_generator, batch_size=200, epochs=20, verbose=1, callbacks=[checkpoint_callback],
     15                          validation_data=test_image_generator,shuffle = True)

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 <module>
      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>
      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 "<ipython-input-62-9476c3b0457e>", line 14, in <cell line: 14>
      hist = model.fit(train_image_generator, batch_size=200, epochs=20, verbose=1, callbacks=[checkpoint_callback],
    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,3] labels_size=[32,2]
	 [[{{node categorical_crossentropy/softmax_cross_entropy_with_logits}}]] [Op:__inference_train_function_60472]

How can I solve this???

Hi @Subhrajyoti_25

Welcome to the TensorFlow Forum!

There are 3 mistakes observed in the above mentioned code.

  1. You have 2 labels in the dataset but you have mentioned in the final Dense layers as 3.
    output = tf.keras.layers.Dense(3, activation="softmax")(output)
  2. The input_shape(224, 224, 3) in base_model is not matching with the dataset input_shape(128, 128, 3)
  3. You have already defined the Softmax activation function in the final layer of the model which will collect the appropriate logits while model training, but you again defined the from_logits=True in the CategoricalCrossentropy loss function of model.compile().

Please check the correct model definition and model compile code as below and try running the code again:

base_model=tf.keras.applications.MobileNetV2(weights="imagenet", include_top=False, input_shape=(128, 128, 3))
output=base_model.output
output = tf.keras.layers.GlobalAveragePooling2D()(output)
output = tf.keras.layers.Dense(1024, activation="relu")(output)
output = tf.keras.layers.Dense(512, activation="relu")(output)
output = tf.keras.layers.Dense(2, activation="softmax")(output)
model = tf.keras.Model(inputs=base_model.input, outputs=output)

model.compile(optimizer='adam',
              loss=tf.keras.losses.CategoricalCrossentropy(label_smoothing=0.1),
              metrics=['accuracy'])

(Attaching the replicated gist for your reference.)