Keras中出现意外的关键字参数“参差不齐”


11

尝试使用以下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

编辑添加:

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'

h5文件链接(Google驱动器)


请添加完整的错误堆栈跟踪以及出现错误的部分代码。
Vivek Mehta,

@VivekMehta我已经添加了完整的代码和错误跟踪,我想这就是您要的内容吗?不确定,抱歉。
zxsq

"/home/pi/Documents/converted_keras/keras_model.h5"这是完整的路径吗?尝试给它绝对的路径。
DuDoff

@daudnadeem是的,这是它的绝对路径。
zxsq

感谢您添加完整的代码和堆栈跟踪。似乎使用__init __(ragged ='something')调用了generic_utils中的某些内容,但不确定为什么会发生这种情况。
rajah9

Answers:


21

因此,我尝试了上面提到过可教机器的链接,因为
事实证明您导出的模型tensorflow.keras来自kerasAPI ,而不是直接来自API。这两个是不同的。因此,在加载时可能使用了与keras API不兼容的tf.ragged张量。

解决问题的方法:

当使用Tensorflow的keras高级API保存模型时,请勿直接导入keras。将所有导入tensorflow.keras

更改为Change:

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

它将解决您的问题。

编辑:
您所有的进口商品,都应该来自Kerastensorflow.keras。尽管是相同的API,但造成这些问题的地方却有所不同。也为tensorflow后端tf.keras是首选,因为Keras 2.3.0是最后一个主要版本将支持比tensorflow其他后端。

自TensorFlow 2.0起,此版本使该API与tf.keras API 同步。但是请注意,它不支持大多数TensorFlow 2.0功能,尤其是急于执行的功能。如果需要这些功能,请使用tf.keras。这也是多后端Keras的最后一个主要版本。展望未来,我们建议用户考虑在TensorFlow 2.0 中将Keras代码切换为tf.keras


这解决了我的问题。非常感谢您:)
Manthan_Admane
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.