question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to load binarynet model after saving

See original GitHub issue

Hi

I am using the mnist_mlp.py and after saving the model using model.save I get the following error when I try to load it Code

from keras.models import load_model

model.save('my_model.h5')

model = load_model('my_model.h5', custom_objects={'DropoutNoScale': DropoutNoScale,'BinaryDense':BinaryDense,
                                                                 'Clip':Clip})

Error

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-30-78c6bac33446> in <module>()
      2 # identical to the previous one
      3 model = load_model('my_model.h5', custom_objects={'DropoutNoScale': DropoutNoScale,'BinaryDense':BinaryDense,
----> 4                                                                  'Clip':Clip})

/opt/dl/anaconda/envs/chs_keras/lib/python3.5/site-packages/keras/models.py in load_model(filepath, custom_objects, compile)
    231             raise ValueError('No model found in config file.')
    232         model_config = json.loads(model_config.decode('utf-8'))
--> 233         model = model_from_config(model_config, custom_objects=custom_objects)
    234 
    235         # set weights

/opt/dl/anaconda/envs/chs_keras/lib/python3.5/site-packages/keras/models.py in model_from_config(config, custom_objects)
    305                         'Maybe you meant to use '
    306                         '`Sequential.from_config(config)`?')
--> 307     return layer_module.deserialize(config, custom_objects=custom_objects)
    308 
    309 

/opt/dl/anaconda/envs/chs_keras/lib/python3.5/site-packages/keras/layers/__init__.py in deserialize(config, custom_objects)
     52                                     module_objects=globs,
     53                                     custom_objects=custom_objects,
---> 54                                     printable_module_name='layer')

/opt/dl/anaconda/envs/chs_keras/lib/python3.5/site-packages/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    137                 return cls.from_config(config['config'],
    138                                        custom_objects=dict(list(_GLOBAL_CUSTOM_OBJECTS.items()) +
--> 139                                                            list(custom_objects.items())))
    140             with CustomObjectScope(custom_objects):
    141                 return cls.from_config(config['config'])

/opt/dl/anaconda/envs/chs_keras/lib/python3.5/site-packages/keras/models.py in from_config(cls, config, custom_objects)
   1207         model = cls()
   1208         for conf in config:
-> 1209             layer = layer_module.deserialize(conf, custom_objects=custom_objects)
   1210             model.add(layer)
   1211         return model

/opt/dl/anaconda/envs/chs_keras/lib/python3.5/site-packages/keras/layers/__init__.py in deserialize(config, custom_objects)
     52                                     module_objects=globs,
     53                                     custom_objects=custom_objects,
---> 54                                     printable_module_name='layer')

/opt/dl/anaconda/envs/chs_keras/lib/python3.5/site-packages/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    139                                                            list(custom_objects.items())))
    140             with CustomObjectScope(custom_objects):
--> 141                 return cls.from_config(config['config'])
    142         else:
    143             # Then `cls` may be a function returning a class.

/opt/dl/anaconda/envs/chs_keras/lib/python3.5/site-packages/keras/engine/topology.py in from_config(cls, config)
   1240             A layer instance.
   1241         """
-> 1242         return cls(**config)
   1243 
   1244     def count_params(self):

~/Code/BinartNet/binary_layers.py in __init__(self, units, H, kernel_lr_multiplier, bias_lr_multiplier, **kwargs)
     35     '''
     36     def __init__(self, units, H=1., kernel_lr_multiplier='Glorot', bias_lr_multiplier=None, **kwargs):
---> 37         super(BinaryDense, self).__init__(units, **kwargs)
     38         self.H = H
     39         self.kernel_lr_multiplier = kernel_lr_multiplier

/opt/dl/anaconda/envs/chs_keras/lib/python3.5/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
     85                 warnings.warn('Update your `' + object_name +
     86                               '` call to the Keras 2 API: ' + signature, stacklevel=2)
---> 87             return func(*args, **kwargs)
     88         wrapper._original_function = func
     89         return wrapper

/opt/dl/anaconda/envs/chs_keras/lib/python3.5/site-packages/keras/layers/core.py in __init__(self, units, activation, use_bias, kernel_initializer, bias_initializer, kernel_regularizer, bias_regularizer, activity_regularizer, kernel_constraint, bias_constraint, **kwargs)
    810         self.bias_regularizer = regularizers.get(bias_regularizer)
    811         self.activity_regularizer = regularizers.get(activity_regularizer)
--> 812         self.kernel_constraint = constraints.get(kernel_constraint)
    813         self.bias_constraint = constraints.get(bias_constraint)
    814         self.input_spec = InputSpec(min_ndim=2)

/opt/dl/anaconda/envs/chs_keras/lib/python3.5/site-packages/keras/constraints.py in get(identifier)
    170         return None
    171     if isinstance(identifier, dict):
--> 172         return deserialize(identifier)
    173     elif isinstance(identifier, six.string_types):
    174         config = {'class_name': str(identifier), 'config': {}}

/opt/dl/anaconda/envs/chs_keras/lib/python3.5/site-packages/keras/constraints.py in deserialize(config, custom_objects)
    163                                     module_objects=globals(),
    164                                     custom_objects=custom_objects,
--> 165                                     printable_module_name='constraint')
    166 
    167 

/opt/dl/anaconda/envs/chs_keras/lib/python3.5/site-packages/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    146             custom_objects = custom_objects or {}
    147             with CustomObjectScope(custom_objects):
--> 148                 return cls(**config['config'])
    149     elif isinstance(identifier, six.string_types):
    150         function_name = identifier

TypeError: __init__() got an unexpected keyword argument 'name'

It looks like it is unable to deserialize the Clip object. What would be the recommend solution here? Thanks.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
DingKecommented, Aug 12, 2017

The code here is mostly for demo. For serious experiments, I got the numbers by strictly following the paper (try my best) :

datasets paper reported squared hinge loss crossentropy loss
MNIST (MLP) 0.96% 1.06% 1.02%
CIFAR10 (CNN) 11.40% 11.92% 11.91%
SVHN (CNN) 2.80% 2.94% 2.82%
1reaction
DingKecommented, Aug 10, 2017

The repo is updated w.r.t the newest keras. This line will correctly load saved model.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to load a Keras saved model (Error: unable to open file)
Try like this, model.save("my_model"). Calling model.save('my_model') creates a folder named my_model, containing the following:
Read more >
How to Save and Load Your Keras Deep Learning Model
In this post, you will discover how to save your Keras models to files and load them up again to make predictions. After...
Read more >
Save and load model neural network · Issue #37135 - GitHub
Hi, I am a beginner in neural network, I follow a tutorial (https://www.datacamp.com/community/tutorials/cnn-tensorflow-python) for create ...
Read more >
Saving and Loading Models (Coding TensorFlow) - YouTube
Training models can take a very long time, and you definitely don't want to have to retrain everything over a single mishap.
Read more >
Why Loading a Previously Saved Keras Model Gives Different ...
Namely, after training a Keras model, which is stored in a model variable, we like to save it as it is so that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found