Search code examples
pythontensorflowopencvjupyter-notebookneural-network

ValueError: Input 0 of layer "model_1" is incompatible with the layer: expected shape=(None, 224, 224, 3), found shape=(None, 290, 290, 3)


I am trying to implement the game of Rock, paper and scissors in jupyther notebook using tensorflow with a neural network, the code I am trying to implement is this one: https://learnopencv.com/playing-rock-paper-scissors-with-ai/

When I use my webcam It works correctly, but when I use a dslr camera it doesnt work

The specific line when the code broke is here:

history = model.fit(x=augment.flow(trainX, trainY, batch_size=batchsize), validation_data=(testX, testY), 
steps_per_epoch= len(trainX) // batchsize, epochs=epochs)

The complete error is :

Epoch 1/15
7/7 [==============================] - ETA: 0s - loss: 1.0831 - accuracy: 0.6154
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_17300/1526770187.py in <module>
      4 
      5 # Start training
----> 6 history = model.fit(x=augment.flow(trainX, trainY, batch_size=batchsize), validation_data=(testX, testY), 
      7 steps_per_epoch= len(trainX) // batchsize, epochs=epochs)
      8 

C:\ProgramData\Anaconda3\lib\site-packages\keras\utils\traceback_utils.py in 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

C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py in tf__test_function(iterator)
     13                 try:
     14                     do_return = True
---> 15                     retval_ = ag_.converted_call(ag.ld(step_function), (ag.ld(self), ag_.ld(iterator)), None, fscope)
     16                 except:
     17                     do_return = False

ValueError: in user code:

    File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py", line 1557, in test_function  *
        return step_function(self, iterator)
    File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py", line 1546, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py", line 1535, in run_step  **
        outputs = model.test_step(data)
    File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py", line 1499, in test_step
        y_pred = self(x, training=False)
    File "C:\ProgramData\Anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
        raise e.with_traceback(filtered_tb) from None
    File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\input_spec.py", line 264, in assert_input_compatibility
        raise ValueError(f'Input {input_index} of layer "{layer_name}" is '

    ValueError: Input 0 of layer "model_1" is incompatible with the layer: expected shape=(None, 224, 224, 3), found shape=(None, 290, 290, 3)

THE COMPLETE CODE OF THE PROGRAM IS HERE: https://learnopencv.com/playing-rock-paper-scissors-with-ai/


Solution

  • From the error, it seems like the shape of the input images is (290, 290, 3). Resizing the images to (224, 224, 3) will solve the issue. Please add the following line before normalizing.

    #Resizing images
    images = np.resize(images,(400, 224, 224, 3))
    #Normalizing images
    images = np.array(images, dtype="float") / 255.0