Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.2.2 network not compatible on 2.0.2 #6382

Closed
chivalry123 opened this issue Apr 24, 2017 · 19 comments
Closed

1.2.2 network not compatible on 2.0.2 #6382

chivalry123 opened this issue Apr 24, 2017 · 19 comments

Comments

@chivalry123
Copy link

Hi, I have a network generated with 1.2.2 and I could load it successfully with

import keras
keras.models.load_model("32pairs_duration_12_predictiongap_1_keras")

However, when I upgrade to 2.0.2 and perform the same code. I got the issue:

/home/myname/anaconda2/lib/python2.7/site-packages/keras/engine/topology.py:1206: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(batch_input_shape=[None, 512..., kernel_constraint=None, name="dense_1", bias_regularizer=None, bias_constraint=None, activity_regularizer={u'l2': 0...., trainable=True, kernel_initializer="glorot_uniform", kernel_regularizer={u'l2': 0...., input_dtype="float32", units=240, input_dim=512, use_bias=True, activation="sigmoid")`
  return cls(**config)
Traceback (most recent call last):
  File "quick_test.py", line 32, in <module>
    keras.models.load_model("32pairs_duration_12_predictiongap_1_keras")
  File "/home/myname/anaconda2/lib/python2.7/site-packages/keras/models.py", line 232, in load_model
    model = model_from_config(model_config, custom_objects=custom_objects)
  File "/home/myname/anaconda2/lib/python2.7/site-packages/keras/models.py", line 293, in model_from_config
    return layer_module.deserialize(config, custom_objects=custom_objects)
  File "/home/myname/anaconda2/lib/python2.7/site-packages/keras/layers/__init__.py", line 46, in deserialize
    printable_module_name='layer')
  File "/home/myname/anaconda2/lib/python2.7/site-packages/keras/utils/generic_utils.py", line 141, in deserialize_keras_object
    return cls.from_config(config['config'])
  File "/home/myname/anaconda2/lib/python2.7/site-packages/keras/models.py", line 1189, in from_config
    layer = layer_module.deserialize(conf)
  File "/home/myname/anaconda2/lib/python2.7/site-packages/keras/layers/__init__.py", line 46, in deserialize
    printable_module_name='layer')
  File "/home/myname/anaconda2/lib/python2.7/site-packages/keras/utils/generic_utils.py", line 141, in deserialize_keras_object
    return cls.from_config(config['config'])
  File "/home/myname/anaconda2/lib/python2.7/site-packages/keras/engine/topology.py", line 1206, in from_config
    return cls(**config)
  File "/home/myname/anaconda2/lib/python2.7/site-packages/keras/legacy/interfaces.py", line 88, in wrapper
    return func(*args, **kwargs)
  File "/home/myname/anaconda2/lib/python2.7/site-packages/keras/layers/core.py", line 811, in __init__
    self.kernel_regularizer = regularizers.get(kernel_regularizer)
  File "/home/myname/anaconda2/lib/python2.7/site-packages/keras/regularizers.py", line 75, in get
    return deserialize(identifier)
  File "/home/myname/anaconda2/lib/python2.7/site-packages/keras/regularizers.py", line 68, in deserialize
    printable_module_name='regularizer')
  File "/home/myname/anaconda2/lib/python2.7/site-packages/keras/utils/generic_utils.py", line 122, in deserialize_keras_object
    raise ValueError('Improper config format: ' + str(config))
ValueError: Improper config format: {u'l2': 0.0, u'name': u'L1L2Regularizer', u'l1': 9.99999993922529e-09}

Do you know what might be happening and what should i do? Thank you. I generate my model in this way if you think it could help...


    def _generate_model(self):
        model=keras.models.Sequential()


        if self._activation=="PReLU":
            self._activation_function = (keras.layers.advanced_activations.PReLU())
        else:
            self._activation_function = (keras.layers.Activation(self._activation))


        for i in range(len(self._hidden_layer_dim)):
            layer_now=self._hidden_layer_dim[i]
            print("i now is ",i)
            if i==0:
                model.add(keras.layers.Dense(layer_now,input_dim=self._input_dimension,
                                             W_regularizer=keras.regularizers.l1(self._weight_decay),
                                             activity_regularizer=keras.regularizers.activity_l1(self._weight_decay),
                                             activation=self._activation))
            else:
                model.add(keras.layers.Dense(layer_now,W_regularizer=keras.regularizers.l1(self._weight_decay),
                                             activity_regularizer=keras.regularizers.activity_l1(self._weight_decay),
                                             activation=self._activation))

            print("dropout rate is ", self._dropout_rate)


            # if self._activation=="PReLU":
            #     model.add(keras.layers.advanced_activations.PReLU())
            # else:
            #     model.add(keras.layers.Activation(self._activation))
            model.add(keras.layers.Dropout(self._dropout_rate))
            model.add(keras.layers.BatchNormalization())

        model.add(keras.layers.Dense(self._output_dimension,W_regularizer=keras.regularizers.l1(self._weight_decay)))

        loss='mean_absolute_error'
        # loss=custom_objective

        self._optimizer=keras.optimizers.adam(lr=self._Learning_rate)

        model.compile(optimizer=keras.optimizers.adam(lr=self._Learning_rate),loss=self._Loss_function,metrics=[loss,'mean_absolute_error','mean_squared_error','mean_squared_logarithmic_error'])
        return model

@cangermueller
Copy link

I have the same problem. Does Keras2 support loading Keras1 model?

----> 1 km.load_model('model.h5')

/Users/angermue/python/keras/keras/models.py in load_model(filepath, custom_objects)
    238         raise ValueError('No model found in config file.')
    239     model_config = json.loads(model_config.decode('utf-8'))
--> 240     model = model_from_config(model_config, custom_objects=custom_objects)
    241
    242     # set weights

/Users/angermue/python/keras/keras/models.py in model_from_config(config, custom_objects)
    302                         'Maybe you meant to use '
    303                         '`Sequential.from_config(config)`?')
--> 304     return layer_module.deserialize(config, custom_objects=custom_objects)
    305
    306

/Users/angermue/python/keras/keras/layers/__init__.py in deserialize(config, custom_objects)
     52                                     module_objects=globs,
     53                                     custom_objects=custom_objects,
---> 54                                     printable_module_name='layer')

/Users/angermue/python/keras/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    138                 return cls.from_config(config['config'],
    139                                        custom_objects=dict(list(_GLOBAL_CUSTOM_OBJECTS.items()) +
--> 140                                                            list(custom_objects.items())))
    141             return cls.from_config(config['config'])
    142         else:

/Users/angermue/python/keras/keras/engine/topology.py in from_config(cls, config, custom_objects)
   2414
   2415         for layer_data in config['layers']:
-> 2416             process_layer(layer_data)
   2417
   2418         name = config.get('name')

/Users/angermue/python/keras/keras/engine/topology.py in process_layer(layer_data)
   2383             from ..layers import deserialize as deserialize_layer
   2384             layer = deserialize_layer(layer_data,
-> 2385                                       custom_objects=custom_objects)
   2386             created_layers[layer_name] = layer
   2387

/Users/angermue/python/keras/keras/layers/__init__.py in deserialize(config, custom_objects)
     52                                     module_objects=globs,
     53                                     custom_objects=custom_objects,
---> 54                                     printable_module_name='layer')

/Users/angermue/python/keras/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    139                                        custom_objects=dict(list(_GLOBAL_CUSTOM_OBJECTS.items()) +
    140                                                            list(custom_objects.items())))
--> 141             return cls.from_config(config['config'])
    142         else:
    143             # Then `cls` may be a function returning a class.

/Users/angermue/python/keras/keras/engine/topology.py in from_config(cls, config)
   1229             A layer instance.
   1230         """
-> 1231         return cls(**config)
   1232
   1233     def count_params(self):

/Users/angermue/python/keras/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
     86                 warnings.warn('Update your `' + object_name +
     87                               '` call to the Keras 2 API: ' + signature, stacklevel=2)
---> 88             return func(*args, **kwargs)
     89         wrapper._legacy_support_signature = inspect.getargspec(func)
     90         return wrapper

/Users/angermue/python/keras/keras/layers/convolutional.py in __init__(self, filters, kernel_size, strides, padding, dilation_rate, activation, use_bias, kernel_initializer, bias_initializer, kernel_regularizer, bias_regularizer, activity_regularizer, kernel_constraint, bias_constraint, **kwargs)
    328             kernel_constraint=kernel_constraint,
    329             bias_constraint=bias_constraint,
--> 330             **kwargs)
    331         self.input_spec = InputSpec(ndim=3)
    332

/Users/angermue/python/keras/keras/layers/convolutional.py in __init__(self, rank, filters, kernel_size, strides, padding, data_format, dilation_rate, activation, use_bias, kernel_initializer, bias_initializer, kernel_regularizer, bias_regularizer, activity_regularizer, kernel_constraint, bias_constraint, **kwargs)
    110         self.kernel_initializer = initializers.get(kernel_initializer)
    111         self.bias_initializer = initializers.get(bias_initializer)
--> 112         self.kernel_regularizer = regularizers.get(kernel_regularizer)
    113         self.bias_regularizer = regularizers.get(bias_regularizer)
    114         self.activity_regularizer = regularizers.get(activity_regularizer)

/Users/angermue/python/keras/keras/regularizers.py in get(identifier)
     73         return None
     74     if isinstance(identifier, dict):
---> 75         return deserialize(identifier)
     76     elif isinstance(identifier, six.string_types):
     77         config = {'class_name': str(identifier), 'config': {}}

/Users/angermue/python/keras/keras/regularizers.py in deserialize(config, custom_objects)
     66                                     module_objects=globals(),
     67                                     custom_objects=custom_objects,
---> 68                                     printable_module_name='regularizer')
     69
     70

