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

Can't load Sequential with custom layers #5956

Closed
3 of 4 tasks
t-ae opened this issue Mar 24, 2017 · 1 comment
Closed
3 of 4 tasks

Can't load Sequential with custom layers #5956

t-ae opened this issue Mar 24, 2017 · 1 comment

Comments

@t-ae
Copy link
Contributor

t-ae commented Mar 24, 2017

Now Sequeltial.from_config doesn't have cutsom_objects argument.
Sequential with custom layers can create config, but can't restore from it.
Is it a specification?

And if I save/load such model, Sequential.from_config is called internally here. custom_objects is ignored and raises ValueError: Unknown layer.
I think it should check custom_objects is None if from_config doesn't have custom_objects argument.

Sample code

#!/usr/bin/env python

import os
from keras.models import Model, Sequential, load_model
from keras.layers import Input, InputLayer
from keras.engine.topology import Layer

class MyLayer(Layer):
    def __init__(self, **kwargs):
        super(MyLayer, self).__init__(**kwargs)

    def call(self, inputs):
        return inputs + 1

    def compute_output_shape(self, input_shape):
        return input_shape

    def get_config(self):
        return super(MyLayer, self).get_config()

modelB = Sequential([
    InputLayer([10]),
    MyLayer()
])
modelB.save("model.h5")
try:
    load_model("model.h5", custom_objects={"MyLayer": MyLayer})  # this fails
    print("Succeeded to load modelB.")
except:
    print("Failed to load modelB.")
os.remove("model.h5")

Please make sure that the boxes below are checked before you submit your issue. If your issue is an implementation question, please ask your question on StackOverflow or join the Keras Slack channel and ask there instead of filing a GitHub issue.

Thank you!

  • Check that you are up-to-date with the master branch of Keras. You can update with:
    pip install git+git://github.com/fchollet/keras.git --upgrade --no-deps

  • If running on TensorFlow, check that you are up-to-date with the latest version. The installation instructions can be found here.

  • If running on Theano, check that you are up-to-date with the master branch of Theano. You can update with:
    pip install git+git://github.com/Theano/Theano.git --upgrade --no-deps

  • Provide a link to a GitHub Gist of a Python script that can reproduce your issue (or just copy the script here if it is short).

@t-ae
Copy link
Contributor Author

t-ae commented Mar 27, 2017

Handed over to #5995.
@ushakov
Thank you!

@t-ae t-ae closed this as completed Mar 27, 2017
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

1 participant