다음 파이썬 코드로 훈련 된 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
추가하려면 다음을 편집하십시오.
Traceback (most recent call last):
File "/home/pi/Documents/converted_keras/keras-script.py", line 18, in <module>
model = load_model(MODEL_PATH)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/saving.py", line 492, in load_wrapper
return load_function(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/saving.py", line 584, in load_model
model = _deserialize_model(h5dict, custom_objects, compile)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/saving.py", line 274, in _deserialize_model
model = model_from_config(model_config, custom_objects=custom_objects)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/saving.py", line 627, in model_from_config
return deserialize(config, custom_objects=custom_objects)
File "/usr/local/lib/python3.7/dist-packages/keras/layers/__init__.py", line 168, in deserialize
printable_module_name='layer')
File "/usr/local/lib/python3.7/dist-packages/keras/utils/generic_utils.py", line 147, in deserialize_keras_object
list(custom_objects.items())))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/sequential.py", line 301, in from_config
custom_objects=custom_objects)
File "/usr/local/lib/python3.7/dist-packages/keras/layers/__init__.py", line 168, in deserialize
printable_module_name='layer')
File "/usr/local/lib/python3.7/dist-packages/keras/utils/generic_utils.py", line 147, in deserialize_keras_object
list(custom_objects.items())))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/sequential.py", line 301, in from_config
custom_objects=custom_objects)
File "/usr/local/lib/python3.7/dist-packages/keras/layers/__init__.py", line 168, in deserialize
printable_module_name='layer')
File "/usr/local/lib/python3.7/dist-packages/keras/utils/generic_utils.py", line 147, in deserialize_keras_object
list(custom_objects.items())))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/network.py", line 1056, in from_config
process_layer(layer_data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/network.py", line 1042, in process_layer
custom_objects=custom_objects)
File "/usr/local/lib/python3.7/dist-packages/keras/layers/__init__.py", line 168, in deserialize
printable_module_name='layer')
File "/usr/local/lib/python3.7/dist-packages/keras/utils/generic_utils.py", line 149, in deserialize_keras_object
return cls.from_config(config['config'])
File "/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py", line 1179, in from_config
return cls(**config)
File "/usr/local/lib/python3.7/dist-packages/keras/legacy/interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'ragged'
답변
그래서 나는 가르 칠 수있는 기계를 언급 한 위의 링크를 시도
했습니다. 내 보낸 모델 tensorflow.keras
은 keras
API 에서 직접 가져온 것이 아니라 API 에서 직접 나온 것 입니다. 이 두 가지가 다릅니다. 따라서로드하는 동안 keras API와 호환되지 않을 수있는 tf.ragged 텐서를 사용하고있을 수 있습니다 .
문제 해결 :
모델이 Tensorflow의 keras 고급 API로 저장되어 있으므로 keras를 직접 가져 오지 마십시오. 모든 가져 오기를 tensorflow.keras
변경 으로 변경하십시오.
from keras.preprocessing.image import img_to_array
from keras.models import load_model
이에:
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.models import load_model
문제가 해결됩니다.
편집 :
모든 가져 오기는 Keras
또는 에서 가져와야합니다 tensorflow.keras
. 동일한 API이지만 이러한 종류의 문제를 일으키는 몇 가지 차이점이 있습니다. Keras 2.3.0 은 tensorflow 이외의 백엔드를 지원하는 마지막 주요 릴리스 이므로 tensorflow
백엔드 에도 tf.keras
선호 됩니다.
이 릴리스는 TensorFlow 2.0 부터 API를 tf.keras API 와 동기화합니다 . 그러나 대부분의 TensorFlow 2.0 기능, 특히 열성적인 실행을 지원하지 않습니다. 이러한 기능이 필요한 경우 tf.keras를 사용 하십시오 . 이것은 또한 다중 백엔드 Keras의 마지막 주요 릴리스입니다. 앞으로 TensorFlow 2.0에서 Keras 코드를 tf.keras 로 전환하는 것이 좋습니다 .