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())
發佈了116 篇原創文章 · 獲贊 204 · 訪問量 70萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章