How can I solve "InvalidArgumentError: Graph execution error:"

I’m facing the below issue when trying to train the model, can someone please help me.

InvalidArgumentError Traceback (most recent call last)
in <cell line: 1>()
25
26 for epoch in range(num_epochs):
—> 27 gan.fit(images_dataset, epochs=1)
28 g_model.save(“/content/drive/MyDrive/GAN/nn/BCg_model.h5”)
29 d_model.save(“/content/drive/MyDrive/GAN/nn/BCd_model.h5”)

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)
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:

InvalidArgumentError: Graph execution error:

@Mushrat_Jahan,

Welcome to the Tensorflow Forum!

Could you please share standalone code to debug the issue?

Thank you!

I was trying to fit the GAN model and faced an error. model fitting code is given below-

if name == “main”:
## Hyperparameters
batch_size = 128
latent_dim = 128
num_epochs = 250
images_path = glob(“/content/drive/MyDrive/Research/Image/hasnahena/*”)
d_model = build_discriminator()
g_model = build_generator(latent_dim)

 #d_model.load_weights("/content/drive/MyDrive/Dataset orginial/output/output/d_model.h5")
 #g_model.load_weights("/content/drive/MyDrive/Dataset orginial/output/output/g_model.h5")

d_model.summary()
g_model.summary()

gan = GAN(d_model, g_model, latent_dim)

bce_loss_fn = tf.keras.losses.BinaryCrossentropy(from_logits=True, label_smoothing=0.1)
d_optimizer = tf.keras.optimizers.Adam(learning_rate=0.0002, beta_1=0.5)
g_optimizer = tf.keras.optimizers.Adam(learning_rate=0.0002, beta_1=0.5)
gan.compile(d_optimizer, g_optimizer, bce_loss_fn)

images_dataset = tf_dataset(images_path, batch_size)

for epoch in range(num_epochs):
    gan.fit(images_dataset, epochs=1)
    g_model.save("/content/drive/MyDrive/GAN/nn/BCg_model.h5")
    d_model.save("/content/drive/MyDrive/GAN/nn/BCd_model.h5")

    n_samples = 1
    noise = np.random.normal(size=(n_samples, latent_dim))
    examples = g_model.predict(noise)
    save_plot(examples, epoch, int(np.sqrt(n_samples)))

I’m facing the below issue-

InvalidArgumentError Traceback (most recent call last)
in <cell line: 1>()
25
26 for epoch in range(num_epochs):
—> 27 gan.fit(images_dataset, epochs=1)
28 g_model.save(“/content/drive/MyDrive/GAN/nn/BCg_model.h5”)
29 d_model.save(“/content/drive/MyDrive/GAN/nn/BCd_model.h5”)

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)
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:

InvalidArgumentError: Graph execution error:

Detected at node ‘IteratorGetNext’ 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/ipykernel_launcher.py”, line 16, in
app.launch_new_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 27, in <cell line: 1>
gan.fit(images_dataset, epochs=1)
File “/usr/local/lib/python3.10/dist-packages/keras/utils/traceback_utils.py”, line 64, in error_handler
return fn(*args, **kwargs)
File “/usr/local/lib/python3.10/dist-packages/keras/engine/training.py”, line 1384, in fit
tmp_logs = self.train_function(iterator)
File “/usr/local/lib/python3.10/dist-packages/keras/engine/training.py”, line 1021, in train_function
return step_function(self, iterator)
File “/usr/local/lib/python3.10/dist-packages/keras/engine/training.py”, line 1009, in step_function
data = next(iterator)
Node: ‘IteratorGetNext’
Detected at node ‘IteratorGetNext’ 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/ipykernel_launcher.py”, line 16, in
app.launch_new_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 27, in <cell line: 1>
gan.fit(images_dataset, epochs=1)
File “/usr/local/lib/python3.10/dist-packages/keras/utils/traceback_utils.py”, line 64, in error_handler
return fn(*args, **kwargs)
File “/usr/local/lib/python3.10/dist-packages/keras/engine/training.py”, line 1384, in fit
tmp_logs = self.train_function(iterator)
File “/usr/local/lib/python3.10/dist-packages/keras/engine/training.py”, line 1021, in train_function
return step_function(self, iterator)
File “/usr/local/lib/python3.10/dist-packages/keras/engine/training.py”, line 1009, in step_function
data = next(iterator)
Node: ‘IteratorGetNext’
2 root error(s) found.
(0) INVALID_ARGUMENT: Cannot add tensor to the batch: number of elements does not match. Shapes are: [tensor]: [224,224,3], [batch]: [224,224,1]
[[{{node IteratorGetNext}}]]
[[Shape/_4]]
(1) INVALID_ARGUMENT: Cannot add tensor to the batch: number of elements does not match. Shapes are: [tensor]: [224,224,3], [batch]: [224,224,1]
[[{{node IteratorGetNext}}]]
0 successful operations.
0 derived errors ignored. [Op:__inference_train_function_9683]

It seems shape mismatch issue between the tensors and the batch in your code.

What is the shape of your input images? You need to ensure that the number of channels in the input tensor matches the number of channels in the batch.

Could you provide tf_dataset() implementation part?

Thank you!

Here is the shape of my image-

IMG_H = 224
IMG_W = 224
IMG_C = 1 ## Change this to 1 for grayscale.
w_init = tf.keras.initializers.RandomNormal(mean=0.0, stddev=0.02)

#tf_dataset part
def tf_dataset(images_path, batch_size):
dataset = tf.data.Dataset.from_tensor_slices(images_path)
dataset = dataset.shuffle(buffer_size=10240)
dataset = dataset.map(load_image, num_parallel_calls=tf.data.experimental.AUTOTUNE)
dataset = dataset.batch(batch_size)
dataset = dataset.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)
return dataset

@Mushrat_Jahan,

It seems that model is expecting RGB image (224,224,3) but the input image are of grayscale (224,224,1). Please ensure the shape compatibility for the model to run.

Thank you!

1 Like

image_height=256
image_width=256
num_channels=3
num_classes=2
input_shape = (image_height, image_width, num_channels)
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation=‘relu’, input_shape=input_shape))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, kernel_size=(3, 3), activation=‘relu’))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(128, activation=‘relu’))
model.add(Dense(num_classes, activation=‘softmax’)) # Adjust num_classes as needed
model.summary()
model.compile(loss=SparseCategoricalCrossentropy(), optimizer=‘rmsprop’, metrics=[‘accuracy’])

Train the model

model.fit(X_train, y_train, batch_size=32, epochs=10, verbose=1)

I had the same issue, however the issue was fixed after I changed my TF version from 2.14.0 to 2.13.0.

1 Like

i was facing the same error,check the units value and your classes.If your classes are two then the value must be 2 and same for other no of classes.

I have a similar issue but my images already have 3 channels.
Could someone help me with this error?
Following I added my model summary.

InvalidArgumentError: Graph execution error:

Detected at node decode_image/DecodeImage defined at (most recent call last):

Number of channels inherent in the image must be 1, 3 or 4, was 2
[[{{node decode_image/DecodeImage}}]]
[[IteratorGetNext]] [Op:__inference_train_function_7266]

Model: “sequential_1”


Layer (type) Output Shape Param #

conv2d_4 (Conv2D) (None, 198, 198, 32) 896

max_pooling2d_4 (MaxPoolin (None, 99, 99, 32) 0
g2D)

conv2d_5 (Conv2D) (None, 97, 97, 64) 18496

max_pooling2d_5 (MaxPoolin (None, 48, 48, 64) 0
g2D)

conv2d_6 (Conv2D) (None, 46, 46, 64) 36928

max_pooling2d_6 (MaxPoolin (None, 23, 23, 64) 0
g2D)

conv2d_7 (Conv2D) (None, 21, 21, 64) 36928

max_pooling2d_7 (MaxPoolin (None, 10, 10, 64) 0
g2D)

flatten_1 (Flatten) (None, 6400) 0

dense_4 (Dense) (None, 512) 3277312

batch_normalization_3 (Bat (None, 512) 2048
chNormalization)

dense_5 (Dense) (None, 512) 262656

dropout_2 (Dropout) (None, 512) 0

batch_normalization_4 (Bat (None, 512) 2048
chNormalization)

dense_6 (Dense) (None, 512) 262656

dropout_3 (Dropout) (None, 512) 0

batch_normalization_5 (Bat (None, 512) 2048
chNormalization)

dense_7 (Dense) (None, 1) 513

=================================================================
Total params: 3902529 (14.89 MB)
Trainable params: 3899457 (14.88 MB)
Non-trainable params: 3072 (12.00 KB)


I have the same issues how can I resolve it?
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 model/dense/MatMul defined at (most recent call last):
File “/usr/lib/python3.10/runpy.py”, line 196, in _run_module_as_main

File “/usr/lib/python3.10/runpy.py”, line 86, in _run_code

File “/usr/local/lib/python3.10/dist-packages/colab_kernel_launcher.py”, line 37, in

File “/usr/local/lib/python3.10/dist-packages/traitlets/config/application.py”, line 992, in launch_instance

File “/usr/local/lib/python3.10/dist-packages/ipykernel/kernelapp.py”, line 619, in start

File “/usr/local/lib/python3.10/dist-packages/tornado/platform/asyncio.py”, line 195, in start

File “/usr/lib/python3.10/asyncio/base_events.py”, line 603, in run_forever

File “/usr/lib/python3.10/asyncio/base_events.py”, line 1909, in _run_once

File “/usr/lib/python3.10/asyncio/events.py”, line 80, in _run

File “/usr/local/lib/python3.10/dist-packages/tornado/ioloop.py”, line 685, in

File “/usr/local/lib/python3.10/dist-packages/tornado/ioloop.py”, line 738, in _run_callback

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

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

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

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 5, in <cell line: 5>

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 1150, in train_step

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 590, in call

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/base_layer.py”, line 1149, in call

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

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

File “/usr/local/lib/python3.10/dist-packages/keras/src/engine/functional.py”, line 672, in _run_internal_graph

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/base_layer.py”, line 1149, in call

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

File “/usr/local/lib/python3.10/dist-packages/keras/src/layers/core/dense.py”, line 241, in call

Matrix size-incompatible: In[0]: [32,221952], In[1]: [37632,1024]
[[{{node model/dense/MatMul}}]] [Op:__inference_train_function_10869]

1 Like

The same issue occurs at my end; if you resolve it, please let me know how to fix it.