【问题标题】:How to debug a Python program that freezes on one line?如何调试在一行上冻结的 Python 程序?
【发布时间】:2017-06-02 07:24:53
【问题描述】:

当我运行我的代码时,它只是停留在image_batch, label_batch = sess.run([test_images, test_labels]) 行中,没有任何错误提示。它就呆在这儿不能动。

这是我的代码:

# 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()

我的最后一个问题与此类似,但代码与此有些不同,也许您可​​以在最后一个问题中得到一些东西。

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    似乎您没有正确启动队列运行器/初始化变量。当我忘记这一点时,我已经看到我的模型有类似的行为。 在这种情况下,您很可能会卡在这条线上

    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()
    

    然后在会话开始时,在从 tfrecord 中提取任何数据之前,运行 op 并启动​​队列运行器:

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

    【讨论】:

    • 非常感谢。你能帮我解决另一个问题吗?问题名称是“ Fetch argument 不能被解释为张量。”在我的另一个问题中。我需要你的帮助!非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    • 2016-11-15
    • 2018-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多