TensorFlow从文件中读取数据的queue方法 使用tf.train.string_input_producer(), tf.train.batch()等接口
无监督学习的单个数据读入(只有图像,没有标签,如GAN、VAE)
问题1:如果不用tf.image.crop_to_bounding_box函数把图像的尺寸固定,直接用尺寸为None的图像读入会如何?
答案是不行!使用tf.train.batch接口要求输入图像的尺寸必须是确定的!
问题2:使用tf.train.batch得到输入的图像batch队列之后,如何把batch送入网络呢?是通过feed_dict吗?
答:显然不是,feed_dict是输入的numpy数组
问题3:注意读入的图像只能是png,jpg或者gif格式
import os import tensorflow as tf import numpy as np import collections ''' 我们希望批量地读取图像数据,每个batch送入一个简单的网络中 然后计算图像均值或者不作处理,最后sess.run输出 ''' # 可选参数如下 path = '../DATA/textures_png/' batch_size = 10 crop_size = 64 # 首先我们搭建一个象征性的网络 def net(inputs_): return inputs_#tf.reduce_mean(inputs_,axis = 0) def data_loader(path): with tf.device('/cpu:0'): Data = collections.namedtuple('Data', 'image_batch, image_count') image_list = os.listdir(path) print(image_list) image_list = [_ for _ in image_list if _.endswith('.png')] if len(image_list)==0: raise Exception('No png files in the input directory !') image_list = [os.path.join(path, _) for _ in image_list] with tf.variable_scope('load_image'): filename_queue = tf.train.string_input_producer(image_list, shuffle=False, capacity=128) print('filename_queue : ',filename_queue) reader = tf.WholeFileReader() key,value = reader.read(filename_queue) image = tf.image.decode_png(value, channels=3) image = tf.image.convert_image_dtype(image, dtype=tf.float32) assertion = tf.assert_equal(tf.shape(image)[2], 3, message="image does not have 3 channels") with tf.control_dependencies([assertion]): image = tf.identity(image) # The data augmentation part with tf.name_scope('random_crop'): print('[Config] Use random crop') input_size = tf.shape(image) h, w = tf.cast(input_size[0], tf.float32),\ tf.cast(input_size[1], tf.float32) offset_w = tf.cast(tf.floor(tf.random_uniform([], 0, w - crop_size)), dtype=tf.int32) offset_h = tf.cast(tf.floor(tf.random_uniform([], 0, h - crop_size)), dtype=tf.int32) image = tf.image.crop_to_bounding_box(image, offset_h, offset_w, crop_size, crop_size) # image = preprocess(image) image_batch = tf.train.batch([image],\ batch_size = batch_size,\ capacity = 128,\ num_threads = 4) return Data( image_batch = image_batch, image_count=len(image_list), ) data = data_loader(path) with tf.variable_scope('neural_net'): outputs = net(data.image_batch) config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config = config) as sess: sess.run(tf.global_variables_initializer()) coord = tf.train.Coordinator() thread = tf.train.start_queue_runners(sess, coord) for i in range(10): outputs_ = sess.run(outputs) print(i) print(outputs_.shape) coord.join(thread) sess.close() # 这两句不要也罢
有监督学习的 标签在文件名中 类型的数据读入(图像分类)
import tensorflow as tf import os import matplotlib.pyplot as plt ''' 所有数据都放在一个文件夹下,文件名中包含了图像的类别信息 ''' path = '../DATA/marble_test_png/' classes = 130 batch_size = 1 crop_size = 1024 imagePathList=[] labelList=[] for filename in os.listdir(path): imagePathList.append(os.path.join(path, filename)) labelList.append(int(filename.split('_')[0])) # 从文件名获得图像所述的类别标签(还没有one-hot,是一个整数) image = tf.cast(imagePathList,tf.string) label = tf.cast(labelList,tf.int64) queue = tf.train.slice_input_producer([image,label],shuffle=True, capacity=128) label = queue[1] image = tf.read_file(queue[0]) image = tf.image.decode_png(image,channels = 3) # 对图像进行预处理(这里只是简单地进行了crop) with tf.name_scope('crop'): training_image_data = tf.image.crop_to_bounding_box(image, 0, 0, crop_size, crop_size) image_batch, label_batch = tf.train.batch([training_image_data, label],\ batch_size = batch_size,\ capacity = 128,\ num_threads = 4) # 运行Graph with tf.Session() as sess: coord = tf.train.Coordinator() #创建一个协调器,管理线程 threads = tf.train.start_queue_runners(coord=coord) #启动QueueRunner, 此时文件名队列已经进队。 for i in range(6): e_val,l_val = sess.run([image_batch, label_batch]) print(l_val) fig = plt.figure(figsize=(2,2),dpi=200) plt.imshow(e_val[0]) plt.show() coord.request_stop() coord.join(threads)
image_batch, label_batch本身就是tensor,可以直接前馈入网络、计算损失函数或者sess.run得到numpy数组。
特别注意!sess.run的时候要将image_batch和label_batch同时执行sess.run()操作,否则会出现图像与标签不对应的情况!如果有不同的步长(例如每100次display,每10000次保存模型)建议用一个dict对各种需要run的对象进行fetch。
有监督学习的 图像-图像对 数据读入(图像增强、语义分割、超分等end2end任务)
import tensorflow as tf import os import matplotlib.pyplot as plt import collections ''' 将图像对按照完全相同的文件名存放在两个不同的文件夹下 ''' im1_dir = 'D:\WorkSpace\DATA\marble130_pair' im2_dir = 'D:\WorkSpace\DATA\marble130_pair1' h = 1024 w = 1024 batch_size = 1 name_queue_capacity = 128 image_queue_capacity = 128 Data = collections.namedtuple('Data', 'path_im1, path_im2, im1, im2, count') list_im1 = os.listdir(im1_dir) list_im1 = [_ for _ in list_im1 if _.endswith('.png')] if len(list_im1) == 0: raise Exception('No png files in the im1 directory') list_im1_ = sorted(list_im1) list_im1 = [os.path.join(im1_dir, _) for _ in list_im1_] list_im2 = [os.path.join(im2_dir, _) for _ in list_im1_] with tf.variable_scope('load_image'): output = tf.train.slice_input_producer([list_im1, list_im2], shuffle=False, capacity=name_queue_capacity) print("output : ", output) # Reading and decode the images reader = tf.WholeFileReader(name='image_reader') image1 = tf.read_file(output[0]) image2 = tf.read_file(output[1]) image1 = tf.image.decode_png(image1, channels=3) image2 = tf.image.decode_png(image2, channels=3) image1 = tf.image.convert_image_dtype(image1, dtype=tf.float32) image2 = tf.image.convert_image_dtype(image2, dtype=tf.float32) image1.set_shape([h, w, 3]) image2.set_shape([h, w, 3]) paths_im1_batch, paths_im2_batch, im1_batch, im2_batch = tf.train.shuffle_batch( [output[0], output[1], image1, image2], batch_size=batch_size, capacity=image_queue_capacity + 4 * batch_size, min_after_dequeue = image_queue_capacity, num_threads = 4) # 运行Graph with tf.Session() as sess: coord = tf.train.Coordinator() # 创建一个协调器,管理线程 threads = tf.train.start_queue_runners(coord=coord) # 启动QueueRunner, 此时文件名队列已经进队。 for i in range(6): path_im1, path_im2, im1, im2 = sess.run([paths_im1_batch, paths_im2_batch, im1_batch, im2_batch]) print(path_im1) print(path_im2) # fig = plt.figure(figsize=(2, 2), dpi=200) # plt.imshow(e_val[0]) # plt.show() coord.request_stop() coord.join(threads)
有监督学习的 图像-标签对 数据读入(图像分类、检测)
import tensorflow as tf
import os
import matplotlib.pyplot as plt
import collections
'''
将图像对按照完全相同的文件名存放在两个不同的文件夹下
'''
im1_dir = 'D:\WorkSpace\DATA\marble130_pair'
im2_dir = 'D:\WorkSpace\DATA\marble130_pair1'
h = 1024
w = 1024
batch_size = 1
name_queue_capacity = 128
image_queue_capacity = 128
Data = collections.namedtuple('Data', 'path_im1, path_im2, im1, im2, count')
list_im1 = os.listdir(im1_dir)
list_im1 = [_ for _ in list_im1 if _.endswith('.png')]
if len(list_im1) == 0:
raise Exception('No png files in the im1 directory')
list_im1_ = sorted(list_im1)
list_im1 = [os.path.join(im1_dir, _) for _ in list_im1_]
list_im2 = [os.path.join(im2_dir, _) for _ in list_im1_]
with tf.variable_scope('load_image'):
output = tf.train.slice_input_producer([list_im1, list_im2],
shuffle=False, capacity=name_queue_capacity)
print("output : ", output)
# Reading and decode the images
reader = tf.WholeFileReader(name='image_reader')
image1 = tf.read_file(output[0])
image2 = tf.read_file(output[1])
image1 = tf.image.decode_png(image1, channels=3)
image2 = tf.image.decode_png(image2, channels=3)
image1 = tf.image.convert_image_dtype(image1, dtype=tf.float32)
image2 = tf.image.convert_image_dtype(image2, dtype=tf.float32)
image1.set_shape([h, w, 3])
image2.set_shape([h, w, 3])
paths_im1_batch, paths_im2_batch, im1_batch, im2_batch = tf.train.shuffle_batch(
[output[0], output[1], image1, image2],
batch_size=batch_size, capacity=image_queue_capacity + 4 * batch_size,
min_after_dequeue = image_queue_capacity, num_threads = 4)
# 运行Graph
with tf.Session() as sess:
coord = tf.train.Coordinator() # 创建一个协调器,管理线程
threads = tf.train.start_queue_runners(coord=coord) # 启动QueueRunner, 此时文件名队列已经进队。
for i in range(6):
path_im1, path_im2, im1, im2 = sess.run([paths_im1_batch, paths_im2_batch, im1_batch, im2_batch])
print(path_im1)
print(path_im2)
# fig = plt.figure(figsize=(2, 2), dpi=200)
# plt.imshow(e_val[0])
# plt.show()
coord.request_stop()
coord.join(threads)