Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to save Transfer Learning model in Keras

I have trained a constitutional net using transfer learning from ResNet50 in keras as given below.

base_model = applications.ResNet50(weights='imagenet', include_top=False, input_shape=(333, 333, 3))

## set model architechture
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x) 
x = Dense(256, activation='relu')(x) 
predictions = Dense(y_train.shape[1], activation='softmax')(x) 
model = Model(input=base_model.input, output=predictions)

model.compile(loss='categorical_crossentropy', optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),
              metrics=['accuracy'])

model.summary()

After training the model as given below I want to save the model.

history = model.fit_generator(
    train_datagen.flow(x_train, y_train, batch_size=batch_size),
    steps_per_epoch=600,
    epochs=epochs,
    callbacks=callbacks_list
)

I can't use save_model() function from models of keras as model is of type Model here. I used save() function to save the model. But later when i loaded the model and validated the model it behaved like a untrained model. I think the weights were not saved. What was wrong.? How to save this model properly.?

like image 744
Sreeram TP Avatar asked Sep 21 '17 13:09

Sreeram TP


2 Answers

As per Keras official docs, If you only need to save the architecture of a model you can use

model_json = model.to_json()
with open("model_arch.json", "w") as json_file:
    json_file.write(model_json)

To save weights

model.save_weights("my_model_weights.h5")

You can later load the json file and use

from keras.models import model_from_json
model = model_from_json(json_string)

And similarly, for weights you can use

model.load_weights('my_model_weights.h5')

I am using the same approach and this works perfectly well.

like image 107
Amarpreet Singh Avatar answered Oct 10 '22 08:10

Amarpreet Singh


I don't know what happens with my models, but I've never been able to use save_model() and load_model(), there is always an error associated. But these functions exist.

What I usually do is to save and load weights (it's enough for using the model, but may cause a little problem for further training, as the "optimizer" state was not saved, but it was never a big problem, soon a new optimizer finds its way)

model.save_weights(fileName)
model.load_weights(fileName)

Another option us using numpy for saving - this one never failed:

np.save(fileName,model.get_weights())
model.set_weights(np.load(fileName))

For this to work, just create your model again (keep the code you use to create it) and set its weights.

like image 1
Daniel Möller Avatar answered Oct 10 '22 06:10

Daniel Möller