尝试使用以下python代码运行经过训练的keras模型:
from keras.preprocessing.image import img_to_array
from keras.models import load_model
from imutils.video import VideoStream
from threading import Thread
import numpy as np
import imutils
import time
import cv2
import os
MODEL_PATH = "/home/pi/Documents/converted_keras/keras_model.h5"
print("[info] loading model..")
model = load_model(MODEL_PATH)
print("[info] starting vid stream..")
vs = VideoStream(usePiCamera=True).start()
time.sleep(2.0)
while True:
frame = vs.Read()
frame = imutils.resize(frame, width=400)
image = cv2.resize(frame, (28, 28))
image = image.astype("float") / 255.0
image = img_to_array(image)
image = np.expand_dims(image, axis=0)
(fuel, redBall, whiteBall, none) = model.predict(image)[0]
label = "none"
proba = none
if fuel > none and fuel > redBall and fuel > whiteBall:
label = "Fuel"
proba = fuel
elif redBall > none and redBall > fuel and redBall > whiteBall:
label = "Red Ball"
proba = redBall
elif whiteBall > none and whiteBall > redBall and whiteBall > fuel:
label = "white ball"
proba = whiteBall
else:
label = "none"
proba = none
label = "{}:{:.2f%}".format(label, proba * 100)
frame = cv2.putText(frame, label, (10, 25),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
cv2.imshow("Frame", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
print("[info] cleaning up..")
cv2.destroyAllWindows()
vs.stop()
当我用python3运行它时,我得到以下错误:
TypeError: __init__() got an unexpected keyword argument 'ragged'
是什么导致了这个错误,我该如何避免它?在
版本: Keras v2.3.1版 tensorflow v1.13.1版
编辑以添加:
^{pr2}$
所以我尝试了上面你提到的teachable machine
事实证明,您导出的模型是从
tensorflow.keras
而不是直接从keras
API导出的。这两者是不同的。所以在加载时可能使用衣衫褴褛可能与keras API不兼容的张量。解决您的问题:
不要直接导入keras,因为模型是用Tensorflow的keras高级api保存的。将所有导入更改为
tensorflow.keras
更改:
为此:
^{pr2}$它会解决你的问题。在
相关问题 更多 >
编程相关推荐