2017-01-02 24 views
2

如何检查string_input_producer读取的文件名数量?根据输入数据的大小,将执行不同的操作,所以我需要知道将读取或读取多少图像。tensorflow获取string_input_producer的大小

下面的代码不告诉我有多少图片我已阅读或即将阅读。

import tensorflow as tf 
import matplotlib.pyplot as plt 

# Make a queue of file names including all the JPEG images files in the relative image directory. 
filename_queue = tf.train.string_input_producer(tf.train.match_filenames_once("./MNIST_data/*.png")) 

reader = tf.WholeFileReader() 
key, value = reader.read(filename_queue) 

image = tf.image.decode_png(value) # use png or jpg decoder based on your files. 

num_preprocess_threads = 1 
min_queue_examples = 256 
batch_size=2; 
images = tf.train.shuffle_batch([image], batch_size, min_queue_examples + 3 * batch_size, num_threads=num_preprocess_threads, min_after_dequeue=min_queue_examples) 

with tf.Session() as sess: 
    sess.run(tf.initialize_all_variables()) 

    coord = tf.train.Coordinator() 
    threads = tf.train.start_queue_runners(coord=coord) 
    t_image = image.eval() #here is your image Tensor :) 
    fig = plt.figure() 
    plt.imshow(t_image) 
    plt.show() 

    coord.request_stop() 
    coord.join(threads) 

回答

1

string_input_producer等功能将一个队列添加到当前图其可以每次只出队只是一个例子。通常情况下,输出张量将被馈送到像tf.train.shuffle_batch这样的函数,这正是你想要的。这个函数的自变量batch_size可以控制每次多少例子离队作为模型的输入

UPDATE:

,如果你想查询你的输入数据是否正确,你可以用sess.run(my_img)跑出来这会给你一个numpy.array张量。您可以直接查看张量的元素,或者将其绘制为matplotlib

确保您sess.run前已经开始排队跑步或你的程序将被挂起永远

+0

你有你的答案算法?我编辑我的代码包括shuffle_batch但即时通讯错误。你能显示完整的代码来阅读图像然后绘图吗?请fensortlow硬是 – kong

+1

你得到哪种错误?我看了你的代码,我认为没关系 –

+0

它说所有的形状都必须完全定义。错误指向tf.train.shuffle_batch – kong

0

string_input_producer回报你回到一个标准FIFOQueue(它会返回一个input_producer并返回你queue

一个FIFOQueue没有关于它读取的元素数量的信息,只有元素数量当前在队列中(q.size())。如果你想知道有多少元素已经被读取,你需要手动添加一个计数器,你将每次增加一个计数器当你阅读一个元素。