OpenCV keras model raises error "__init__() got an unexpected keyword argument 'ragged'"

Viewed 234

While predicting from model using OpenCV I am encountering this error: __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())

The traceback:

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'

I am using Keras 2.3.1 and OpenCV 4.1.2.

I also tried replacing
from keras.models import model_from_json with
from tensorflow.keras.models import model_from_json
with no improvement.

1 Answers

While importing the model it might be using tf.ragged that might not compatible with Keras. Change imports from

from keras.models import model_from_json
from keras.preprocessing import image

to

from tensorflow.keras.models import model_from_json
from tensorflow.keras.preprocessing import image
Related