機器學習 tensorflow 2 的model 的保存,導入和繼續培訓

關於保存和導入可以參看 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")

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章