关于保存和导入可以参看 https://www.tensorflow.org/guide/keras/save_and_serialize
# Save the model
model.save('path_to_my_model.h5')
# Recreate the exact same model purely from the file
new_model = keras.models.load_model('path_to_my_model.h5')
知道了保存和导入,我关心的是再培训。培训次数太多,不知道什么时候结束。培训了很久,基本达到要求了,只要再培训一点点了,就不知道怎么再继续培训。我找呀找,后来找到很简单,就是继续培训 model.fit 就好了。
比如
model.fit(xs, ys, epochs=100)
或者:
INIT_LR = 0.01
EPOCHS = 15
BS = 32
# initialize the model and optimizer (you'll want to use
# binary_crossentropy for 2-class classification)
print("[INFO] retraining network...")
#opt = SGD(lr=INIT_LR, decay=INIT_LR / EPOCHS)
#model.compile(loss="categorical_crossentropy", optimizer=opt,
# metrics=["accuracy"])
# train the network
H = model.fit_generator(aug.flow(trainX, trainY, batch_size=BS),
validation_data=(testX, testY), steps_per_epoch=len(trainX) // BS,
epochs=EPOCHS)
注意的是不要再compile 等初始化了,否则以前的培训不起作用了。
下面再说说保存和导入:
全部保存在一个文件可以用
model.save('path_to_saved_model',save_format='tf')
导入的方式还是一样,new_model=keras.models.load_model('path_to_saved_model')
示范代码如下:
# Export the model to a SavedModel
model.save('path_to_saved_model', save_format='tf')
# Recreate the exact same model
new_model = keras.models.load_model('path_to_saved_model')
# Check that the state is preserved
new_predictions = new_model.predict(x_test)
np.testing.assert_allclose(predictions, new_predictions, rtol=1e-6, atol=1e-6)
# Note that the optimizer state is preserved as well:
# you can resume training where you left off.
开头的那种方式会保存成一个目录,model.save('path_to_saved_model'),会保存一个目录path_to_saved_model ,然后其下会有2个目录和一个文件save_model.pb。
path_to_saved_model
assets saved_model.pb variables
注意:
虽然我们用tensorflow2的也是keras,但是其保存的模型和 直接的keras 保存的模型不通用,有时会报错。
模型保存到JSON
JSON是一种用于分层描述数据的简单文件格式。
Keras提供了使用to_json()函数使用JSON格式描述任何模型的功能。可以通过model_from_json()函数保存该函数,该函数将根据JSON规范创建一个新模型。
使用save_weights()函数直接从模型中保存权重到model.h5,然后使用load_weights()函数加载权重。
from tensorflow.keras.models import model_from_json
from tensorflow.keras.models import model_from_json
#-----other code------
# serialize model to JSON
print("save 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
print('load from json')
json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
print('load from h5')
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights("model.h5")
print("Loaded model from disk")
保存模型到 YAML
这个例子和保存到 JSON 一样,只是改为 YAML 格式,要求安装好了 PyYAML 5
from tensorflow.keras.models import model_from_yaml
#-----other code -------
# serialize model to YAML
model_yaml = model.to_yaml()
with open("model.yaml", "w") as yaml_file:
yaml_file.write(model_yaml)
# serialize weights to HDF5
model.save_weights("model.h5")
print("Saved model to disk")
# later...
# load YAML and create model
print('load from yaml')
yaml_file = open('model.yaml', 'r')
loaded_model_yaml = yaml_file.read()
yaml_file.close()
loaded_model = model_from_yaml(loaded_model_yaml)
# load weights into new model
print('load from h5')
loaded_model.load_weights("model.h5")
print("Loaded model from disk")