我已经为这个问题困扰了几个星期。我想从一系列图像文件名中创建一个图像批次。我将文件名列表插入队列,并使用读取器获取文件。读取器随后返回文件名和读取的图像文件。
我的问题是,当我使用解码的jpg和读取器中的标签来制作批次时,tf.train.shuffle_batch()会混淆图像和文件名,导致标签与图像文件的顺序不匹配。我在使用队列/混洗批次时是不是做错了什么?如何修复这个问题,使批次输出时标签与正确的文件对应?
非常感谢!
import tensorflow as tffrom tensorflow.python.framework import opsdef preprocess_image_tensor(image_tf): image = tf.image.convert_image_dtype(image_tf, dtype=tf.float32) image = tf.image.resize_image_with_crop_or_pad(image, 300, 300) image = tf.image.per_image_standardization(image)return image# 原图像名称和标签image_paths = ["image_0.jpg", "image_1.jpg", "image_2.jpg", "image_3.jpg", "image_4.jpg", "image_5.jpg", "image_6.jpg", "image_7.jpg", "image_8.jpg"]labels = [0, 1, 2, 3, 4, 5, 6, 7, 8]# 将数组转换为张量image_paths_tf = ops.convert_to_tensor(image_paths, dtype=tf.string, name="image_paths_tf")labels_tf = ops.convert_to_tensor(labels, dtype=tf.int32, name="labels_tf")# 获取张量切片image_path_tf, label_tf = tf.train.slice_input_producer([image_paths_tf, labels_tf], shuffle=False)# 从jpeg获取图像张量并进行预处理image_buffer_tf = tf.read_file(image_path_tf, name="image_buffer")image_tf = tf.image.decode_jpeg(image_buffer_tf, channels=3, name="image")image_tf = preprocess_image_tensor(image_tf)# 创建图像和标签的批次batch_size = 5num_threads = 4images_batch_tf, labels_batch_tf = tf.train.batch([image_tf, label_tf], batch_size=batch_size, num_threads=num_threads)# 运行测试会话以检查图像和标签的顺序 init = tf.global_variables_initializer()with tf.Session() as sess: sess.run(init) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) print image_path_tf.eval() print label_tf.eval() coord.request_stop() coord.join(threads)
回答:
等等……你的tf用法是不是有点奇怪?
你通过调用以下代码基本上是运行了两次图:
print image_path_tf.eval() print label_tf.eval()
而且由于你只请求了image_path_tf
和label_tf
,以下这行代码之后的任何内容都没有运行:
image_path_tf, label_tf = tf.train.slice_input_producer([image_paths_tf, labels_tf], shuffle=False)
也许你可以试试这个?
image_paths, labels = sess.run([images_batch_tf, labels_batch_tf])print(image_paths)print(labels)