How to update a model.json saved in keras 1.1.0 to latest (tensorflow 2.3.1)?

238 views Asked by At

I'm creating a pull request to https://github.com/WaqasSultani/AnomalyDetectionCVPR2018, updating https://github.com/WaqasSultani/AnomalyDetectionCVPR2018/blob/master/Test_Anomaly_Detector_public.py to python3 and the latest keras (now part of tensorflow)

I'd like to load this model https://github.com/WaqasSultani/AnomalyDetectionCVPR2018/blob/master/model.json

{
   "class_name":"Sequential",
   "keras_version":"1.1.0",
   "config":[
      {
         "class_name":"Dense",
         "config":{
            "W_constraint":null,
            "b_constraint":null,
            "name":"dense_1",
            "output_dim":512,
            "activity_regularizer":null,
            "trainable":true,
            "init":"glorot_normal",
            "bias":true,
            "input_dtype":"float32",
            "input_dim":4096,
            "b_regularizer":null,
            "W_regularizer":{
               "l2":0.0010000000474974513,
               "name":"WeightRegularizer",
               "l1":0.0
            },
            "activation":"relu",
            "batch_input_shape":[
               null,
               4096
            ]
         }
      },
      {
         "class_name":"Dropout",
         "config":{
            "p":0.6,
            "trainable":true,
            "name":"dropout_1"
         }
      },
      {
         "class_name":"Dense",
         "config":{
            "W_constraint":null,
            "b_constraint":null,
            "name":"dense_2",
            "activity_regularizer":null,
            "trainable":true,
            "init":"glorot_normal",
            "bias":true,
            "input_dim":null,
            "b_regularizer":null,
            "W_regularizer":{
               "l2":0.0010000000474974513,
               "name":"WeightRegularizer",
               "l1":0.0
            },
            "activation":"linear",
            "output_dim":32
         }
      },
      {
         "class_name":"Dropout",
         "config":{
            "p":0.6,
            "trainable":true,
            "name":"dropout_2"
         }
      },
      {
         "class_name":"Dense",
         "config":{
            "W_constraint":null,
            "b_constraint":null,
            "name":"dense_3",
            "activity_regularizer":null,
            "trainable":true,
            "init":"glorot_normal",
            "bias":true,
            "input_dim":null,
            "b_regularizer":null,
            "W_regularizer":{
               "l2":0.0010000000474974513,
               "name":"WeightRegularizer",
               "l1":0.0
            },
            "activation":"sigmoid",
            "output_dim":1
         }
      }
   ]
}

in the latest keras (tensorflow 2.3.1), but

from tensorflow.keras.models import model_from_json
model = model_from_json(open('model.json').read())

yields

Traceback (most recent call last):
  File "/home/dario/AnomalyDetectionCVPR2018/minimal_example_load.py", line 4, in <module>
    model = model_from_json(open('model.json').read())
  File "/home/dario/.local/lib/python3.6/site-packages/tensorflow/python/keras/saving/model_config.py", line 122, in model_from_json
    return deserialize(config, custom_objects=custom_objects)
  File "/home/dario/.local/lib/python3.6/site-packages/tensorflow/python/keras/layers/serialization.py", line 175, in deserialize
    printable_module_name='layer')
  File "/home/dario/.local/lib/python3.6/site-packages/tensorflow/python/keras/utils/generic_utils.py", line 358, in deserialize_keras_object
    list(custom_objects.items())))
  File "/home/dario/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/sequential.py", line 487, in from_config
    custom_objects=custom_objects)
  File "/home/dario/.local/lib/python3.6/site-packages/tensorflow/python/keras/layers/serialization.py", line 175, in deserialize
    printable_module_name='layer')
  File "/home/dario/.local/lib/python3.6/site-packages/tensorflow/python/keras/utils/generic_utils.py", line 360, in deserialize_keras_object
    return cls.from_config(cls_config)
  File "/home/dario/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py", line 697, in from_config
    return cls(**config)
TypeError: __init__() missing 1 required positional argument: 'units'

How to update it so that it is loadable by tensorflow 2.3.1?

0

There are 0 answers