The right way to save Keras learning transfer model

I trained the constitutional network using the transfer of training from the ResNet50 to keras, as follows.

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 learning the model 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 cannot use the save_model () function from keras models, since the model is of type Model here. To save the model, I used the save () function. But later, when I downloaded the model and checked the model, it behaved like an unprepared model. I think the weights were not saved. What happened.? How to save this model.?

+4
source share
2 answers

Keras, ,

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

model.save_weights("my_model_weights.h5")

json

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

,

model.load_weights('my_model_weights.h5')

, .

+5

, , save_model() load_model(), . .

, , - ( , , "" , , )

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

numpy - :

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

, ( , ) .

+1

Source: https://habr.com/ru/post/1686134/


All Articles