Keras 中意外的关键字参数“参差不齐" [英] Unexpected keyword argument 'ragged' in Keras

查看:34
本文介绍了Keras 中意外的关键字参数“参差不齐"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用以下 python 代码运行经过训练的 keras 模型:

Trying to run a trained keras model with the following python code:

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__() 得到一个意外的关键字参数 'ragged'

When I run it with python3, I get the following error: TypeError: __init__() got an unexpected keyword argument 'ragged'

导致错误的原因是什么,我该如何解决?

What's causing the error, and how do I get around it?

版本:Keras v2.3.1张量流 v1.13.1

Versions: 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 文件链接(谷歌驱动器)

推荐答案

所以我尝试了上面你提到的链接 teachable machine
事实证明,您导出的模型来自 tensorflow.keras 而不是直接来自 keras API.这两个是不同的.因此,在加载它时可能会使用 tf.ragged 张量,这些张量可能与 keras API 不兼容.

您的问题的解决方案:

不要直接导入 keras,因为您的模型是使用 Tensorflow 的 keras 高级 api 保存的.将所有导入更改为 tensorflow.keras

变化:

So I tried link above which you have mentioned teachable machine
As it turns out model you have exported is from tensorflow.keras and not directly from keras API. These two are different. So while loading it might be using tf.ragged tensors that might not be compatible with keras API.

Soulution to your issue:

Don't import keras directly as your model is saved with Tensorflow's keras high level api. Change all your imports to 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 以外的后端.

EDIT :
All of your imports, either should be from Keras or tensorflow.keras. Although being same API few things are different which creates these kind of issues. Also for tensorflow backend tf.keras is preferred, because Keras 2.3.0 is last major release which will support backends other than tensorflow.

此版本使 API 与 tf.keras API 从 TensorFlow 2.0 开始同步.但是请注意,它不支持大多数 TensorFlow 2.0 功能,尤其是 Eager Execution.如果您需要这些功能,请使用 tf.keras.这也是多后端 Keras 的最后一个主要版本.展望未来,我们建议用户考虑将他们的 Keras 代码切换到 TensorFlow 2.0 中的tf.keras.

This release brings the API in sync with the tf.keras API as of TensorFlow 2.0. However note that it does not support most TensorFlow 2.0 features, in particular eager execution. If you need these features, use tf.keras. This is also the last major release of multi-backend Keras. Going forward, we recommend that users consider switching their Keras code to tf.keras in TensorFlow 2.0.

这篇关于Keras 中意外的关键字参数“参差不齐"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