批量生成时,Tensorflow会混合图像和标签 [英] Tensorflow mixes up images and labels when making batch

查看:836
本文介绍了批量生成时,Tensorflow会混合图像和标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以几周以来我一直坚持这个问题。我想从图像文件名列表中批量生成图像。我将文件名列表插入队列并使用阅读器来获取文件。然后,阅读器返回文件名和读取的图像文件。



我的问题是,当我使用解码的jpg和阅读器中的标签进行批处理时,tf.train.shuffle_batch()将图像和文件名混合在一起现在标签的图像文件顺序错误。我在使用queue / 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#original image names and labelsimage_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] #swolution arrays to tensorsimage_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)#getting tensor slicesimage_path_tf,label_tf = tf .train.slice_input_producer([image_paths_tf,labels_tf],shuffle = False)从jpeg获取图像张量并执行preprocessingimage_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)#创建一批图像和labelsbatch_size = 5num_threads = 4images_batch_tf,labels_batch_tf = tf.train.batch([image_tf,label_tf],batch_size = batch_size,num_threads = num_threads)#running测试会话以检查图像和标签的顺序init = tf.global_variables_initializer(),使用tf.Session()作为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 )


So I've been stuck on this problem for weeks. I want to make an image batch from a list of image filenames. I insert the filename list into a queue and use a reader to get the file. The reader then returns the filename and the read image file.

My problem is that when I make a batch using the decoded jpg and the labels from the reader, tf.train.shuffle_batch() mixes up the images and the filenames so that now the labels are in the wrong order for the image files. Is there something I am doing wrong with the queue/shuffle_batch and how can I fix it such that the batch comes out with the right labels for the right files?

Much thanks!

import tensorflow as tf
from tensorflow.python.framework import ops


def 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

# original image names and labels
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]

# converting arrays to tensors
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")

# getting tensor slices
image_path_tf, label_tf = tf.train.slice_input_producer([image_paths_tf, labels_tf], shuffle=False)

# getting image tensors from jpeg and performing preprocessing
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)

# creating a batch of images and labels
batch_size = 5
num_threads = 4
images_batch_tf, labels_batch_tf = tf.train.batch([image_tf, label_tf], batch_size=batch_size, num_threads=num_threads)

# running testing session to check order of images and labels 
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)

解决方案

Wait.... Isn't your tf usage a little weird?

You are basically running the graph twice by calling:

  print image_path_tf.eval()
  print label_tf.eval()

And since you are only asking for image_path_tf and label_tf, anything below this line is not even run:

image_path_tf, label_tf = tf.train.slice_input_producer([image_paths_tf, labels_tf], shuffle=False)

Maybe try this?

image_paths, labels = sess.run([images_batch_tf, labels_batch_tf])
print(image_paths)
print(labels)

这篇关于批量生成时,Tensorflow会混合图像和标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