tf.train.get_checkpoint_state函数通过checkpoint文件找到模型文件名。

tf.train.get_checkpoint_state(checkpoint_dir,latest_filename=None)

该函数返回的是checkpoint文件CheckpointState proto类型的内容,其中有model_checkpoint_path和all_model_checkpoint_paths两个属性。其中model_checkpoint_path保存了最新的tensorflow模型文件的文件名,all_model_checkpoint_paths则有未被删除的所有tensorflow模型文件的文件名。

下图是在训练过程中生成的几个模型文件列表:

【tensorflow】tf.train.get_checkpoint_state_函数返回


以下是测试程序里的部分代码:

    with tf.Session() as sess:            
                ckpt=tf.train.get_checkpoint_state('Model/')
                print(ckpt)
                if ckpt and ckpt.all_model_checkpoint_paths:
                    #加载模型
                    #这一部分是有多个模型文件时,对所有模型进行测试验证
                    for path in ckpt.all_model_checkpoint_paths:
                        saver.restore(sess,path)                
                        global_step=path.split('/')[-1].split('-')[-1]
                        accuracy_score=sess.run(accuracy,feed_dict=validate_feed)
                        print("After %s training step(s),valisation accuracy = %g"%(global_step,accuracy_score))
                    '''
                    #对最新的模型进行测试验证
                    saver.restore(sess,ckpt.model_checkpoint_paths)                
                    global_step=ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
                    accuracy_score=sess.run(accuracy,feed_dict=validate_feed)
                    print("After %s training step(s),valisation accuracy = %g"%(global_step,accuracy_score))
                    '''
                else:
                    print('No checkpoint file found')
                    return
            #time.sleep(eval_interval_secs)
            return

在上面代码中,通过tf.train.get_checkpoint_state函数得到的相关模型文件名如下:

【tensorflow】tf.train.get_checkpoint_state_文件名_02


对所有模型进行测试,得到:

【tensorflow】tf.train.get_checkpoint_state_文件名_03

​​参考1​​