keras-team / tf-keras

The TensorFlow-specific implementation of the Keras API, which was the default Keras from 2019 to 2023.
Apache License 2.0
58 stars 28 forks source link

InvalidArgumentError: Graph execution error: #66

Open David-hosting opened 1 year ago

David-hosting commented 1 year ago

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]
tilakrayal commented 1 year ago

@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

google-ml-butler[bot] commented 1 year ago

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.

damlaYasarr commented 1 year ago

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

AakarSharmaHME commented 1 year ago

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

DanielLopez651 commented 1 year ago

bueno no se paso gracias

Indranil-359 commented 1 year ago

facing same error

xoumyax commented 1 year ago

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

JayanthBobbili commented 1 year ago

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

573-pankaj commented 1 year ago

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.runcode(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 commented 1 year ago

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........

Abdullah-Fiaz commented 1 year ago

@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

573-pankaj commented 1 year ago

Okay will share!

AnujLahoty commented 1 year ago

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.

bindu56 commented 1 year ago

same problem im facing is there any solution to find out

husainridwan commented 1 year ago

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]

Karthigai44 commented 1 year ago

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

jsalinasvela commented 1 year ago

@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
Karthigai44 commented 1 year ago

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: @.***>

KONTACT406 commented 1 year ago

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.runcode(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]

SarfarajRahmn commented 1 year ago

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

iffishells commented 1 year ago

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

NicoaraBogdan commented 1 year ago

any workarounds?

ynzhang23 commented 1 year ago

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

Link to Article

oldwei7404 commented 1 year ago

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.

SerhatDerya commented 1 year ago

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.

oldwei7404 commented 1 year ago

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

Anchaliya75 commented 1 year ago

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

Sanaaisam commented 1 year ago

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.runcode(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]

yusagunes commented 1 year ago

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.runcode(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]

AvijitChowdhury commented 1 year ago

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...

samsyano commented 1 year ago

In my case, I used the wrong input shape. Try check if you are using the correct input dim

yusagunes commented 1 year ago

Thank you. I solved the problem. I wrote codes compatible with TF 2.0.

claracidras commented 1 year ago

I'm having the same problem with my project. Anyone could help please, the deadline is soon :):

from tensorflow.keras.layers import Input, Embedding, LSTM, Dense from tensorflow.keras.models import Model from tensorflow.keras.losses import SparseCategoricalCrossentropy from tensorflow.keras.optimizers import Adam

def get_model(input_shape, vocab_size, embedding_dim, rnn_units): input = Input(shape=input_shape) embedding = Embedding(vocab_size, embedding_dim, input_length=max_sequence_len)(input) lstm = LSTM(rnn_units)(embedding) output = Dense(vocab_size, activation='softmax')(lstm) model = Model(input,output) model.compile(loss = SparseCategoricalCrossentropy(), optimizer = Adam(), metrics=['accuracy']) model.summary() return model

from tensorflow.keras.callbacks import EarlyStopping callback = EarlyStopping(monitor='val_accuracy', mode = 'max', patience=5, restore_best_weights=True)

vocab_size = len(tokenizer.word_index) #total num of unique words embedding_dim = 16 rnn_units = 55 batch_size = 32 epochs = 10 model = get_model(max_sequence_len, vocab_size, embedding_dim, rnn_units)

history = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_data = (x_val, y_val), callbacks = [callback])

ERROR: Epoch 1/10

