Reputation: 39
I want to load weights from .hdf5
file, and use load_weights()
.
Error doesn't occur. But when I use the model to predict. The result is the same with the model before I load weights.
The load weights doesn't work.
My keras
version is 2.2.2
tensorflow
version is 1.10.0
How can I solve this problem. Thanks
Upvotes: 2
Views: 6528
Reputation: 11
If you can share code on how you are saving model in.hd5 then we would be able to assist you. Assuming you are trying to solve a "Computer Vision" problem. Also if needed you can print shape after input and pooling layer It is best practice to save weights instead of whole model as mentioned in Keras docs. Hope this helps
For eg: I have used modelcheckpoint to load a model (or feature extractor model). Also note below code is in functional API.
# create a base model(eg: EfficientNetB0 and so on)
base_model = tf.keras.applications.EfficientNetB0(include_top=False)
# include_top=False allow setting base model per your problem
# freeze layer
base_model.trainable=False
# create inputs
inputs = tf.keras.layers.Input(shape=(224,224,3), name="input_layer")
# pass inputs to base model
x = base_model(inputs)
# perform pooling
x = tf.keras.layers.GlobalAveragePooling2D(name="pooling_layer")(x)
# create outputs
outputs = tf.keras.layers.Dense(10, activation="softmax",name="output_layer")(x)
# need to modify classes per your problem
# combine inputs and outputs with base model
model = tf.keras.Model(inputs,outputs)
# compile model
....
#fit model
model.fit(train_data, epochs=5,validation_data=test_data,validation_steps=len(test_data),callbacks=[tf.keras.callbacks.ModelCheckpoint(ckhpt_filepath,save_weights=True,save_best_only=True,save_freq="epoch",verbose=1)
# load best weights and evaluate model
model.load_weights(ckhpt_filepath)
loaded_model_results = model.evaluate(test_data)
Upvotes: 0
Reputation: 646
Have you saved weight and load weight like this?
from keras.models import Sequential
from keras.layers import Dense
from keras.models import model_from_json
model = Sequential()
model.add(Dense(12, input_dim=8, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1,activation='sigmoid'))
Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
Fit the model & evaluate
model.fit(X, Y, epochs=150, batch_size=10, verbose=0)
scores = model.evaluate(X, Y, verbose=0)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
serialize model to JSON
model_json = model.to_json()
with open("model.json", "w") as json_file:
json_file.write(model_json)
serialize weights to HDF5
model.save_weights("model.h5")
print("Saved model to disk")
later... load json and create model
json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
load weights into new model
loaded_model.load_weights("model.h5")
print("Loaded model from disk")
evaluate loaded model on test data
loaded_model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
score = loaded_model.evaluate(X, Y, verbose=0)
print("%s: %.2f%%" % (loaded_model.metrics_names[1], score[1]*100))
Upvotes: 3