【问题标题】:ValueError: Input 0 of layer "model_1" is incompatible with the layer: expected shape=(None, 224, 224, 3), found shape=(None, 290, 290, 3)ValueError: 图层“model_1”的输入 0 与图层不兼容:预期形状=(None, 224, 224, 3),发现形状=(None, 290, 290, 3)
【发布时间】:2022-06-21 18:56:52
【问题描述】:

我正在尝试使用带有神经网络的 tensorflow 在 jupyther notebook 中实现石头剪刀布游戏,我尝试实现的代码是:https://learnopencv.com/playing-rock-paper-scissors-with-ai/

当我使用我的网络摄像头时它可以正常工作,但是当我使用单反相机时它不工作

代码中断时的具体行在这里:

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

完整的错误是:

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)

程序的完整代码在这里:https://learnopencv.com/playing-rock-paper-scissors-with-ai/

【问题讨论】:

  • 不可能。该代码不会调整为 290x290,而是调整为 224x224。你是不是抄错了?
  • 你是对的,代码是 history = model.fit(x=augment.flow(trainX, trainY, batch_size=batchsize), validation_data=(testX, testY), steps_per_epoch= len(trainX) / /batchsize, epochs=epochs)

标签: python tensorflow opencv jupyter-notebook neural-network


【解决方案1】:

从错误来看,输入图像的形状似乎是(290, 290, 3)。将图像大小调整为(224, 224, 3) 将解决此问题。请在规范化之前添加以下行。

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

【讨论】:

    猜你喜欢
    • 2022-06-15
    • 2021-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 2021-06-16
    • 2022-01-25
    相关资源
    最近更新 更多