-
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
custom_objects argument dropped on layer loading when calling model_from_config #5815
Comments
I ran into similar problem in 2.0.2. #!/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()
i = Input([3])
x = MyLayer()(i)
modelA = Model(inputs=i, outputs=x)
modelA.save("model.h5")
try:
load_model("model.h5", custom_objects={"MyLayer": MyLayer})
print("Succeeded to load modelA.")
except:
print("Failed to load modelA.")
os.remove("model.h5")
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") I also noticed |
I found my last post may be separate issue... I'll create it. In 2.0.2, #!/usr/bin/env python
import os
from keras.models import Model, Sequential, load_model, model_from_config
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()
i = Input([3])
x = MyLayer()(i)
modelA = Model(inputs=i, outputs=x)
conf = modelA.get_config()
try:
Model.from_config(conf, custom_objects={"MyLayer": MyLayer})
print("Succeeded.")
except:
print("Failed with Model.from_config.")
try:
model_from_config(conf, custom_objects={"MyLayer": MyLayer}) # this fails
print("Succeeded.")
except:
print("Failed with model_from_config.") |
@alreadytaikeune Thank you for your explanation. I still wonder why it cannot find SpatialPyramidPooling in models.py. So I copy my layer.py into keras folder directly. It is quite ugly but useful. |
@buptss I think there is also a modification to be made in |
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. |
After switching to keras 2.0.0 and progressively migrating our code base (containing various custom layers and losses) it seemed that there were a bug in the way models are now created from a config. More specifically, the
custom_objects
arguments passed tomodel_from_config
is lost in the back and forth between thedeserialize_keras_object
defined ingenerics_utils
and thefrom_config
method inmodels
resulting in an errorValueError: Unknown layer: ...
.The only solution that I've found is to introduce two simple modifications in the
Model.from_config
method:custom_objects={}
argumentlayer_module.deserialize
call in the layer loop.Did I miss something or is it indeed a problem with this new version? If it is should I submit a pull request?
Anis
The text was updated successfully, but these errors were encountered: