【问题标题】:I can't figure out with an error in python我无法弄清楚python中的错误
【发布时间】:2022-01-04 14:01:36
【问题描述】:

我使用以下 Youtube 编写了一个代码,并且 youtuber 没有遇到此代码的问题,但我遇到了.. 代码似乎没有问题,但我认为这是关于带有 anaconda 或 tensorflow 或其他程序的程序...... 我已经在 google 上花了 3 个小时来解决这个问题,但甚至找不到任何线索....

代码是

import tensorflow.keras
import numpy as np
import cv2

model = tensorflow.keras.models.load_model('keras_model.h5')  

cap = cv2.VideoCapture(0) 

while cap.isOpened():       
    ret, img = cap.read()   

    if not ret:
        break

    img = cv2.flip(img, 1)  

    h, w, c = img.shape 
    img = img[:, 100:100+h] 
    img_input = cv2.resize(img, (224,224)) 
    img_input = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)   
    img_input = (img.astype(np.float32) / 127.0) - 1.0  
    img_input = np.expand_dims(img, axis=0)   

    prediction = model.predict(img_input)   
    print(prediction)
    
    cv2.imshow('result', img)   
    if cv2.waitKey(1) == ord('q'):  
        break

错误是

Traceback (most recent call last):
  File "d:\Python work\Rock\main.py", line 24, in <module>
    prediction = model.predict(img_input)
  File "d:\anaconda3\envs\test\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler   
    raise e.with_traceback(filtered_tb) from None
  File "d:\anaconda3\envs\test\lib\site-packages\tensorflow\python\framework\func_graph.py", line 1129, in autograph_handler
    raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:

    File "d:\anaconda3\envs\test\lib\site-packages\keras\engine\training.py", line 1621, in predict_function  
*
        return step_function(self, iterator)
    File "d:\anaconda3\envs\test\lib\site-packages\keras\engine\training.py", line 1611, in step_function  ** 
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "d:\anaconda3\envs\test\lib\site-packages\keras\engine\training.py", line 1604, in run_step  **      
        outputs = model.predict_step(data)
    File "d:\anaconda3\envs\test\lib\site-packages\keras\engine\training.py", line 1572, in predict_step      
        return self(x, training=False)
    File "d:\anaconda3\envs\test\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler 
        raise e.with_traceback(filtered_tb) from None
    File "d:\anaconda3\envs\test\lib\site-packages\keras\engine\input_spec.py", line 263, in assert_input_compatibility
        raise ValueError(f'Input {input_index} of layer "{layer_name}" is '

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

[ WARN:1] global D:\a\opencv-python\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (438) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback

--版本-- keras 2.7.0 蟒蛇 3.9.7 张量流 2.7.0

如果您需要更多信息,请告诉我。 如果你能解决这个问题,我将不胜感激......

【问题讨论】:

    标签: python tensorflow anaconda


    【解决方案1】:

    错误很明显,当模型需要 (224, 224, 3) 时,您将形状为 (480,480,3) 的图像发送到模型中。

    您没有正确更新 img_input 变量,在最后一步您使用 img_input = np.expand_dims(img, axis=0) 这将扩展仍为 (480,480,3)img 变量的尺寸,从而导致模型的输入大小错误。

    您可以通过保持相同的变量并像这样更新它来轻松解决此问题:

    img = img[:, 100:100+h] 
    img = cv2.resize(img, (224,224)) 
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)   
    img = (img.astype(np.float32) / 127.0) - 1.0  
    img = np.expand_dims(img, axis=0)  
    
    prediction = model.predict(img)   
    

    【讨论】:

    • 非常感谢!!你们两个都是我的英雄!!!一旦我改为“img”,它就可以工作。你们真的有一颗温暖的心!我现在可以睡个好觉了:>
    • 很高兴为您提供帮助!您可以将答案标记为已接受:)
    【解决方案2】:

    当您拥有的模型经过训练以接受与测试不同尺寸的图像时,就会出现此问题...

    您可以尝试使用 256 而不是 127

    img_input = (img.astype(np.float32) / 256.0) 
    

    或者你可以使用

    img_input = cv2.resize(img, (114,114)) 
    

    希望这可能会有所帮助。

    记住, 主要问题在于您插入的图像尺寸与您训练模型的图像尺寸之间。

    您还可以检查用于创建该模型的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多