最近在tensorflow框架下调试代码时遇到sess.run()读取数据卡住的情况,搜索尝试了许多方法终于找到解决方案,希望遇到同样问题的小伙伴能尽快解决问题。
问题描述:img, label = sess.run([image, labels])
程序运行到改行进行数据读取时,不报错,但是程序卡在这条语句,无法往下执行
解决方案:在with tf.session() as sess: 部分加如下语句
coord = tf.train.Coordinator()
thread = tf.train.start_queue_runners(sess, coord)
详细解释:
image—图片保存路径列表(string类型);
labels—标签列表(int32类型);
由于数据量太大,因此在对图片数据我们通常采用批量处理的方式,也即一次处理batch_size(最小可以取为1)张图片,减轻CPU的负担,保证程序的正常运行。
而在此过程当中,数据经历的两次变换(现将原类型string / int32 均记为 numpy ):
numpy — tensor — numpy
所述的问题就是在第二次变换(由tensor转为numpy)时出现的
(1)由numpy转为tensor代码
def get_batch(image, label, image_W, image_H, batch_size, capacity):
'''
Args:
image: list type
label: list type
image_W: image width
image_H: image height
batch_size: batch size
capacity: the maximum elements in queue
Returns:
image_batch: 4D tensor [batch_size, width, height, 3], dtype=tf.float32
label_batch: 1D tensor [batch_size], dtype=tf.int32
'''
# image_W, image_H, :设置好固定的图像高度和宽度
# 设置batch_size:每个batch要放多少张图片
# capacity:一个队列最大多少
image = tf.cast(image,