2017-06-02 90 views
1

當我運行我的代碼時,它只是保持在行image_batch, label_batch = sess.run([test_images, test_labels])沒有任何錯誤提示。它只是呆在這裏,不能移動。如何調試在一行上凍結的Python程序?

這裏是我的代碼:

# coding=utf-8 
from color_1 import read_and_decode, get_batch, get_test_batch 
import color_inference 
import cv2 
import os 
import time 
import numpy as np 
import tensorflow as tf 
import color_train 
import math 

batch_size=128 
num_examples = 10000 
crop_size=56 

def evaluate(): 
    image_holder = tf.placeholder(tf.float32, [batch_size, 56, 56, 3], name='x-input') 
    label_holder = tf.placeholder(tf.int32, [batch_size], name='y-input') 

    test_image, test_label = read_and_decode('val.tfrecords') 
    test_images, test_labels = get_test_batch(test_image, test_label, batch_size, crop_size) 
    y=color_inference.inference(image_holder) 

    num_iter = int(math.ceil(num_examples/batch_size)) 
    true_count = 0 
    total_sample_count = num_iter * batch_size 

    top_k_op = tf.nn.in_top_k(y, label_holder, 1) 
    saver = tf.train.Saver() 
    with tf.Session() as sess: 

     ckpt=tf.train.get_checkpoint_state(color_train.MODEL_SAVE_PATH) 
     if ckpt and ckpt.model_checkpoint_path: 
      ckpt_name = os.path.basename(ckpt.model_checkpoint_path) 
      global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] 
      saver.restore(sess, os.path.join(color_train.MODEL_SAVE_PATH, ckpt_name)) 
      print('Loading success, global_step is %s' % global_step) 

      image_batch, label_batch = sess.run([test_images, test_labels]) 
      predictions = sess.run([top_k_op], feed_dict={image_holder: image_batch, 
                  label_holder: label_batch}) 
      true_count += np.sum(predictions) 
      print("Count is:%g" % true_count) 
      precision = true_count * 1.0/total_sample_count 
      print("After %s training step,the prediction is :%g",global_step,precision) 
     else: 
      print('No checkpoint file found') 
      return 

def main(argv=None): 
    evaluate() 

if __name__=='__main__': 
    tf.app.run() 

我的最後一個問題是與此類似,但代碼是垃圾與此不同,也許你可以在最後一個問題的東西。

回答

0

好像你沒有啓動隊列運行者/正確地初始化變量。當我忘記了這一點時,我看到了與我的模特類似的行爲。 如果這是你最有可能卡住在行

image_batch, label_batch = sess.run([test_images, test_labels]) 

,因爲從tfrecords提取數據的線程都沒有開始的情況。

之前初始化會話建立一個運算初始化的變量和線程協調:

init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) 
coord = tf.train.Coordinator() 

然後在會話的一開始,從tfrecords拉任何數據之前運行的運算和啓動隊列跑步者:

sess.run(init_op) 
threads = tf.train.start_queue_runners(sess=sess, coord=coord) 
# main loop goes here, like training and evaluating 
+0

非常感謝。你能幫我解決另一個問題嗎?問題名稱是「Fetch argument 不能被解釋爲張量。」在我的另一個問題,我需要你的幫助!非常感謝你 –