Đối số từ khóa bất ngờ 'rách rưới' trong Keras


11

Đang cố gắng chạy một mô hình máy ảnh được đào tạo với mã python sau:

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()

Khi tôi chạy nó với python3, tôi gặp lỗi sau: TypeError: __init__() got an unexpected keyword argument 'ragged'

Điều gì gây ra lỗi, và làm thế nào để tôi khắc phục nó?

Các phiên bản: Kera v2.3.1 tenorflow v1.13.1

Chỉnh sửa để thêm:

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'

liên kết tệp h5 (ổ google)


Vui lòng thêm theo dõi ngăn xếp lỗi đầy đủ của bạn, cùng với một phần mã nơi bạn đang gặp lỗi.
Vivek Mehta

@VivekMehta Tôi đã thêm mã đầy đủ và theo dõi lỗi, tôi nghĩ đây là những gì bạn yêu cầu? Không chắc chắn, xin lỗi.
zxsq

"/home/pi/Documents/converted_keras/keras_model.h5"Đây có phải là con đường đầy đủ? Hãy thử cho nó con đường tuyệt đối.
DuDoff

@daudnadeem Vâng, đó là con đường tuyệt đối với nó.
zxsq

Cảm ơn đã thêm mã đầy đủ và theo dõi ngăn xếp. Có vẻ như một cái gì đó trong generic_utils đang được gọi với một __init __ (ragged = 'Something') nhưng không chắc tại sao điều đó sẽ xảy ra.
rajah9

Câu trả lời:


21

Vì vậy, tôi đã thử liên kết ở trên mà bạn đã đề cập đến máy có thể dạy được
vì hóa ra mô hình bạn đã xuất là từ tensorflow.kerasvà không trực tiếp từ kerasAPI. Hai cái này khác nhau. Vì vậy, trong khi tải, có thể sử dụng các thang đo tf.ragged có thể không tương thích với API của máy ảnh.

Lưu ý đến vấn đề của bạn:

Không nhập trực tiếp máy ảnh vì mô hình của bạn được lưu với api cấp độ cao của máy kéo. Thay đổi tất cả nhập khẩu của bạn thành tensorflow.keras

Thay đổi:

from keras.preprocessing.image import img_to_array
from keras.models import load_model

đến đây:

from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.models import load_model

Nó sẽ giải quyết vấn đề của bạn.

EDIT:
Tất cả hàng nhập khẩu của bạn, nên từ Kerashoặc tensorflow.keras. Mặc dù là cùng một API nhưng có một số thứ khác nhau tạo ra các loại vấn đề này. Ngoài ra, đối với tensorflowphụ trợ tf.kerasđược ưu tiên hơn, bởi vì Keras 2.3.0 là phiên bản chính cuối cùng sẽ hỗ trợ các phụ trợ khác ngoài dòng chảy.

Bản phát hành này mang API đồng bộ hóa với API tf.keras kể từ TensorFlow 2.0. Tuy nhiên, lưu ý rằng nó không hỗ trợ hầu hết các tính năng của TensorFlow 2.0, đặc biệt là thực thi háo hức. Nếu bạn cần những tính năng này, hãy sử dụng tf.keras . Đây cũng là phiên bản chính cuối cùng của Keras đa phụ trợ. Trong tương lai, chúng tôi khuyên người dùng nên cân nhắc chuyển đổi mã Keras của họ sang tf.keras trong TensorFlow 2.0.


Điều này đã khắc phục vấn đề của tôi. Cảm ơn bạn rất nhiều :)
Manthan_Admane
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.