2、tensorflow读取数据、形成batch、显示数据

1、变量常量输入

x = tf.Variable([1,2])

b = tf.Constant([3,3])

2、placeholder输入

    1     # 定义两个placeholder
    2     x = tf.placeholder(tf.float32,[None,784])
    3     y = tf.placeholder(tf.float32,[None,10])
dtype = tf.float32:
    一般dtype要去输入的格式,如果赋值的不是这种格式,就会出错误。
    但是这里是tf的入口,所以无论什么格式,只要能转换成这种类型即可
    输入可以是np格式的

3、使用内存队列、不使用文件队列

queue:定义一个队列,文件队列tf自己会定义,自己定义的是内存队列
enqueue:定义输入队列的操作,  传入一个批次或者多个批次的数据,这个就没有使用文件名列表
dequeue:定义输出队列的操作
train_batch:这个利用dequeue获取一个批次的数据,传入网络

1、定义文件队列-数据
input_queue=data_flow_ops.FIFOQueue(capacity=100000,
dtypes=[tf.string,tf.int64],
shapes=[(3,),(3,)],
shared_name=None,name=None)
2、定义文件队列的输入操作
enqueue_op=input_queue.enqueue_many([image_paths_placeholder,labels_placeholder])
文件队列的输出和内存队列的输入可以不用定义
3、定义文件队列队列的输出操作
filenames,label=input_queue.dequeue()
4、利用文件队列的输出操作获取一个批量的数据,内存队列的输入操作
这是一个内存队列
image_batch,labels_batch=tf.train.batch_join(
images_and_labels,#将这个整体的第一维度进行差分
batch_size=batch_size_placeholder,#出队的时候的大小
shapes=[(args.image_size,args.image_size,3),()],
enqueue_many=True,#表示image_batch和labels_batch同时入队
capacity=4*nrof_preprocess_threads*args.batch_size,
allow_smaller_final_batch=True)
5、内存队列的输出操作时自动训练的时候自动获取的
6、之前都是定义的队列的操作,这个相当于阀门的打开,数据开始流入
tf.train.start_queue_runners(coord=coord,sess=sess)

4、使用内存队列 和 文件队列、里面是imageDirList

#将一系列的图片的绝对路径组成list
def GetImageDirList():
    pass
    return ImageDirList

#对于分类的任务,lable就是每个类别的号码
def GetLabelList():
    pass
    return labelList

#根据路径可以
def GetBatch(ImageDirList, labelList):
    #第一个是我们的原始图片
    imagesTensor = tf.convert_to_tensor(imageList, dtype=tf.string)
    #如果进行实力分割,这个就是我们的label,也就是说,一个batch里面可以有两组图片
    imagesMaskTensor = tf.convert_to_tensor(imageMaskList, dtype=tf.string)
    #batch里面还可以有数字
    label0Tensor = tf.convert_to_tensor(label0List, dtype=tf.int32)
    label1Tensor = tf.convert_to_tensor(label1List, dtype=tf.int32)

    #形成文件队列
    queue = tf.train.slice_input_producer(
        [imagesTensor, imagesMaskTensor, label0Tensor, label1Tensor])

    # 提取图片内容和标签内容,一定注意数据之间的转化;
    image_content0 = tf.read_file(queue[0])
    imageData0 = tf.image.decode_jpeg(image_content, channels=3)  # channels必须要制定,当时没指定,程序报错
    imageData0 = tf.image.convert_image_dtype(imageData0, tf.uint8)  # 图片数据进行转化,此处为了显示而转化
    image_content1 = tf.read_file(queue[1])
    imageData1 = tf.image.decode_jpeg(image_content, channels=3)  # channels必须要制定,当时没指定,程序报错
    imageData1 = tf.image.convert_image_dtype(imageData0, tf.uint8)  # 图片数据进行转化,此处为了显示而转化
    label0Data = tf.cast(queue[1], tf.int32)
    label1Data = tf.cast(queue[2], tf.int32)

    # resieze
    new_size = tf.constant([imgHeight, imgWidth], dtype=tf.int32)
    image0 = tf.image.resize_images(imageData0, new_size)

    # 这是数据提取关键,因为设置了batch_size,决定了每次提取数据的个数,比如此处是3,则每次为3个文件
    imageBatch, label0Batch, label1Batch, label2Batch, label3Batch, label4Batch = \
        tf.train.shuffle_batch([image0, label0Data, label1Data],
                               batch_size=batchSize,
                               capacity=2000, 
                               min_after_dequeue=1000)

    return imageBatch, label0Batch, label1Batch
    

5、使用内存队列 和 文件队列、里面是tfrecord

• string-into producer:这个传输的是文件名这样的列表,而上一个是多个批次的数据,这个相当于是两步
• 这个使用了文件名列表专门的reader
reader = tf.WholeFileReader()不同的reader对应不同的文件结构
• train_batch:这个利用dequeue获取一个批次的数据,传入网络


文件用不用入队,直接用reader出队。而数据需要全部进行
1、指定文件队列-文件,这个就相当于前面的第一步和第二步,这一步文件队列入队操作已经完成。
filename_queue = tf.train.string_input_producer(tfrecord_dir_list, shuffle = True) 
2、不同的文件有不同的reader,文件队列的输出操作
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
image_features = tf.parse_single_example(serialized_example,    
features = {
       'image/encoded':tf.FixedLenFeature([],tf.string),
      'image/roi':tf.FixedLenFeature([4], tf.float32),
      'image/landmark':tf.FixedLenFeature([10],tf.float32)
          })    
3、放入内存队列入队,使用的时候是出队。内存队列的输入操作    
image, label, roi ,mark = tf.train.batch(    
         [image, label, roi, mark],
         batch_size = batch_size,#从队列中获取的出队列的数量
         num_threads = 2,#入队线程的限制
         capacity = 1 * batch_size#设置队列的最大数量
    )
4、内存队列的输出操作时自动训练的时候自动获取的    
6、之前都是定义的队列的操作,这个相当于阀门的打开,数据开始流入
tf.train.start_queue_runners(coord=coord,sess=sess)

sess.中显示数据

import tensorflow as tf

# Fetch:可以在session中同时计算多个tensor或执行多个操作
# 定义三个常量
input1 = tf.constant(3.0)
input2 = tf.constant(2.0)
input3 = tf.constant(5.0)
# 加法op
add = tf.add(input2,input3)
# 乘法op
mul = tf.multiply(input1, add)

with tf.Session() as sess:
#sess.run([ ]),列表里面就可以放置很多输出。这样就可以一步获得多个输出。
    result1,result2 = sess.run([mul, add])
    print(result1,result2)
# Feed:先定义占位符,等需要的时候再传入数据
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
# 乘法op
output = tf.multiply(input1, input2)

with tf.Session() as sess:
#feed_dict():用字典的方式,进行输出所需要的输入的提供
    print(sess.run(output, feed_dict={input1:8.0,input2:2.0}))