tensorflow的模型文件爲如下的形式
checkpoint
model.ckpt-200.data-00000-of-00001
model.ckpt-200.index
model.ckpt-200.meta
1.checkpoint文件
tf.train.get_checkpoint_state
函數通過checkpoint文件找到模型文件名
model_checkpoint_path保存了最新的tensorflow模型文件的文件名,all_model_checkpoint_paths則有未被刪除的所有tensorflow模型文件的文件名
model_checkpoint_path: "./model/model_test.ckpt-251"
all_model_checkpoint_paths: "./model/model_test.ckpt-51"
all_model_checkpoint_paths: "./model/model_test.ckpt-101"
all_model_checkpoint_paths: "./model/model_test.ckpt-151"
all_model_checkpoint_paths: "./model/model_test.ckpt-201"
all_model_checkpoint_paths: "./model/model_test.ckpt-251"
2.meta文件
保存圖結構
tf.train.import_meta_graph(‘model.ckpt-200.meta’)能夠導入圖結構
3.data文件
保存模型中權重等數據
4.保存與加載模型
saver=tf.train.Saver(max_to_keep = 5)
###max_to_keep參數表示最多保存的模型的個數
with tf.Session() as sess:
###如果True則重新加載模型
if Cfg.restore_model_flag:
###tf.train.get_checkpoint_state函數通過checkpoint文件找到模型文件名
ckpt = tf.train.get_checkpoint_state('./model/')
print('ckpt:\n',ckpt)
saver.restore(sess,ckpt.model_checkpoint_path) ###導入最新的模型
else:
tf.global_variables_initializer().run()
..........
###保存模型
saver.save(sess,os.path.join(Cfg.MODEL_SAVE_PATH,Cfg.MODEL_NAME),global_step=global_step)