InvalidArgumentError Traceback (most recent call last) in <cell line: 1>() ----> 1 history = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_data=(x_val, y_val))

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) 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.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.runcode(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> history = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_data=(x_val, y_val)) 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 2078, in sparse_categorical_crossentropy return backend.sparse_categorical_crossentropy( File "/usr/local/lib/python3.10/dist-packages/keras/backend.py", line 5660, 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 [32,271573] and labels shape [960] [[{{node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits}}]] [Op:__inference_train_function_18376]

Sagar9921 commented 1 year ago

I was working on lane detection along with object detection n i am also facing same problem Graph execution error:

Can anyone help me to solve thi problem?

in set_make_frame(self, mf) in get_frame(self, t) [/usr/local/lib/python3.10/dist-packages/tensorflow/python/eager/execute.py](https://localhost:8080/#) 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 'sequential_1/batch_normalization_1/FusedBatchNormV3' 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 81, in vid_clip = vid_input.fl_image(lambda x: road_lanes(x, lanes)) File "/usr/local/lib/python3.10/dist-packages/moviepy/video/VideoClip.py", line 490, in fl_image return self.fl(lambda gf, t: image_func(gf(t)), apply_to) File "/usr/local/lib/python3.10/dist-packages/moviepy/Clip.py", line 136, in fl newclip = self.set_make_frame(lambda t: fun(self.get_frame, t)) File "", line 2, in set_make_frame File "/usr/local/lib/python3.10/dist-packages/moviepy/decorators.py", line 14, in outplace f(newclip, *a, **k) File "/usr/local/lib/python3.10/dist-packages/moviepy/video/VideoClip.py", line 644, in set_make_frame self.size = self.get_frame(0).shape[:2][::-1] File "", line 2, in get_frame File "/usr/local/lib/python3.10/dist-packages/moviepy/decorators.py", line 89, in wrapper return f(*new_a, **new_kw) File "/usr/local/lib/python3.10/dist-packages/moviepy/Clip.py", line 93, in get_frame return self.make_frame(t) File "/usr/local/lib/python3.10/dist-packages/moviepy/Clip.py", line 136, in newclip = self.set_make_frame(lambda t: fun(self.get_frame, t)) File "/usr/local/lib/python3.10/dist-packages/moviepy/video/VideoClip.py", line 490, in return self.fl(lambda gf, t: image_func(gf(t)), apply_to) File "", line 81, in vid_clip = vid_input.fl_image(lambda x: road_lanes(x, lanes)) File "", line 49, in road_lanes prediction = model.predict(blurred_img)[0] * 255 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 2382, in predict tmp_batch_outputs = self.predict_function(iterator) File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 2169, in predict_function return step_function(self, iterator) File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 2155, 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 2143, in run_step outputs = model.predict_step(data) File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 2111, in predict_step return self(x, training=False) 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 558, in __call__ return super().__call__(*args, **kwargs) 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/base_layer.py", line 1145, in __call__ outputs = call_fn(inputs, *args, **kwargs) File "/usr/local/lib/python3.10/dist-packages/keras/utils/traceback_utils.py", line 96, in error_handler return fn(*args, **kwargs) File "/usr/local/lib/python3.10/dist-packages/keras/engine/sequential.py", line 412, in call return super().call(inputs, training=training, mask=mask) File "/usr/local/lib/python3.10/dist-packages/keras/engine/functional.py", line 512, in call return self._run_internal_graph(inputs, training=training, mask=mask) File "/usr/local/lib/python3.10/dist-packages/keras/engine/functional.py", line 669, in _run_internal_graph outputs = node.layer(*args, **kwargs) 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/base_layer.py", line 1145, in __call__ outputs = call_fn(inputs, *args, **kwargs) File "/usr/local/lib/python3.10/dist-packages/keras/utils/traceback_utils.py", line 96, in error_handler return fn(*args, **kwargs) File "/usr/local/lib/python3.10/dist-packages/keras/layers/normalization/batch_normalization.py", line 922, in call outputs = self._fused_batch_norm( File "/usr/local/lib/python3.10/dist-packages/keras/layers/normalization/batch_normalization.py", line 688, in _fused_batch_norm output, mean, variance = control_flow_util.smart_cond( File "/usr/local/lib/python3.10/dist-packages/keras/utils/control_flow_util.py", line 108, in smart_cond return tf.__internal__.smart_cond.smart_cond( File "/usr/local/lib/python3.10/dist-packages/keras/layers/normalization/batch_normalization.py", line 677, in _fused_batch_norm_inference return tf.compat.v1.nn.fused_batch_norm( Node: 'sequential_1/batch_normalization_1/FusedBatchNormV3' input must be 4 or 5-dimensional[1,80] [[{{node sequential_1/batch_normalization_1/FusedBatchNormV3}}]] [Op:__inference_predict_function_48193]
s2526220 commented 1 year ago

@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

hello @573-pankaj, I am stuck with the same error, can you please share your solution?

luisCT1 commented 1 year ago

Tuve el mismo problema y logré solucionarlo. No se si sea la misma solución para todos. Lo que hice fue asegurarme que la cantidad de neuronas de salida fueran la mismas cantidad de categorías de imágenes que tengo.

tf.keras.layers.Dense(2, activation='softmax')

en mi caso solo eran dos categorías.

PavanNVSS commented 1 year ago

Same Issue... must I downgrade my TF? Epoch 1/25

InvalidArgumentError Traceback (most recent call last) Cell In [25], line 9 6 nb_validation_samples = 3006 7 epochs=25 ----> 9 history=model.fit( 10 train_generator, 11 steps_per_epoch=nb_train_samples//batch_size, 12 epochs=epochs, 13 callbacks=callbacks, 14 validation_data=validation_generator, 15 validation_steps=nb_validation_samples//batch_size)

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\utils\traceback_utils.py:70, in filter_traceback..error_handler(*args, **kwargs) 67 filtered_tb = _process_traceback_frames(e.traceback) 68 # To get the full stack trace, call: 69 # tf.debugging.disable_traceback_filtering() ---> 70 raise e.with_traceback(filtered_tb) from None 71 finally: 72 del filtered_tb

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\tensorflow\python\eager\execute.py:52, 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: 55 if name is not None:

InvalidArgumentError: Graph execution error:

Detected at node 'categorical_crossentropy/softmax_cross_entropy_with_logits' defined at (most recent call last): File "C:\Users\pavan\AppData\Local\Programs\Python\Python310\Lib\runpy.py", line 196, in _run_module_as_main return _run_code(code, main_globals, None, File "C:\Users\pavan\AppData\Local\Programs\Python\Python310\Lib\runpy.py", line 86, in _run_code exec(code, run_globals) File "C:\Users\pavan\AppData\Local\Programs\Python\Python310\lib\site-packages\ipykernel_launcher.py", line 17, in app.launch_new_instance() File "C:\Users\pavan\AppData\Local\Programs\Python\Python310\lib\site-packages\traitlets\config\application.py", line 1043, in launch_instance app.start() File "C:\Users\pavan\AppData\Local\Programs\Python\Python310\lib\site-packages\ipykernel\kernelapp.py", line 712, in start self.io_loop.start() File "C:\Users\pavan\AppData\Local\Programs\Python\Python310\lib\site-packages\tornado\platform\asyncio.py", line 215, in start self.asyncio_loop.run_forever() File "C:\Users\pavan\AppData\Local\Programs\Python\Python310\Lib\asyncio\base_events.py", line 600, in run_forever self._run_once() File "C:\Users\pavan\AppData\Local\Programs\Python\Python310\Lib\asyncio\base_events.py", line 1896, in _run_once handle._run() File "C:\Users\pavan\AppData\Local\Programs\Python\Python310\Lib\asyncio\events.py", line 80, in _run self._context.run(self._callback, self._args) File "C:\Users\pavan\AppData\Local\Programs\Python\Python310\lib\site-packages\ipykernel\kernelbase.py", line 510, in dispatch_queue await self.process_one() File "C:\Users\pavan\AppData\Local\Programs\Python\Python310\lib\site-packages\ipykernel\kernelbase.py", line 499, in process_one await dispatch(args) File "C:\Users\pavan\AppData\Local\Programs\Python\Python310\lib\site-packages\ipykernel\kernelbase.py", line 406, in dispatch_shell await result File "C:\Users\pavan\AppData\Local\Programs\Python\Python310\lib\site-packages\ipykernel\kernelbase.py", line 730, in execute_request reply_content = await reply_content File "C:\Users\pavan\AppData\Local\Programs\Python\Python310\lib\site-packages\ipykernel\ipkernel.py", line 383, in do_execute res = shell.run_cell( File "C:\Users\pavan\AppData\Local\Programs\Python\Python310\lib\site-packages\ipykernel\zmqshell.py", line 528, in run_cell return super().run_cell(*args, *kwargs) File "C:\Users\pavan\AppData\Local\Programs\Python\Python310\lib\site-packages\IPython\core\interactiveshell.py", line 2885, in run_cell result = self._run_cell( File "C:\Users\pavan\AppData\Local\Programs\Python\Python310\lib\site-packages\IPython\core\interactiveshell.py", line 2940, in _run_cell return runner(coro) File "C:\Users\pavan\AppData\Local\Programs\Python\Python310\lib\site-packages\IPython\core\async_helpers.py", line 129, in _pseudo_sync_runner coro.send(None) File "C:\Users\pavan\AppData\Local\Programs\Python\Python310\lib\site-packages\IPython\core\interactiveshell.py", line 3139, in run_cell_async has_raised = await self.run_ast_nodes(code_ast.body, cell_name, File "C:\Users\pavan\AppData\Local\Programs\Python\Python310\lib\site-packages\IPython\core\interactiveshell.py", line 3318, in run_ast_nodes if await self.runcode(code, result, async=asy): File "C:\Users\pavan\AppData\Local\Programs\Python\Python310\lib\site-packages\IPython\core\interactiveshell.py", line 3378, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "C:\Users\pavan\AppData\Local\Temp\ipykernel_24944\537660998.py", line 9, in history=model.fit( File "C:\Users\pavan\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\utils\traceback_utils.py", line 65, in error_handler return fn(args, kwargs) File "C:\Users\pavan\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\training.py", line 1650, in fit tmp_logs = self.train_function(iterator) File "C:\Users\pavan\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\training.py", line 1249, in train_function return step_function(self, iterator) File "C:\Users\pavan\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\training.py", line 1233, in step_function outputs = model.distribute_strategy.run(run_step, args=(data,)) File "C:\Users\pavan\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\training.py", line 1222, in run_step outputs = model.train_step(data) File "C:\Users\pavan\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\training.py", line 1024, in train_step loss = self.compute_loss(x, y, y_pred, sample_weight) File "C:\Users\pavan\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\training.py", line 1082, in compute_loss return self.compiled_loss( File "C:\Users\pavan\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\compile_utils.py", line 265, in call loss_value = loss_obj(y_t, y_p, sample_weight=sw) File "C:\Users\pavan\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\losses.py", line 152, in call losses = call_fn(y_true, y_pred) File "C:\Users\pavan\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\losses.py", line 284, in call return ag_fn(y_true, y_pred, self._fn_kwargs) File "C:\Users\pavan\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\losses.py", line 2004, in categorical_crossentropy return backend.categorical_crossentropy( File "C:\Users\pavan\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\backend.py", line 5538, 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,5] labels_size=[32,6] [[{{node categorical_crossentropy/softmax_cross_entropy_with_logits}}]] [Op:__inference_train_function_11405]

PavanNVSS commented 1 year ago

I fixed the error , it was because of additional files ,causing the number of classes assigned in the code and number of classes present in files to mismatch.

m-abdallah98 commented 1 year ago

Upgrading TensorFlow or Keras may help

Derin-tade commented 1 year ago

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

I had this same issue, found out the problem was in the loss I used. I changed mine from sparse_categorical_crossentropy to categorical_crossentropy. So maybe just use the categorical_crossentropy instead and see if it works.

shahriyar67 commented 1 year ago

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

did u find any way?

vasupatelll commented 1 year ago

It is very simple guys just match

shape = 224 train_generator(target_size = (shape, shape)), with model input_shape(shape, shape)

harshans164 commented 1 year ago

It is very simple guys just match

shape = 224 train_generator(target_size = (shape, shape)), with model input_shape(shape, shape)

is shape =224 a predefined argument or assigned value?

ChowMeinFan commented 1 year ago

As @AnujLahoty stated before, a possible fix is using a different data type. I used an int (2000) and changed it to a float (2000.0) and that fixed this error for me.

arifanedian commented 1 year ago

It is very simple guys just match

shape = 224 train_generator(target_size = (shape, shape)), with model input_shape(shape, shape)

yes, this worked for me. Thankyou.

AliNazeri commented 11 months ago

I faced this issue on kaggle when I was using the original environment. I don't know what causes that issue but when I changed to latest one it solved the error. the tensorflow version didn't changed but problem didn't appear. it might be version of some libraries doesnt match.

MathavanSG commented 11 months ago

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]

Check the output layer its should be equal to the number of classes present in the datasets

SiddhantOjha17 commented 11 months ago

For me it was a batch size error please check your hyper parameters and then run it again!!

kafka-91 commented 9 months ago

We had the same error message (InvalidArgumentError: Graph execution error), and in our case the problem was the labels. For some reason, our labels went from 1 - 4 at some point, and changing them to 0 - 3 solved the problem. Apparently keras needs the labels to start at 0 (feel free to correct me if I'm wrong). Hope that helps some of you who encounter this issue.

MariamJBa commented 4 months ago

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