-
Notifications
You must be signed in to change notification settings - Fork 19.6k
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 not load_model() or load_from_json() if my model contains my own Layer #8612
Comments
From the FAQ: Handling custom layers (or other custom objects) in saved modelsIf the model you want to load includes custom layers or other custom classes or functions, from keras.models import load_model
# Assuming your model includes instance of an "AttentionLayer" class
model = load_model('my_model.h5', custom_objects={'AttentionLayer': AttentionLayer}) Alternatively, you can use a custom object scope: from keras.utils import CustomObjectScope
with CustomObjectScope({'AttentionLayer': AttentionLayer}):
model = load_model('my_model.h5') Custom objects handling works the same way for from keras.models import model_from_json
model = model_from_json(json_string, custom_objects={'AttentionLayer': AttentionLayer}) |
@bmabey Thanks for the hints!
Neither of two methods failed, return "Unknown layer: Attention" It's so strange. I can use model.load_weights(filepath) to load the saved weights genearted by the same model architecture. I cannot load the model architecture from file. But only by running the code again. |
@stevewyl Is the Attention layer defined within the same file? As far as I know you have to provide the module of the Attention layer, e.g. custom_ob = {'AttLayer1': custom_layer.Attention,'AttLayer2': custom_layer.Attention} This is analogue to the import statement at the beginning of the file. |
It will help you |
It will error out when using ModelCheckpoint Callback |
Updated README.md for tested models (AlexNet/Keras). The only change needed is while importing the LRN layer, pass it as a dictionary (more info here : keras-team/keras#8612)
@stevewyl I am facing the same issue too. Did you get any solution for the issue ? |
* Updated README.md for tested models (AlexNet/Keras) Updated README.md for tested models (AlexNet/Keras). The only change needed is while importing the LRN layer, pass it as a dictionary (more info here : keras-team/keras#8612) * Update README.md * Update README.md * Update README.md * Create keras_custom_layer_usage.md Instructions for adding custom layers to Keras * Update keras_custom_layer_usage.md Changed keras_custom_layer_usage * Update README.md * Update README.md
When using a custom layer, you will have to define a get_config function into the layer class. Example: https://github.com/keras-team/keras/blob/master/keras/layers/convolutional.py#L214 This will show you how to adapt the get_config code to your custom layers. With the example above:
You will need to retrain the model using the new class code. |
i have seen this error posted in several places on the internet, and has been fixed in tensorflowjs but not keras or tf python. my model is culled from early-stopping callback, im not saving it manually. this appears to be common Traceback (most recent call last): |
* Updated README.md for tested models (AlexNet/Keras) Updated README.md for tested models (AlexNet/Keras). The only change needed is while importing the LRN layer, pass it as a dictionary (more info here : keras-team/keras#8612) * Update README.md * Update README.md * Update README.md * Create keras_custom_layer_usage.md Instructions for adding custom layers to Keras * Update keras_custom_layer_usage.md Changed keras_custom_layer_usage * Update README.md * Update README.md
I was having same problem when my model contains customer layers, after few hours of debugging, perfectly worked using: with CustomObjectScope({'AttentionLayer': AttentionLayer}): other than: model = load_model('my_model.h5', custom_objects={'AttentionLayer': AttentionLayer}) maybe bug of keras? |
Hello! I have tried both but I got the error. "ValueError: Unknown layer: Attention" |
@AdnanRiaz107 is the name of attention layer |
This repo shows a simple sample code to build your own keras layer and use it in your model |
I am trying to build my own model_from_json function from scratch as I am working with a custom .json file. My custom json file follows this format:
How can I extract the training_params and model architecture from my custom json to create a model of that architecture and parameters with this line of code |
this is my code:
`from keras import backend as K
from keras.engine.topology import Layer
from keras.models import load_model
from keras.layers import Dense
from keras.models import Sequential,model_from_json
import numpy as np
class MyLayer(Layer):
model = Sequential()
model.add(Dense(32, input_shape=(784,)))
model.add(MyLayer(100))
#this is ok
model.save('mode_test.h5')
#wrong
model = load_model('mode_test.h5')
json_string = model.to_json()
open('my_model_architecture.json', 'w').write(json_string)
model.save_weights('my_model_weights.h5')
model = model_from_json(open('my_model_architecture.json').read())
model.load_weights('my_model_weights.h5')`
the Error is:
ValueError: Unknown layer: MyLayer
The text was updated successfully, but these errors were encountered: