Home > Software design >  I can't figure out with an error in python
I can't figure out with an error in python

Time:11-27

I made a code with following Youtube and the youtuber didn't have trouble with this code but I had.. it seems the code has no trouble but I think it is about programs with anaconda or tensorflow or others... I've been spending 3 hours with google to solve this problem but couldn't find even a clue....

The code is

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

and the error is

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

--version-- keras 2.7.0 python 3.9.7 tensorflow 2.7.0

If you need more information, let me know. I will really appreciate it if you solve this problem....

CodePudding user response:

The error is clear, you are sending an image of shape (480,480,3) into the model when it expects (224, 224, 3).

You are not correctly updating the img_input variable, at the final step you use img_input = np.expand_dims(img, axis=0) which will expand the dims of the img variable which is still (480,480,3) resulting in the wrong input size for the model.

You can easily fix this by keeping the same variable and update it like this:

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)   

CodePudding user response:

This problem arises when the model you have is trained for accepting the different size of the image than testing one...

You can try using 256 instead of 127

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

or you can use

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

Hope this might help.

Remember, The main problem is between the dimension of the image you insert and the dimension of the image with you trained the model.

You can also check your code with which you created that model.

  • Related