broadinstitute / keras-resnet

Keras package for deep residual networks
Other
300 stars 127 forks source link

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

Open RainVerse opened 5 years ago

RainVerse commented 5 years ago

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

studyzx commented 4 years ago

I also encountered this problem. Did you solve it?

QinggangSUN commented 3 years ago

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 commented 3 years ago

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)