Mencoba menjalankan model keras terlatih dengan kode python berikut:
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()
Ketika saya menjalankannya dengan python3, saya mendapatkan kesalahan berikut:
TypeError: __init__() got an unexpected keyword argument 'ragged'
Apa yang menyebabkan kesalahan, dan bagaimana cara mengatasinya?
Versi: Keras v2.3.1 tensorflow v1.13.1
Edit untuk ditambahkan:
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'
python
tensorflow
keras
zxsq
sumber
sumber
"/home/pi/Documents/converted_keras/keras_model.h5"
apakah ini jalan penuh? Coba berikan jalan absolut.Jawaban:
Jadi saya mencoba tautan di atas yang Anda sebutkan mesin yang bisa diajar.
Ternyata model yang Anda ekspor berasal
tensorflow.keras
dan tidak langsung darikeras
API. Keduanya berbeda. Jadi saat memuat mungkin menggunakan tensor tf.ragged yang mungkin tidak kompatibel dengan API keras.Soulution untuk masalah Anda:
Jangan mengimpor keras secara langsung karena model Anda disimpan dengan api tingkat tinggi keras Tensorflow. Ubah semua impor Anda ke
tensorflow.keras
Ubah:
untuk ini:
Itu akan menyelesaikan masalah Anda.
EDIT:
Semua impor Anda, harus dari
Keras
atautensorflow.keras
. Meskipun API yang sama beberapa hal berbeda yang menciptakan masalah semacam ini. Juga untuktensorflow
backendtf.keras
lebih disukai, karena Keras 2.3.0 adalah rilis utama terakhir yang akan mendukung backend selain tensorflow.sumber