Skip to main content
 首页 » 编程设计

python之tensorflow 获取 string_input_producer 的大小

2025年02月15日28xiaohuochai

如何检查 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) 

请您参考如下方法:

string_input_producer 等函数会向当前图添加一个队列,每次只能出列一个示例。通常,输出张量将提供给 tf.train.shuffle_batch 之类的函数,这正是您想要的。此函数的参数 batch_size 可以控制每次作为模型输入出队的示例数量

更新:

如果你想检查你的输入数据是否正确,你可以用 sess.run(my_img) 运行它,它会给你一个 numpy.array 张量。您可以直接查看此张量的元素,也可以使用 matplotlib 绘制它。

确保你已经在 sess.run 之前启动了队列运行器,否则你的程序将永远挂起