/Users/angermue/python/keras/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    120         config = identifier
    121         if 'class_name' not in config or 'config' not in config:
--> 122             raise ValueError('Improper config format: ' + str(config))
    123         class_name = config['class_name']
    124         if custom_objects and class_name in custom_objects:

ValueError: Improper config format: {'l1': 0.0, 'name': 'WeightRegularizer', 'l2': 9.999999747378752e-05}

@tianke0711
Copy link

I have the same issues, anyone done it?

@tianke0711
Copy link

I have solved it, firstly uninstall the version of keras 2, then install the old versio by: pip3 install keras==1.2.2

@cangermueller
Copy link

This is not really a solution. Keras 2 should be backward compatible and enable loading models built with Keras 1. Otherwise one would have to retrain models, which costs time and resources.

@tianke0711
Copy link

do you have a good solution?

@maderafunk
Copy link

Same error here.

ValueError: Improper config format: {'l2': 0.0, 'l1': 0.0, 'name': 'L1L2Regularizer'}

@travischoma
Copy link

I get the error when running the followin lines in Keras 2.0.4:

config = model.get_config()
new_model = model_from_config(config)

@tianke0711
Copy link

@travischoma it may cause the issue that you code used the previous version of keras. Please check it.

@travischoma
Copy link

@tianke0711 in my case, everything was done with 2.0.4 (my first time using the framework), so I don't believe it's a problem with regards to a model/config from an older version.

