我使用以下 Youtube 制作了一個代碼,youtuber 沒有遇到此代碼的問題,但我遇到了.. 似乎代碼沒有問題,但我認為它與 anaconda 或 tensorflow 或其他程式有關...用 3 個小時用 google 解決了這個問題,但找不到任何線索....
代碼是
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
--version-- keras 2.7.0 蟒蛇 3.9.7 張量流 2.7.0
如果您需要更多資訊,請告訴我。如果你解決了這個問題,我將不勝感激......
uj5u.com熱心網友回復:
錯誤很明顯,您正在將形狀影像發送(480,480,3)到模型中(224, 224, 3)。
您沒有正確更新img_input變數,在您使用的最后一步img_input = np.expand_dims(img, axis=0)會擴大img變數的維度,這仍然(480,480,3)導致模型的輸入大小錯誤。
您可以通過保持相同的變數并像這樣更新它來輕松解決這個問題:
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)
uj5u.com熱心網友回復:
當您擁有的模型經過訓練以接受與測驗不同大小的影像時,就會出現此問題......
您可以嘗試使用 256 而不是 127
img_input = (img.astype(np.float32) / 256.0)
或者你可以使用
img_input = cv2.resize(img, (114,114))
希望這可能會有所幫助。
請記住,主要問題在于您插入的影像尺寸與您訓練模型的影像尺寸之間。
您還可以檢查用于創建該模型的代碼。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/368275.html
下一篇:退出權重多久更新一次
