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

How should I load the trained model with keras? #58

Open
RainVerse opened this issue Jul 26, 2019 · 3 comments
Open

How should I load the trained model with keras? #58

RainVerse opened this issue Jul 26, 2019 · 3 comments

Comments

@RainVerse
Copy link

RainVerse commented Jul 26, 2019

When I try to load a trained model, I get the error:
ValueError: Unknown layer: ResNet2D50
I tried add custom objects while loading:
{'ResNet2D50': keras_resnet.models.ResNet2D50}
but I find it useless
@hgaiser @bzamecnik

@RainVerse RainVerse changed the title How should I How should I load the trained model with keras? Jul 26, 2019
@studyzx
Copy link

studyzx commented Jun 2, 2020

I also encountered this problem. Did you solve it?

@QinggangSUN
Copy link

I also encountered this problem. Did you solve it?

Me too. This link may be helpful.
https://stackoverflow.com/questions/51806852/cant-save-custom-subclassed-model

@QinggangSUN
Copy link

QinggangSUN commented Jan 15, 2021

I found a way to solve it. Create a new model and load the weights from the saved .h5 model. This way is not preferred, but it works. I don't want to re-train my models.

class MyModel(keras.Model):
    def __init__(self, inputs, *args, **kwargs):
        outputs = func(inputs)
    super(MyModel, self).__init__( inputs=inputs, outputs=outputs, *args, **kwargs)

def get_model():
    return MyModel(inputs, *args, **kwargs)

model = get_model()
model.save(‘file_path.h5’)

model_new = get_model()
model_new.compile(optimizer=optimizer, loss=loss, metrics=metrics)
model_new.load_weights(‘file_path.h5’)
model_new.evaluate(x_test, y_test, **kwargs)

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

3 participants