@travischoma
Copy link

Fixed it. Thanks

@tianke0711
Copy link

Hi @travischoma , I am glad that you have fixed it, could you share your solution with others, please!

@hacheemaster
Copy link

@travischoma, could you please share your solution? Running into the same issue...

@delton137
Copy link

Also running into this issue! Have to load model parameters from JSON. Hate it when backwards compatibility is broken.

@stale
Copy link

stale bot commented Oct 15, 2017

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed.

@stale stale bot added the stale label Oct 15, 2017
@stale stale bot closed this as completed Nov 14, 2017
@diliplilaramani
Copy link

diliplilaramani commented May 14, 2018

Hi @travischoma , I'm facing the same issue. Can you tell us the fix?

@AndreGuerra123
Copy link

@travischoma care to share?

@BillRobust
Copy link

I solve it,by using the version 1.2.2,just like what @tianke0711 did.....
I can't find another way......
If somebody have better solution,please tell me.

@JHancox
Copy link

JHancox commented Sep 11, 2019

For those, like me, who don't have the options of reverting to 1.2.2, here is the workaround that I used, which only works if you have the model architecture saved in json format. What I found (by generating some json using 2.2.4) is that the syntax for L1/L2 regularizer has changed. So I just replaced all the instances in this format:
"activity_regularizer": {"l2": 9.999999747378752e-06, "name": "L1L2Regularizer", "l1": 0.0}
with this:
"activity_regularizer": {"class_name": "L1L2", "config": {"l2": 9.999999747378752e-06, "l1": 0.0}}
I also noticed some float values used in shape descriptions, which older versions of keras must have been more tolerant of. Just replace them with integers.

@yuchi1989
Copy link

For those, like me, who don't have the options of reverting to 1.2.2, here is the workaround that I used, which only works if you have the model architecture saved in json format. What I found (by generating some json using 2.2.4) is that the syntax for L1/L2 regularizer has changed. So I just replaced all the instances in this format:
"activity_regularizer": {"l2": 9.999999747378752e-06, "name": "L1L2Regularizer", "l1": 0.0}
with this:
"activity_regularizer": {"class_name": "L1L2", "config": {"l2": 9.999999747378752e-06, "l1": 0.0}}
I also noticed some float values used in shape descriptions, which older versions of keras must have been more tolerant of. Just replace them with integers.

This works for me. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests