how can reslove : InvalidArgumentError: Graph execution error?

NAJI Abdessamad

Hello guys i am a biggner at computer vision and classification, i am trying to train a model using cnn method with tensorflow and keras, but i keep getting the error bellow this code , could anyone help me or give me at least a peace of advice?

model = keras.models.Sequential([
    keras.layers.Conv2D(filters=16, kernel_size=(3,3), activation='relu',input_shape=(IMG_HEIGHT,IMG_WIDTH,channels)),
    keras.layers.Conv2D(filters=32, kernel_size=(3,3), activation='relu'),
    keras.layers.MaxPool2D(pool_size=(2,2)),
    keras.layers.BatchNormalization(axis=-1),

    keras.layers.Conv2D(filters=64, kernel_size=(3,3), activation='relu'),
    keras.layers.Conv2D(filters=128, kernel_size=(3,3), activation='relu'),
    keras.layers.MaxPool2D(pool_size=(2,2)),
    keras.layers.BatchNormalization(axis=-1),

    keras.layers.Flatten(),
    keras.layers.Dense(512,activation='relu'),
    keras.layers.BatchNormalization() ,
    keras.layers.Dropout(rate=0.5),

    keras.layers.Dense(3,activation='softmax')

])

learning_rate = 0.001
    epochs=30
    opt= Adam(learning_rate=learning_rate , decay=learning_rate/(epochs*0.5))
    model.compile(loss='sparse_categorical_crossentropy',optimizer=opt,metrics=['accuracy'])


aug = ImageDataGenerator(
          rotation_range=10,
          zoom_range=0.15,
          width_shift_range=0.1,
          height_shift_range=0.1,
          shear_range=0.15,
          horizontal_flip= False,
          vertical_flip= False,
          fill_mode="nearest"
          )
          
    
    history = model.fit(aug.flow(X_train, y_train,batch_size=32), epochs=epochs,validation_data=(X_val,y_val) )

InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-15-15df12cd6846> in <module>()
     11 
     12 
---> 13 history = model.fit(aug.flow(X_train, y_train,batch_size=32), epochs=epochs,validation_data=(X_val,y_val) )

1 frames
/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)
AloneTogether

You just have to make sure your labels are zero-based starting from 0 to 2, since your output layer has 3 nodes and a softmax activation function and you are using sparse_categorical_crossentropy. Here is a working example:

import tensorflow as tf

model = tf.keras.Sequential([
    tf.keras.layers.Conv2D(filters=16, kernel_size=(3,3), activation='relu',input_shape=(256, 256, 3)),
    tf.keras.layers.Conv2D(filters=32, kernel_size=(3,3), activation='relu'),
    tf.keras.layers.MaxPool2D(pool_size=(2,2)),
    tf.keras.layers.BatchNormalization(axis=-1),

    tf.keras.layers.Conv2D(filters=64, kernel_size=(3,3), activation='relu'),
    tf.keras.layers.Conv2D(filters=128, kernel_size=(3,3), activation='relu'),
    tf.keras.layers.MaxPool2D(pool_size=(2,2)),
    tf.keras.layers.BatchNormalization(axis=-1),

    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(512,activation='relu'),
    tf.keras.layers.BatchNormalization() ,
    tf.keras.layers.Dropout(rate=0.5),

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

])

learning_rate = 0.001
epochs=2
opt= tf.keras.optimizers.Adam(learning_rate=learning_rate , decay=learning_rate/(epochs*0.5))
model.compile(loss='sparse_categorical_crossentropy',optimizer=opt,metrics=['accuracy'])


aug = tf.keras.preprocessing.image.ImageDataGenerator(
          rotation_range=10,
          zoom_range=0.15,
          width_shift_range=0.1,
          height_shift_range=0.1,
          shear_range=0.15,
          horizontal_flip= False,
          vertical_flip= False,
          fill_mode="nearest"
          )
          

X_train = tf.random.normal((50, 256, 256, 3))
y_train = tf.random.uniform((50, ), maxval=3, dtype=tf.int32)
history = model.fit(aug.flow(X_train, y_train, batch_size=2), epochs=epochs)

Use the dummy data as an orientation for your real data.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How reslove pointer to array from pointer to single object

Why univocity's CsvParser throwing error when use LocalDate as the datatype for POJO and How to reslove it?

BFS Graph loop execution

How can I reslove The method 'toMap' was called on null?

How do I view a live execution of a dask graph?

How to handle consumer execution error in SQS + Lambda?

How to convert model in eager execution to static graph and save in .pb file?

How to fix "Execution Error 429 : ActiveX can't create object" error in VBA

How to fix the error with the execution of the command for the bot?

How to handle error in a for loop (python) and continue execution

Execution Timed Out (12000 ms) : How can I pass this error

How to avoid execution concatMap after throw error?

InvalidArgumentError:Error at the time of fit model

How to solve Execution Error in hive?

error : can not reslove symbol 'R' whenever I'm trying to add appcompat-v7:22.0.0 dependency

How to reslove the appium crash?

How to reslove the duplicate name error with swagger

How can I fix 'InvalidArgumentError' : Placeholder problem

How can I solve this error in Printing Nodes and Edges Boost Graph Library?

How to catch execution error in XMLService getAttribute method?

How can I fix the web-dev-server execution error?

How to solve the sql execution error in snowsql client?

How to continue the execution even if error happens

How can I force an error in code without stoping the execution in quarto?

How to solve Graph execution error during my model training?

InvalidArgumentError: Graph execution error - Fruit detection

InvalidArgumentError: Graph execution error: TensorFlow pose estimation using OpenCV

In need of a Labelencoder code to fix the InvalidArgumentError: Graph execution error when Using deeplabv3+ for a 4 class semantic segmentation

How to reslove permission denied while building docker compose