在使用OpenCV从模型进行预测时,我遇到了这个错误:__init__() got an unexpected keyword argument 'ragged'
。
import os
import cv2
import numpy as np
from keras.models import model_from_json
from keras.preprocessing import image
cap = cv2.VideoCapture(0)
model = model_from_json(open("path of json model", "r").read())
回溯:
TypeError Traceback (most recent call last)
<ipython-input-1-f46b41089474> in <module>
6
7 cap = cv2.VideoCapture(0)
----> 8 model = model_from_json(open("/Users/akashmane/Desktop/wardrobe/wardrobe1.json", "r").read())
9 #load weights
10 model.load_weights('/Users/akashmane/Desktop/wardrobe/wardrobe1.h5')
~/anaconda3/lib/python3.7/site-packages/keras/engine/saving.py in model_from_json(json_string, custom_objects)
662 config = json.loads(json_string)
663 from ..layers import deserialize
--> 664 return deserialize(config, custom_objects=custom_objects)
665
666
~/anaconda3/lib/python3.7/site-packages/keras/layers/__init__.py in deserialize(config, custom_objects)
166 module_objects=globs,
167 custom_objects=custom_objects,
--> 168 printable_module_name='layer')
~/anaconda3/lib/python3.7/site-packages/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
145 config['config'],
146 custom_objects=dict(list(_GLOBAL_CUSTOM_OBJECTS.items()) +
--> 147 list(custom_objects.items())))
148 with CustomObjectScope(custom_objects):
149 return cls.from_config(config['config'])
~/anaconda3/lib/python3.7/site-packages/keras/engine/sequential.py in from_config(cls, config, custom_objects)
299 for conf in layer_configs:
300 layer = layer_module.deserialize(conf,
--> 301 custom_objects=custom_objects)
302 model.add(layer)
303 if not model.inputs and build_input_shape:
~/anaconda3/lib/python3.7/site-packages/keras/layers/__init__.py in deserialize(config, custom_objects)
166 module_objects=globs,
167 custom_objects=custom_objects,
--> 168 printable_module_name='layer')
~/anaconda3/lib/python3.7/site-packages/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
147 list(custom_objects.items())))
148 with CustomObjectScope(custom_objects):
--> 149 return cls.from_config(config['config'])
150 else:
151 # Then `cls` may be a function returning a class.
~/anaconda3/lib/python3.7/site-packages/keras/engine/base_layer.py in from_config(cls, config)
1177 A layer instance.
1178 """
-> 1179 return cls(**config)
1180
1181 def count_params(self):
~/anaconda3/lib/python3.7/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name + '` call to the ' +
90 'Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper
TypeError: __init__() got an unexpected keyword argument 'ragged'
我使用的是Keras 2.3.1和OpenCV 4.1.2。
我也试着替换掉
from keras.models import model_from_json
与
from tensorflow.keras.models import model_from_json
没有任何改进。
发布于 2021-02-02 14:51:12
导入模型时,它可能使用的tf.ragged可能与Keras不兼容。更改导入自
from keras.models import model_from_json
from keras.preprocessing import image
至
from tensorflow.keras.models import model_from_json
from tensorflow.keras.preprocessing import image
https://stackoverflow.com/questions/64789085
复制相似问题