-
Notifications
You must be signed in to change notification settings - Fork 19.4k
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
load_model() with custom layers, and custom layers in general #4871
Comments
Use https://github.com/fchollet/keras/blob/master/keras/utils/layer_utils.py Make sure to implement get_config() in your custom layer, it is used to save the model correctly. If you have a lot of issues with load_model, save_weights and load_weights can be more reliable. Cheers, |
Thanks. Luckily I could use def load_model(filepath, custom_objects=None): so I guess I'm supposed to send a dictionary when I use I already implemented def get_config(self):
config = {'n_dft': self.n_dft,
'n_filter': self.n_filter,
'trainable_kernel': self.trainable_kernel,
'n_hop': self.n_hop,
'border_mode': self.border_mode,
'power': self.power,
'return_decibel_spectrogram': self.return_decibel_spectrogram}
base_config = super(Spectrogram, self).get_config()
return dict(list(base_config.items()) + list(config.items())) . I just added the attributes of the custom layer without understand what there should be. |
Models.py saves the layer configuration and the class name.
layer_from_config instantiates the class given the name.
Custom objects are added to the globals.
So, if you're crashing on get_from_module, pass in a dictionary from the string of the class name to the actual class so get_from_module can find your class. In terms of what to put into get_config, that same information is sent to your constructor. So the keywords in your get_config should match the kwargs of your constructor.
https://github.com/fchollet/keras/blob/master/keras/utils/layer_utils.py |
Perfect. Thanks a lot. |
For the people landing here by a Google search, the code we should use:
as can be found on kapre's Github page: https://github.com/keunwoochoi/kapre |
My model has a custom layer - SpatialTransformer which takes in 3 arguments. model.add(SpatialTransformer(localization_net=locnet, Going by your last message, I added the 'custom_objects' attrib to the load_model call. from above posts - How are arguments passed ? |
As far as I understand, custom layers only support loading if the author(s) implemented it. For example, the seq2seq model also doesn't support it, as can be found here: farizrahman4u/seq2seq#144 A workaround for this is to only save the models weight. Then, when you want to load the model, you first recreate it with the same setup and then load the weights. See for details the link above. |
@saritanavuluru
|
@LiamHe I have the same issue. Am new to Keras. What do you mean by overriding get_config do you mind explaining it a bit more detailed? |
@Abhijit-2592 Sorry that I can't elaborate the function for the moment. The function I wrote has bugs in some corner case. I will try to solve this problem over the weekend. |
@LiamHe And you also need to modify the build method in SpatialTransformer so that you correctly build the Sequential Layer that defines your localizer class SpatialTransformer(Layer):
|
I done in this way To save modelmodel.save('my_model_01.hdf5') To load the modelcustom_objects={'CRF': CRF,'crf_loss':crf_loss,'crf_viterbi_accuracy':crf_viterbi_accuracy} To load a persisted model that uses the CRF layermodel1 = load_model("/home/abc/my_model_01.hdf5", custom_objects = custom_objects) |
@bstriner Hello Ben, I'd like to know that, do I still need to pass Thanks a lot! |
Unfortunately keras does not recognize custom layers automatically, so each has to passed as an additional argument when calling `Model.from_config`. keras-team/keras#4871 Signed-off-by: Ângelo Lovatto <angelolovatto@gmail.com>
Unfortunately keras does not recognize custom layers automatically, so each has to passed as an additional argument when calling `Model.from_config`. keras-team/keras#4871 Signed-off-by: Ângelo Lovatto <angelolovatto@gmail.com>
Unfortunately keras does not recognize custom layers automatically, so each has to passed as an additional argument when calling `Model.from_config`. keras-team/keras#4871 Signed-off-by: Ângelo Lovatto <angelolovatto@gmail.com>
@zhoudaxia233 To my understanding, |
This repo shows a simple sample code to build your own keras layer and use it in your model |
try to construct the model and then load the weights |
load_model()
?I used my custom layers in this repo both Spectrogram and Melspectrogram didn't work for
load_model()
.Error message:
self.add_weight()
method? or just appending them inself.trainable_weights
is fine?get_config()
method? When and how are they used?self.built
is used?The text was updated successfully, but these errors were encountered: