windows下tensorflow两种加载模型的测试数据方法

一、加载多次保存的模型中的某一次模型,而不是latest的一次
global sess
global charcnn
def get_logits_with_value_by_input(start,end):
    x=test_x[start:end]
    global sess
    global charcnn
    logits = sess.run(charcnn.predictions, feed_dict={charcnn.input_x: x, charcnn.dropout_keep_prob: 1})
    real_labels=test_y[start:end]
    real_labels_label=tf.argmax(real_labels,axis=1)
    return logits,real_labels_label



with tf.Graph().as_default():
    session_config=tf.ConfigProto(allow_soft_placement=True,log_device_placement=False)
    sess=tf.Session(config=session_config)
    with tf.Session() as sess:
        charcnn = charCNN(config.l0, config.num_classes, config.model.conv_layers, config.model.fc_layers,
                          l2_reg_lambda=0.0)
         saver = tf.train.Saver()
        #checkpoint_dir = os.path.abspath(os.path.join(out_dir, 'checkpoints'))
         if os.path.exists("./run/1513350504/checkpoints/checkpoint"):    #一定要用‘/’这个反斜杠
            print("Restoring Variables from Checkpoint")
            saver = tf.train.import_meta_graph('./run/1513350504/checkpoints/model-1200.meta')              #导入验证准确率比较高的某个计算图
            saver.restore(sess,'./run/1513350504/checkpoints/model-1200')                                                           #加载模型,不加后缀名                  
        else:
            print("Can't find the checkpoint.going to stop")
        logits,real_labels_label=get_logits_with_value_by_input(0,-1)
        print (logits)
        print (real_labels_label.eval())



二、直接加载checkpoint中最近latest的模型,(不过有时候模型收敛的不好,latest的模型准确率不高)
with tf.Graph().as_default():
    session_config=tf.ConfigProto(allow_soft_placement=True,log_device_placement=False)
    sess=tf.Session(config=session_config)
    with tf.Session() as sess:
        charcnn = charCNN(config.l0, config.num_classes, config.model.conv_layers, config.model.fc_layers,
                          l2_reg_lambda=0.0)
        saver = tf.train.Saver()
        #checkpoint_dir = os.path.abspath(os.path.join(out_dir, 'checkpoints'))
         if os.path.exists("checkpoint"):
            print("Restoring Variables from Checkpoint")
            saver.restore(sess, tf.train.latest_checkpoint('checkpoints/'))               #还是这种反斜杠
        else:
            print("Can't find the checkpoint.going to stop")
        logits,real_labels_label=get_logits_with_value_by_input(0,-1)
        print (logits)
        print (real_labels_label.eval())
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值