1

I'm trying to classify product images using Keras. Training Convolution Network, I got an error at model.fit() when I run the program. I'm using Colab Notebook. Keras Version is 2.8.0. Here is my code.

train_data = train_data_generator.flow_from_directory(
    TRAIN_DATA_DIR,
    batch_size=BATCH_SIZE,
    target_size=IMAGE_SIZE,
    class_mode='categorical',
    shuffle=True
)

test_data = test_data_generator.flow_from_directory(
    TEST_DATA_DIR,
    batch_size=BATCH_SIZE,
    target_size=IMAGE_SIZE,
    class_mode='categorical'
)
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics='accuracy')
checkpoint_cb = keras.callbacks.ModelCheckpoint('best-cnn-model.h5', save_best_only=True)
early_stopping_cb = keras.callbacks.EarlyStopping(patience=2, restore_best_weights=True)
history = model.fit(train_data, epochs=20, validation_data=test_data, callbacks=[checkpoint_cb, early_stopping_cb])

I got an error like this:

InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-10-4b5baabcf59c> in <module>()
      3 checkpoint_cb = keras.callbacks.ModelCheckpoint('best-cnn-model.h5', save_best_only=True)
      4 early_stopping_cb = keras.callbacks.EarlyStopping(patience=2, restore_best_weights=True)
----> 5 history = model.fit_generator(train_data, epochs=20, validation_data=test_data, callbacks=[checkpoint_cb, early_stopping_cb])
...
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     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:

Detected at node 'sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits' defined at (most recent call last):
    File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
      "__main__", mod_spec)
...

Where should I change my code? Thank for your favor.

하현호
  • 13
  • 4
  • Hi ! Can you try again with categorical_crossentropy and model.fit_generator ? model.compile(optimizer='adam', loss='categorical_crossentropy', metrics='accuracy') a –  Feb 18 '22 at 03:50
  • Thank you for your comment. But I am getting the same error applying your advise. – 하현호 Feb 18 '22 at 05:30
  • Attaching issues similar stack trace as possible scenarios for reference. https://stackoverflow.com/a/71243498/11530462 https://stackoverflow.com/questions/71079934/training-stops-after-a-while https://stackoverflow.com/questions/71262413/error-when-fit-the-model-with-data-from-imagedatagenerator-and-tf-data-dataset –  Mar 08 '22 at 11:05

0 Answers0