tf-keras icon indicating copy to clipboard operation
tf-keras copied to clipboard

InvalidArgumentError: Graph execution error:

Open David-hosting opened this issue 3 years ago • 53 comments

I am trying to make an image classification program. I am following a video and I ran into some problems, some I think I solved, and others I just couldn't. I am trying to create a model with Transfer Learning and up until now the video was kind of ok but I think it is outdated. I searched the web but couldn't find anything to help solve my problem. Anything would be helpful.

Here is my code:

    !pip install -q -U "tensorflow-gpu==2.2.0"
    !pip install -q -U tensorflow_hub
    !pip install -q -U tensorflow_datasets

    import time
    import numpy as np
    import matplotlib.pylab as plt

    import tensorflow as tf
    import tensorflow_hub as hub
    import tensorflow_datasets as tfds
    tfds.disable_progress_bar()

    from tensorflow.keras import layers

    #Here was supposed to be a split function to split the data 80% (train), 20% (validation), I don't know what I did but in the line below I did "split=['train[:80%]', 'train[20%:]']" is it ok? or should I change something there?
    splits, info = tfds.load('cats_vs_dogs', with_info=True, as_supervised=True, split=['train[:80%]', 'train[20%:]'])

    (train_examples, validation_examples) = splits

    def format_image(image, label):
        images = tf.image.resize(image, (IMAGE_RES, IMAGE_RES))//255.0
        return image, label

    num_examples = info.splits['train'].num_examples

    BATCH_SIZE = 32
    IMAGE_RES = 224

    train_batches      = train_examples.cache().shuffle(num_examples//4).map(format_image).batch(BATCH_SIZE).prefetch(1)
    validation_batches = validation_examples.cache().map(format_image).batch(BATCH_SIZE).prefetch(1)

    URL = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4"
    feature_extractor = hub.KerasLayer(URL, input_shape=(IMAGE_RES,IMAGE_RES,3))

    feature_extractor.trainable = False

    model = tf.keras.Sequential([
        feature_extractor,
        layers.Dense(2, activation='softmax')
    ])

    model.summary()

    model.compile(
        optimizer='adam',
        loss=tf.losses.SparseCategoricalCrossentropy(),
        metrics=['accuracy'])

    EPOCHS = 2
    history = model.fit(train_batches,
                        epochs=EPOCHS,
                        validation_data=validation_batches) #From here I get the problem

Model Summary: image

Error:

InvalidArgumentError: Graph execution error:

Detected at node 'IteratorGetNext' defined at (most recent call last):
    File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main
      return _run_code(code, main_globals, None,
    File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
      exec(code, run_globals)
    File "/usr/local/lib/python3.8/dist-packages/ipykernel_launcher.py", line 16, in <module>
      app.launch_new_instance()
    File "/usr/local/lib/python3.8/dist-packages/traitlets/config/application.py", line 992, in launch_instance
      app.start()
    File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelapp.py", line 612, in start
      self.io_loop.start()
    File "/usr/local/lib/python3.8/dist-packages/tornado/platform/asyncio.py", line 149, in start
      self.asyncio_loop.run_forever()
    File "/usr/lib/python3.8/asyncio/base_events.py", line 570, in run_forever
      self._run_once()
    File "/usr/lib/python3.8/asyncio/base_events.py", line 1859, in _run_once
      handle._run()
    File "/usr/lib/python3.8/asyncio/events.py", line 81, in _run
      self._context.run(self._callback, *self._args)
    File "/usr/local/lib/python3.8/dist-packages/tornado/ioloop.py", line 690, in <lambda>
      lambda f: self._run_callback(functools.partial(callback, future))
    File "/usr/local/lib/python3.8/dist-packages/tornado/ioloop.py", line 743, in _run_callback
      ret = callback()
    File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 787, in inner
      self.run()
    File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 748, in run
      yielded = self.gen.send(value)
    File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelbase.py", line 365, in process_one
      yield gen.maybe_future(dispatch(*args))
    File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 209, in wrapper
      yielded = next(result)
    File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelbase.py", line 268, in dispatch_shell
      yield gen.maybe_future(handler(stream, idents, msg))
    File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 209, in wrapper
      yielded = next(result)
    File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelbase.py", line 543, in execute_request
      self.do_execute(
    File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 209, in wrapper
      yielded = next(result)
    File "/usr/local/lib/python3.8/dist-packages/ipykernel/ipkernel.py", line 306, in do_execute
      res = shell.run_cell(code, store_history=store_history, silent=silent)
    File "/usr/local/lib/python3.8/dist-packages/ipykernel/zmqshell.py", line 536, in run_cell
      return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
    File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 2854, in run_cell
      result = self._run_cell(
    File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 2881, in _run_cell
      return runner(coro)
    File "/usr/local/lib/python3.8/dist-packages/IPython/core/async_helpers.py", line 68, in _pseudo_sync_runner
      coro.send(None)
    File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 3057, in run_cell_async
      has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
    File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 3249, in run_ast_nodes
      if (await self.run_code(code, result,  async_=asy)):
    File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 3326, in run_code
      exec(code_obj, self.user_global_ns, self.user_ns)
    File "<ipython-input-11-ce74159d340b>", line 7, in <module>
      history = model.fit(train_batches,
    File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 64, in error_handler
      return fn(*args, **kwargs)
    File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1409, in fit
      tmp_logs = self.train_function(iterator)
    File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1051, in train_function
      return step_function(self, iterator)
    File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1039, in step_function
      data = next(iterator)
Node: 'IteratorGetNext'
Detected at node 'IteratorGetNext' defined at (most recent call last):
    File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main
      return _run_code(code, main_globals, None,
    File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
      exec(code, run_globals)
    File "/usr/local/lib/python3.8/dist-packages/ipykernel_launcher.py", line 16, in <module>
      app.launch_new_instance()
    File "/usr/local/lib/python3.8/dist-packages/traitlets/config/application.py", line 992, in launch_instance
      app.start()
    File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelapp.py", line 612, in start
      self.io_loop.start()
    File "/usr/local/lib/python3.8/dist-packages/tornado/platform/asyncio.py", line 149, in start
      self.asyncio_loop.run_forever()
    File "/usr/lib/python3.8/asyncio/base_events.py", line 570, in run_forever
      self._run_once()
    File "/usr/lib/python3.8/asyncio/base_events.py", line 1859, in _run_once
      handle._run()
    File "/usr/lib/python3.8/asyncio/events.py", line 81, in _run
      self._context.run(self._callback, *self._args)
    File "/usr/local/lib/python3.8/dist-packages/tornado/ioloop.py", line 690, in <lambda>
      lambda f: self._run_callback(functools.partial(callback, future))
    File "/usr/local/lib/python3.8/dist-packages/tornado/ioloop.py", line 743, in _run_callback
      ret = callback()
    File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 787, in inner
      self.run()
    File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 748, in run
      yielded = self.gen.send(value)
    File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelbase.py", line 365, in process_one
      yield gen.maybe_future(dispatch(*args))
    File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 209, in wrapper
      yielded = next(result)
    File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelbase.py", line 268, in dispatch_shell
      yield gen.maybe_future(handler(stream, idents, msg))
    File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 209, in wrapper
      yielded = next(result)
    File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelbase.py", line 543, in execute_request
      self.do_execute(
    File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 209, in wrapper
      yielded = next(result)
    File "/usr/local/lib/python3.8/dist-packages/ipykernel/ipkernel.py", line 306, in do_execute
      res = shell.run_cell(code, store_history=store_history, silent=silent)
    File "/usr/local/lib/python3.8/dist-packages/ipykernel/zmqshell.py", line 536, in run_cell
      return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
    File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 2854, in run_cell
      result = self._run_cell(
    File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 2881, in _run_cell
      return runner(coro)
    File "/usr/local/lib/python3.8/dist-packages/IPython/core/async_helpers.py", line 68, in _pseudo_sync_runner
      coro.send(None)
    File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 3057, in run_cell_async
      has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
    File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 3249, in run_ast_nodes
      if (await self.run_code(code, result,  async_=asy)):
    File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 3326, in run_code
      exec(code_obj, self.user_global_ns, self.user_ns)
    File "<ipython-input-11-ce74159d340b>", line 7, in <module>
      history = model.fit(train_batches,
    File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 64, in error_handler
      return fn(*args, **kwargs)
    File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1409, in fit
      tmp_logs = self.train_function(iterator)
    File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1051, in train_function
      return step_function(self, iterator)
    File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1039, in step_function
      data = next(iterator)
Node: 'IteratorGetNext'
2 root error(s) found.
  (0) INVALID_ARGUMENT:  Cannot batch tensors with different shapes in component 0. First element had shape [408,500,3] and element 1 had shape [360,343,3].
	 [[{{node IteratorGetNext}}]]
	 [[IteratorGetNext/_2]]
  (1) INVALID_ARGUMENT:  Cannot batch tensors with different shapes in component 0. First element had shape [408,500,3] and element 1 had shape [360,343,3].
	 [[{{node IteratorGetNext}}]]
0 successful operations.
0 derived errors ignored. [Op:__inference_train_function_24172]

David-hosting avatar Dec 21 '22 18:12 David-hosting

@David-hosting, This is a known issue. Developers are working on resolving this bug. Also could you please take a look at the issue and comment from the developer with the similar feature for the reference. Thank you

tilakrayal avatar Dec 22 '22 11:12 tilakrayal

This issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Thank you.

google-ml-butler[bot] avatar Dec 29 '22 12:12 google-ml-butler[bot]

hello! i have the same bug.. is there any solution for it?

damlaYasarr avatar Jan 04 '23 20:01 damlaYasarr

I am also facing the same error but in the RandomFlip layer. Will be grateful if anyone can tell me a fix for this.

AakarSharmaHME avatar Jan 07 '23 12:01 AakarSharmaHME

bueno no se paso gracias

DanielLopez651 avatar Jan 23 '23 17:01 DanielLopez651

facing same error

Indranil-359 avatar Jan 28 '23 18:01 Indranil-359

Hello, I ran into the same problem today and have no idea how to deal with this. It would be great if someone can provide some advise.

# code snippet

model = tf.keras.Sequential([ tf.keras.layers.Dense(16, activation='relu'), tf.keras.layers.Dense(16, activation='relu'), tf.keras.layers.Dense(1, activation='sigmoid') ])

model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), loss=tf.keras.losses.BinaryCrossentropy(), metrics=['accuracy'])

model.fit(x_train, y_train, batch_size=16, epochs=20, validation_data=(x_valid, y_valid))

xoumyax avatar Feb 02 '23 03:02 xoumyax

I'm facing the same issue.....can anyone tell me what to do....?

JayanthBobbili avatar Feb 03 '23 15:02 JayanthBobbili

I am straugleing with the same issue while working on a project , any body can help would be very much appriciated . Thank you ! Below is the error........

Epoch 1/20

InvalidArgumentError Traceback (most recent call last) in ----> 1 history = model.fit( 2 train_generator, 3 steps_per_epoch=47, 4 batch_size=32, 5 validation_data=validation_generator,

1 frames /usr/local/lib/python3.8/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 'sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits' defined at (most recent call last): File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main return _run_code(code, main_globals, None, File "/usr/lib/python3.8/runpy.py", line 87, in _run_code exec(code, run_globals) File "/usr/local/lib/python3.8/dist-packages/ipykernel_launcher.py", line 16, in app.launch_new_instance() File "/usr/local/lib/python3.8/dist-packages/traitlets/config/application.py", line 992, in launch_instance app.start() File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelapp.py", line 612, in start self.io_loop.start() File "/usr/local/lib/python3.8/dist-packages/tornado/platform/asyncio.py", line 149, in start self.asyncio_loop.run_forever() File "/usr/lib/python3.8/asyncio/base_events.py", line 570, in run_forever self._run_once() File "/usr/lib/python3.8/asyncio/base_events.py", line 1859, in _run_once handle._run() File "/usr/lib/python3.8/asyncio/events.py", line 81, in _run self._context.run(self._callback, *self._args) File "/usr/local/lib/python3.8/dist-packages/tornado/ioloop.py", line 690, in lambda f: self._run_callback(functools.partial(callback, future)) File "/usr/local/lib/python3.8/dist-packages/tornado/ioloop.py", line 743, in _run_callback ret = callback() File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 787, in inner self.run() File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 748, in run yielded = self.gen.send(value) File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelbase.py", line 365, in process_one yield gen.maybe_future(dispatch(*args)) File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 209, in wrapper yielded = next(result) File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelbase.py", line 268, in dispatch_shell yield gen.maybe_future(handler(stream, idents, msg)) File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 209, in wrapper yielded = next(result) File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelbase.py", line 543, in execute_request self.do_execute( File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 209, in wrapper yielded = next(result) File "/usr/local/lib/python3.8/dist-packages/ipykernel/ipkernel.py", line 306, in do_execute res = shell.run_cell(code, store_history=store_history, silent=silent) File "/usr/local/lib/python3.8/dist-packages/ipykernel/zmqshell.py", line 536, in run_cell return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs) File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 2854, in run_cell result = self._run_cell( File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 2881, in _run_cell return runner(coro) File "/usr/local/lib/python3.8/dist-packages/IPython/core/async_helpers.py", line 68, in pseudo_sync_runner coro.send(None) File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 3057, in run_cell_async has_raised = await self.run_ast_nodes(code_ast.body, cell_name, File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 3249, in run_ast_nodes if (await self.run_code(code, result, async=asy)): File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 3326, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "", line 1, in history = model.fit( File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 64, in error_handler return fn(*args, **kwargs) File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1409, in fit tmp_logs = self.train_function(iterator) File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1051, in train_function return step_function(self, iterator) File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1040, in step_function outputs = model.distribute_strategy.run(run_step, args=(data,)) File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1030, in run_step outputs = model.train_step(data) File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 890, in train_step loss = self.compute_loss(x, y, y_pred, sample_weight) File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 948, in compute_loss return self.compiled_loss( File "/usr/local/lib/python3.8/dist-packages/keras/engine/compile_utils.py", line 201, in call loss_value = loss_obj(y_t, y_p, sample_weight=sw) File "/usr/local/lib/python3.8/dist-packages/keras/losses.py", line 139, in call losses = call_fn(y_true, y_pred) File "/usr/local/lib/python3.8/dist-packages/keras/losses.py", line 243, in call return ag_fn(y_true, y_pred, **self._fn_kwargs) File "/usr/local/lib/python3.8/dist-packages/keras/losses.py", line 1860, in sparse_categorical_crossentropy return backend.sparse_categorical_crossentropy( File "/usr/local/lib/python3.8/dist-packages/keras/backend.py", line 5238, in sparse_categorical_crossentropy res = tf.nn.sparse_softmax_cross_entropy_with_logits( Node: 'sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits' Received a label value of 14 which is outside the valid range of [0, 3). Label values: 1 10 14 14 5 1 12 2 12 11 4 9 12 14 0 5 7 1 4 9 12 7 9 7 14 7 1 12 9 2 12 14 [[{{node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits}}]] [Op:__inference_train_function_1163]

573-pankaj avatar Feb 05 '23 14:02 573-pankaj

I am straugleing with the same issue while working on a project , any body can help would be very much appriciated . Thank you ! Below is the error........

573-pankaj avatar Feb 05 '23 14:02 573-pankaj

@573-pankaj Is there any solution for that issue I'm also facing this issue so, if you find any solution please share with community.Thanks

Abdullah-Fiaz avatar Feb 06 '23 17:02 Abdullah-Fiaz

Okay will share!

573-pankaj avatar Feb 06 '23 17:02 573-pankaj

I was also faccing the same issue. I resolved it by properly using the type of data that I am feeding in my model. Try seeing the tensors types that you guys are using. For eg I was returning the data in numpy tensors and I was directly using it in my model witthout saig in numpy file and then reloading it from there.

In nutshell I just want to highlight that I was getting this isssue becasue of mismatching between the data objecct that I was feeding my model.

AnujLahoty avatar Feb 08 '23 06:02 AnujLahoty

same problem im facing is there any solution to find out

bindu56 avatar Feb 08 '23 09:02 bindu56

I'm also getting the same error. Any solution yet?

Epoch 1/20

InvalidArgumentError Traceback (most recent call last) Input In [25], in <cell line: 5>() 2 model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False), metrics=['accuracy']) 4 #Fit the model ----> 5 history = model.fit(train, epochs=EPOCHS, batch_size=BATCH_SIZE, verbose=1, validation_data=val)

File ~\anaconda\lib\site-packages\keras\utils\traceback_utils.py:67, in filter_traceback..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 ~\anaconda\lib\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:

Input is empty. [[{{node decode_image/DecodeImage}}]] [[IteratorGetNext]] [Op:__inference_train_function_3767]

husainridwan avatar Feb 09 '23 16:02 husainridwan

I am trying to make an image classification program. I am following a video and I ran into some problems, some I think I solved, and others I just couldn't. I am trying to create a model with Transfer Learning and up until now the video was kind of ok but I think it is outdated. I searched the web but couldn't find anything to help solve my problem. Anything would be helpful.

Here is my code:

    !pip install -q -U "tensorflow-gpu==2.2.0"
    !pip install -q -U tensorflow_hub
    !pip install -q -U tensorflow_datasets

    import time
    import numpy as np
    import matplotlib.pylab as plt

    import tensorflow as tf
    import tensorflow_hub as hub
    import tensorflow_datasets as tfds
    tfds.disable_progress_bar()

    from tensorflow.keras import layers

    #Here was supposed to be a split function to split the data 80% (train), 20% (validation), I don't know what I did but in the line below I did "split=['train[:80%]', 'train[20%:]']" is it ok? or should I change something there?
    splits, info = tfds.load('cats_vs_dogs', with_info=True, as_supervised=True, split=['train[:80%]', 'train[20%:]'])

    (train_examples, validation_examples) = splits

    def format_image(image, label):
        images = tf.image.resize(image, (IMAGE_RES, IMAGE_RES))//255.0
        return image, label

    num_examples = info.splits['train'].num_examples

    BATCH_SIZE = 32
    IMAGE_RES = 224

    train_batches      = train_examples.cache().shuffle(num_examples//4).map(format_image).batch(BATCH_SIZE).prefetch(1)
    validation_batches = validation_examples.cache().map(format_image).batch(BATCH_SIZE).prefetch(1)

    URL = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4"
    feature_extractor = hub.KerasLayer(URL, input_shape=(IMAGE_RES,IMAGE_RES,3))

    feature_extractor.trainable = False

    model = tf.keras.Sequential([
        feature_extractor,
        layers.Dense(2, activation='softmax')
    ])

    model.summary()

    model.compile(
        optimizer='adam',
        loss=tf.losses.SparseCategoricalCrossentropy(),
        metrics=['accuracy'])

    EPOCHS = 2
    history = model.fit(train_batches,
                        epochs=EPOCHS,
                        validation_data=validation_batches) #From here I get the problem

Model Summary: image

Error:

InvalidArgumentError: Graph execution error:

Detected at node 'IteratorGetNext' defined at (most recent call last):
    File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main
      return _run_code(code, main_globals, None,
    File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
      exec(code, run_globals)
    File "/usr/local/lib/python3.8/dist-packages/ipykernel_launcher.py", line 16, in <module>
      app.launch_new_instance()
    File "/usr/local/lib/python3.8/dist-packages/traitlets/config/application.py", line 992, in launch_instance
      app.start()
    File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelapp.py", line 612, in start
      self.io_loop.start()
    File "/usr/local/lib/python3.8/dist-packages/tornado/platform/asyncio.py", line 149, in start
      self.asyncio_loop.run_forever()
    File "/usr/lib/python3.8/asyncio/base_events.py", line 570, in run_forever
      self._run_once()
    File "/usr/lib/python3.8/asyncio/base_events.py", line 1859, in _run_once
      handle._run()
    File "/usr/lib/python3.8/asyncio/events.py", line 81, in _run
      self._context.run(self._callback, *self._args)
    File "/usr/local/lib/python3.8/dist-packages/tornado/ioloop.py", line 690, in <lambda>
      lambda f: self._run_callback(functools.partial(callback, future))
    File "/usr/local/lib/python3.8/dist-packages/tornado/ioloop.py", line 743, in _run_callback
      ret = callback()
    File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 787, in inner
      self.run()
    File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 748, in run
      yielded = self.gen.send(value)
    File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelbase.py", line 365, in process_one
      yield gen.maybe_future(dispatch(*args))
    File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 209, in wrapper
      yielded = next(result)
    File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelbase.py", line 268, in dispatch_shell
      yield gen.maybe_future(handler(stream, idents, msg))
    File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 209, in wrapper
      yielded = next(result)
    File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelbase.py", line 543, in execute_request
      self.do_execute(
    File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 209, in wrapper
      yielded = next(result)
    File "/usr/local/lib/python3.8/dist-packages/ipykernel/ipkernel.py", line 306, in do_execute
      res = shell.run_cell(code, store_history=store_history, silent=silent)
    File "/usr/local/lib/python3.8/dist-packages/ipykernel/zmqshell.py", line 536, in run_cell
      return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
    File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 2854, in run_cell
      result = self._run_cell(
    File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 2881, in _run_cell
      return runner(coro)
    File "/usr/local/lib/python3.8/dist-packages/IPython/core/async_helpers.py", line 68, in _pseudo_sync_runner
      coro.send(None)
    File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 3057, in run_cell_async
      has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
    File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 3249, in run_ast_nodes
      if (await self.run_code(code, result,  async_=asy)):
    File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 3326, in run_code
      exec(code_obj, self.user_global_ns, self.user_ns)
    File "<ipython-input-11-ce74159d340b>", line 7, in <module>
      history = model.fit(train_batches,
    File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 64, in error_handler
      return fn(*args, **kwargs)
    File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1409, in fit
      tmp_logs = self.train_function(iterator)
    File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1051, in train_function
      return step_function(self, iterator)
    File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1039, in step_function
      data = next(iterator)
Node: 'IteratorGetNext'
Detected at node 'IteratorGetNext' defined at (most recent call last):
    File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main
      return _run_code(code, main_globals, None,
    File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
      exec(code, run_globals)
    File "/usr/local/lib/python3.8/dist-packages/ipykernel_launcher.py", line 16, in <module>
      app.launch_new_instance()
    File "/usr/local/lib/python3.8/dist-packages/traitlets/config/application.py", line 992, in launch_instance
      app.start()
    File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelapp.py", line 612, in start
      self.io_loop.start()
    File "/usr/local/lib/python3.8/dist-packages/tornado/platform/asyncio.py", line 149, in start
      self.asyncio_loop.run_forever()
    File "/usr/lib/python3.8/asyncio/base_events.py", line 570, in run_forever
      self._run_once()
    File "/usr/lib/python3.8/asyncio/base_events.py", line 1859, in _run_once
      handle._run()
    File "/usr/lib/python3.8/asyncio/events.py", line 81, in _run
      self._context.run(self._callback, *self._args)
    File "/usr/local/lib/python3.8/dist-packages/tornado/ioloop.py", line 690, in <lambda>
      lambda f: self._run_callback(functools.partial(callback, future))
    File "/usr/local/lib/python3.8/dist-packages/tornado/ioloop.py", line 743, in _run_callback
      ret = callback()
    File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 787, in inner
      self.run()
    File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 748, in run
      yielded = self.gen.send(value)
    File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelbase.py", line 365, in process_one
      yield gen.maybe_future(dispatch(*args))
    File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 209, in wrapper
      yielded = next(result)
    File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelbase.py", line 268, in dispatch_shell
      yield gen.maybe_future(handler(stream, idents, msg))
    File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 209, in wrapper
      yielded = next(result)
    File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelbase.py", line 543, in execute_request
      self.do_execute(
    File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 209, in wrapper
      yielded = next(result)
    File "/usr/local/lib/python3.8/dist-packages/ipykernel/ipkernel.py", line 306, in do_execute
      res = shell.run_cell(code, store_history=store_history, silent=silent)
    File "/usr/local/lib/python3.8/dist-packages/ipykernel/zmqshell.py", line 536, in run_cell
      return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
    File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 2854, in run_cell
      result = self._run_cell(
    File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 2881, in _run_cell
      return runner(coro)
    File "/usr/local/lib/python3.8/dist-packages/IPython/core/async_helpers.py", line 68, in _pseudo_sync_runner
      coro.send(None)
    File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 3057, in run_cell_async
      has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
    File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 3249, in run_ast_nodes
      if (await self.run_code(code, result,  async_=asy)):
    File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 3326, in run_code
      exec(code_obj, self.user_global_ns, self.user_ns)
    File "<ipython-input-11-ce74159d340b>", line 7, in <module>
      history = model.fit(train_batches,
    File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 64, in error_handler
      return fn(*args, **kwargs)
    File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1409, in fit
      tmp_logs = self.train_function(iterator)
    File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1051, in train_function
      return step_function(self, iterator)
    File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1039, in step_function
      data = next(iterator)
Node: 'IteratorGetNext'
2 root error(s) found.
  (0) INVALID_ARGUMENT:  Cannot batch tensors with different shapes in component 0. First element had shape [408,500,3] and element 1 had shape [360,343,3].
	 [[{{node IteratorGetNext}}]]
	 [[IteratorGetNext/_2]]
  (1) INVALID_ARGUMENT:  Cannot batch tensors with different shapes in component 0. First element had shape [408,500,3] and element 1 had shape [360,343,3].
	 [[{{node IteratorGetNext}}]]
0 successful operations.
0 derived errors ignored. [Op:__inference_train_function_24172]

I also faced difficult with the same error when i'm running image classification. The training images were MRI images (grayscale) and indexed images ie its dimension is like (221,181,1). I reduced the third dimension by the following code im = data.astype(np.uint8) im = im[:,:,0] I debugged and got results

Karthigai44 avatar Feb 10 '23 11:02 Karthigai44

@David-hosting I think the problem might be in the format_image method. It's not really formatting the images because the transformation is assigned to a variable images and the method is returning image (the same input)

Screen Shot 2023-02-14 at 21 43 33

jsalinasvela avatar Feb 15 '23 02:02 jsalinasvela

Mam, the problem was solved by me, that code is updated there for the first query. Thank you

Sincerely,. Dr.S.Karthigai Selvi

On Wed, Feb 15, 2023 at 8:16 AM Jesus Salinas Vela @.***> wrote:

@David-hosting https://github.com/David-hosting I think the problem might be in the format_image method. It's not really formatting the images because the transformation is assigned to a variable images and the method is returning image (the same input)

[image: Screen Shot 2023-02-14 at 21 43 33] https://user-images.githubusercontent.com/28662284/218914175-478c0fdb-22ff-4a0f-9133-862781a0499f.png

— Reply to this email directly, view it on GitHub https://github.com/keras-team/tf-keras/issues/66, or unsubscribe https://github.com/notifications/unsubscribe-auth/A5YU55QH5NX6PTUV2E7A7ADWXQ7PDANCNFSM6AAAAAATF5E7TI . You are receiving this because you commented.Message ID: @.***>

Karthigai44 avatar Feb 16 '23 05:02 Karthigai44

I also encountered the same error. I am a beginner and do not know how to use the model. Please tell me. Attached below is the code and error. Code>> model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) history = model.fit(my_train_images, my_train_labels, batch_size=100, epochs=30, validation_data=(my_test_images, my_test_labels)) Error>> Epoch 1/30

InvalidArgumentError Traceback (most recent call last) in 1 model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) ----> 2 history = model.fit(my_train_images, my_train_labels, batch_size=100, epochs=30, validation_data=(my_test_images, my_test_labels))

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

InvalidArgumentError: Graph execution error:

Detected at node 'sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits' defined at (most recent call last): File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main return _run_code(code, main_globals, None, File "/usr/lib/python3.8/runpy.py", line 87, in _run_code exec(code, run_globals) File "/usr/local/lib/python3.8/dist-packages/ipykernel_launcher.py", line 16, in app.launch_new_instance() File "/usr/local/lib/python3.8/dist-packages/traitlets/config/application.py", line 992, in launch_instance app.start() File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelapp.py", line 612, in start self.io_loop.start() File "/usr/local/lib/python3.8/dist-packages/tornado/platform/asyncio.py", line 215, in start self.asyncio_loop.run_forever() File "/usr/lib/python3.8/asyncio/base_events.py", line 570, in run_forever self._run_once() File "/usr/lib/python3.8/asyncio/base_events.py", line 1859, in _run_once handle._run() File "/usr/lib/python3.8/asyncio/events.py", line 81, in _run self._context.run(self._callback, *self._args) File "/usr/local/lib/python3.8/dist-packages/tornado/ioloop.py", line 687, in lambda f: self._run_callback(functools.partial(callback, future)) File "/usr/local/lib/python3.8/dist-packages/tornado/ioloop.py", line 740, in _run_callback ret = callback() File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 821, in inner self.ctx_run(self.run) File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 782, in run yielded = self.gen.send(value) File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelbase.py", line 365, in process_one yield gen.maybe_future(dispatch(*args)) File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 234, in wrapper yielded = ctx_run(next, result) File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelbase.py", line 268, in dispatch_shell yield gen.maybe_future(handler(stream, idents, msg)) File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 234, in wrapper yielded = ctx_run(next, result) File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelbase.py", line 543, in execute_request self.do_execute( File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 234, in wrapper yielded = ctx_run(next, result) File "/usr/local/lib/python3.8/dist-packages/ipykernel/ipkernel.py", line 306, in do_execute res = shell.run_cell(code, store_history=store_history, silent=silent) File "/usr/local/lib/python3.8/dist-packages/ipykernel/zmqshell.py", line 536, in run_cell return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs) File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 2854, in run_cell result = self._run_cell( File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 2881, in _run_cell return runner(coro) File "/usr/local/lib/python3.8/dist-packages/IPython/core/async_helpers.py", line 68, in pseudo_sync_runner coro.send(None) File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 3057, in run_cell_async has_raised = await self.run_ast_nodes(code_ast.body, cell_name, File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 3249, in run_ast_nodes if (await self.run_code(code, result, async=asy)): File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 3326, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "", line 2, in history = model.fit(my_train_images, my_train_labels, batch_size=100, epochs=30, validation_data=(my_test_images, my_test_labels)) File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 65, in error_handler return fn(*args, **kwargs) File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1650, in fit tmp_logs = self.train_function(iterator) File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1249, in train_function return step_function(self, iterator) File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1233, in step_function outputs = model.distribute_strategy.run(run_step, args=(data,)) File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1222, in run_step outputs = model.train_step(data) File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1024, in train_step loss = self.compute_loss(x, y, y_pred, sample_weight) File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1082, in compute_loss return self.compiled_loss( File "/usr/local/lib/python3.8/dist-packages/keras/engine/compile_utils.py", line 265, in call loss_value = loss_obj(y_t, y_p, sample_weight=sw) File "/usr/local/lib/python3.8/dist-packages/keras/losses.py", line 152, in call losses = call_fn(y_true, y_pred) File "/usr/local/lib/python3.8/dist-packages/keras/losses.py", line 284, in call return ag_fn(y_true, y_pred, **self._fn_kwargs) File "/usr/local/lib/python3.8/dist-packages/keras/losses.py", line 2098, in sparse_categorical_crossentropy return backend.sparse_categorical_crossentropy( File "/usr/local/lib/python3.8/dist-packages/keras/backend.py", line 5633, in sparse_categorical_crossentropy res = tf.nn.sparse_softmax_cross_entropy_with_logits( Node: 'sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits' logits and labels must have the same first dimension, got logits shape [36,6] and labels shape [216] [[{{node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits}}]] [Op:__inference_train_function_986]

KONTACT406 avatar Feb 21 '23 11:02 KONTACT406

Also facing the same problem, came here for solutions :)

SarfarajRahmn avatar Feb 24 '23 11:02 SarfarajRahmn

I also faced these issues but the issues I faced In auto keras for time series forecasting. when I debug it there were the issues with the input shape

iffishells avatar Feb 25 '23 06:02 iffishells

any workarounds?

NicoaraBogdan avatar Feb 26 '23 18:02 NicoaraBogdan

I managed to fix this by using an older version of tensorflow and metal. Hope this article helps!

Link to Article

ynzhang23 avatar Mar 02 '23 06:03 ynzhang23

Hello, I ran into the same problem today and have no idea how to deal with this. It would be great if someone can provide some advise.

# code snippet

model = tf.keras.Sequential([ tf.keras.layers.Dense(16, activation='relu'), tf.keras.layers.Dense(16, activation='relu'), tf.keras.layers.Dense(1, activation='sigmoid') ])

model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), loss=tf.keras.losses.BinaryCrossentropy(), metrics=['accuracy'])

model.fit(x_train, y_train, batch_size=16, epochs=20, validation_data=(x_valid, y_valid))

same issue. I was following a Udemy course on tensorflow and Keras, almost same code and run into same issue.

oldwei7404 avatar Mar 10 '23 05:03 oldwei7404

It gives the error in the validation. I faced with the same problem when I tried to use "model.predict()". When you give the validation data as variable it throws error. (for example when you write "model.predict([[a,b]]))". But when you give an integer or float (model.predict([[17,2]])) it works. I bypassed the problem by writing "model.predict([[float(a),float(b)]])" (you can use the related type instead float). I believe that the problem is being caused by a bug or smt in TensorFlow. This solution worked for me. You can give it a shot.

SerhatDerya avatar Mar 11 '23 16:03 SerhatDerya

I followed the error msg and it was complaining about missing so. lib files, but I looked and they were there, so I added this in my bashrc, then it works.

export XLA_FLAGS=--xla_gpu_cuda_data_dir=/usr/lib/cuda

oldwei7404 avatar Mar 12 '23 02:03 oldwei7404

After doing many hit and trials .I resolved this error in my case was due to insufficient memory in machine Connecting with google collab gpu worked for me

Anchaliya75 avatar Apr 02 '23 10:04 Anchaliya75

I faced same error by running this code, can anyone help.............

import json import numpy as np from sklearn.model_selection import train_test_split import tensorflow.keras as keras import matplotlib.pyplot as plt

DATA_PATH ="D:/Untitled Folder/data_18.json"

def load_data(data_path): """Loads training dataset from json file. :param data_path (str): Path to json file containing data :return X (ndarray): Inputs :return y (ndarray): Targets """

with open(data_path, "r") as fp:
    data = json.load(fp)

X = np.array(data["mfcc"])
y = np.array(data["labels"])
return X, y

def plot_history(history): """Plots accuracy/loss for training/validation set as a function of the epochs :param history: Training history of model :return: """

fig, axs = plt.subplots(2)

# create accuracy sublpot
axs[0].plot(history.history["accuracy"], label="train accuracy")
axs[0].plot(history.history["val_accuracy"], label="test accuracy")
axs[0].set_ylabel("Accuracy")
axs[0].legend(loc="lower right")
axs[0].set_title("Accuracy eval")

# create error sublpot
axs[1].plot(history.history["loss"], label="train error")
axs[1].plot(history.history["val_loss"], label="test error")
axs[1].set_ylabel("Error")
axs[1].set_xlabel("Epoch")
axs[1].legend(loc="upper right")
axs[1].set_title("Error eval")

plt.show()

def prepare_datasets(test_size, validation_size): """Loads data and splits it into train, validation and test sets. :param test_size (float): Value in [0, 1] indicating percentage of data set to allocate to test split :param validation_size (float): Value in [0, 1] indicating percentage of train set to allocate to validation split :return X_train (ndarray): Input training set :return X_validation (ndarray): Input validation set :return X_test (ndarray): Input test set :return y_train (ndarray): Target training set :return y_validation (ndarray): Target validation set :return y_test (ndarray): Target test set """

# load data
X, y = load_data(DATA_PATH)

# create train, validation and test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size)
X_train, X_validation, y_train, y_validation = train_test_split(X_train, y_train, test_size=validation_size)

return X_train, X_validation, X_test, y_train, y_validation, y_test

def build_model(input_shape): """Generates RNN-LSTM model :param input_shape (tuple): Shape of input set :return model: RNN-LSTM model """

# build network topology

model = keras.Sequential()

# 2 LSTM layers
model. Add(keras.layers.LSTM(64, input_shape=input_shape, return_sequences=True))
model. Add(keras.layers.LSTM(64))

# dense layer
model. Add(keras.layers.Dense(64, activation='relu'))
model. Add(keras.layers.Dropout(0.3))

# output layer
model. Add(keras.layers.Dense(10, activation='softmax'))

return model

if name == "main":

# get train, validation, test splits
X_train, X_validation, X_test, y_train, y_validation, y_test = prepare_datasets(0.25, 0.2)

# create network
input_shape = (X_train.shape[1], X_train.shape[2]) # 130, 13
model = build_model(input_shape)

# compile model
optimizer = keras.optimizers.Adam(learning_rate=0.0001)
model. Compile(optimizer=optimizer,
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model. Summary()

# train model
history = model. Fit(X_train, y_train, validation_data=(X_validation, y_validation), batch_size=32, epochs=30)

# plot accuracy/error for training and validation
plot_history(history)

# evaluate model on test set
test_loss, test_acc = model.evaluate(X_test, y_test, verbose=2)
print('\nTest accuracy:', test_acc)

The error:-

InvalidArgumentError: Graph execution error:

Detected at node 'sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits' defined at (most recent call last): File "C:\Users\96475\anaconda3\lib\runpy.py", line 196, in _run_module_as_main return _run_code(code, main_globals, None, File "C:\Users\96475\anaconda3\lib\runpy.py", line 86, in _run_code exec(code, run_globals) File "C:\Users\96475\anaconda3\lib\site-packages\ipykernel_launcher.py", line 17, in app.launch_new_instance() File "C:\Users\96475\anaconda3\lib\site-packages\traitlets\config\application.py", line 992, in launch_instance app.start() File "C:\Users\96475\anaconda3\lib\site-packages\ipykernel\kernelapp.py", line 711, in start self.io_loop.start() File "C:\Users\96475\anaconda3\lib\site-packages\tornado\platform\asyncio.py", line 199, in start self.asyncio_loop.run_forever() File "C:\Users\96475\anaconda3\lib\asyncio\base_events.py", line 603, in run_forever self._run_once() File "C:\Users\96475\anaconda3\lib\asyncio\base_events.py", line 1906, in _run_once handle._run() File "C:\Users\96475\anaconda3\lib\asyncio\events.py", line 80, in _run self._context.run(self._callback, *self._args) File "C:\Users\96475\anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 510, in dispatch_queue await self.process_one() File "C:\Users\96475\anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 499, in process_one await dispatch(*args) File "C:\Users\96475\anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 406, in dispatch_shell await result File "C:\Users\96475\anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 729, in execute_request reply_content = await reply_content File "C:\Users\96475\anaconda3\lib\site-packages\ipykernel\ipkernel.py", line 411, in do_execute res = shell.run_cell( File "C:\Users\96475\anaconda3\lib\site-packages\ipykernel\zmqshell.py", line 531, in run_cell return super().run_cell(*args, **kwargs) File "C:\Users\96475\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2961, in run_cell result = self._run_cell( File "C:\Users\96475\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3016, in _run_cell result = runner(coro) File "C:\Users\96475\anaconda3\lib\site-packages\IPython\core\async_helpers.py", line 129, in pseudo_sync_runner coro.send(None) File "C:\Users\96475\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3221, in run_cell_async has_raised = await self.run_ast_nodes(code_ast.body, cell_name, File "C:\Users\96475\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3400, in run_ast_nodes if await self.run_code(code, result, async=asy): File "C:\Users\96475\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3460, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "C:\Users\96475\AppData\Local\Temp\ipykernel_2536\18341082.py", line 115, in history = model.fit(X_train, y_train, validation_data=(X_validation, y_validation), batch_size=32, epochs=30) File "C:\Users\96475\anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 64, in error_handler return fn(*args, **kwargs) File "C:\Users\96475\anaconda3\lib\site-packages\keras\engine\training.py", line 1409, in fit tmp_logs = self.train_function(iterator) File "C:\Users\96475\anaconda3\lib\site-packages\keras\engine\training.py", line 1051, in train_function return step_function(self, iterator) File "C:\Users\96475\anaconda3\lib\site-packages\keras\engine\training.py", line 1040, in step_function outputs = model.distribute_strategy.run(run_step, args=(data,)) File "C:\Users\96475\anaconda3\lib\site-packages\keras\engine\training.py", line 1030, in run_step outputs = model.train_step(data) File "C:\Users\96475\anaconda3\lib\site-packages\keras\engine\training.py", line 890, in train_step loss = self.compute_loss(x, y, y_pred, sample_weight) File "C:\Users\96475\anaconda3\lib\site-packages\keras\engine\training.py", line 948, in compute_loss return self.compiled_loss( File "C:\Users\96475\anaconda3\lib\site-packages\keras\engine\compile_utils.py", line 201, in call loss_value = loss_obj(y_t, y_p, sample_weight=sw) File "C:\Users\96475\anaconda3\lib\site-packages\keras\losses.py", line 139, in call losses = call_fn(y_true, y_pred) File "C:\Users\96475\anaconda3\lib\site-packages\keras\losses.py", line 243, in call return ag_fn(y_true, y_pred, **self._fn_kwargs) File "C:\Users\96475\anaconda3\lib\site-packages\keras\losses.py", line 1860, in sparse_categorical_crossentropy return backend.sparse_categorical_crossentropy( File "C:\Users\96475\anaconda3\lib\site-packages\keras\backend.py", line 5238, in sparse_categorical_crossentropy res = tf.nn.sparse_softmax_cross_entropy_with_logits( Node: 'sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits' Received a label value of 108 which is outside the valid range of [0, 10). Label values: 66 54 19 42 86 9 34 106 43 53 52 33 72 65 2 108 104 53 43 1 80 95 27 39 81 47 70 79 108 6 45 77 [[{{node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits}}]] [Op:__inference_train_function_11127]

Sanaaisam avatar Apr 28 '23 12:04 Sanaaisam

I'm having the same error. I would appreciate your help.

train_data = tf.data.Dataset.from_generator(lambda: data_adapter, output_signature=(tf.TensorSpec(shape=(sig_len, n_sig), dtype=tf.float64), tf.TensorSpec(shape=(), dtype=tf.string)))
.take(2000)
.batch(32) val_data = tf.data.Dataset.from_generator(lambda: data_adapter, output_signature=(tf.TensorSpec(shape=(sig_len, n_sig), dtype=tf.float64), tf.TensorSpec(shape=(), dtype=tf.string)))
.skip(2000)
.batch(32) model.fit_generator(train_data, epochs=10, validation_data=val_data) UnimplementedError: Graph execution error:

Detected at node 'binary_crossentropy/Cast' 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 1, in <cell line: 1> model.fit_generator(train_data, epochs=10, validation_data=val_data) File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 2636, in fit_generator return self.fit( File "/usr/local/lib/python3.10/dist-packages/keras/utils/traceback_utils.py", line 65, in error_handler return fn(*args, **kwargs) File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1685, in fit tmp_logs = self.train_function(iterator) File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1284, in train_function return step_function(self, iterator) File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1268, in step_function outputs = model.distribute_strategy.run(run_step, args=(data,)) File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1249, in run_step outputs = model.train_step(data) File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1051, in train_step loss = self.compute_loss(x, y, y_pred, sample_weight) File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1109, in compute_loss return self.compiled_loss( File "/usr/local/lib/python3.10/dist-packages/keras/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/losses.py", line 142, in call losses = call_fn(y_true, y_pred) File "/usr/local/lib/python3.10/dist-packages/keras/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/losses.py", line 2145, in binary_crossentropy y_true = tf.cast(y_true, y_pred.dtype) Node: 'binary_crossentropy/Cast' 2 root error(s) found. (0) UNIMPLEMENTED: Cast string to float is not supported [[{{node binary_crossentropy/Cast}}]] (1) CANCELLED: Function was cancelled before it was started 0 successful operations. 0 derived errors ignored. [Op:__inference_train_function_1989]

yusagunes avatar May 24 '23 22:05 yusagunes

Try to check if any file is not an image file, this error is occurring just because of containing other files in dataset folder, this change will surely solve this problem...

AvijitChowdhury avatar May 28 '23 18:05 AvijitChowdhury