Python 3.x 正在进行什么tf.train.shuffle\u batch()

Python 3.x 正在进行什么tf.train.shuffle\u batch(),python-3.x,tensorflow,Python 3.x,Tensorflow,我对tensorflow框架非常陌生,我尝试了这段代码来阅读和探索CIFAR-10数据集 import tensorflow as tf import numpy as np import os import matplotlib.pyplot as plt sess=tf.Session() batch_size = 128 output_every = 50 generations = 20000 eval_every = 500 image_height = 32 image_widt

我对tensorflow框架非常陌生,我尝试了这段代码来阅读和探索CIFAR-10数据集

import tensorflow as tf
import numpy as np
import os
import matplotlib.pyplot as plt

sess=tf.Session()

batch_size = 128
output_every = 50
generations = 20000
eval_every = 500
image_height = 32
image_width = 32
crop_height = 24
crop_width = 24
num_channels = 3
num_targets = 10
data_dir="CIFAR10"


image_vec_length = image_height * image_width * num_channels
record_length = 1 + image_vec_length

def read_cifar_files(filename_queue, distort_images = True):
   reader = tf.FixedLengthRecordReader(record_bytes=record_length*10)
   key, record_string = reader.read(filename_queue)
   record_bytes = tf.decode_raw(record_string, tf.uint8)

# Extract label
   image_label = tf.cast(tf.slice(record_bytes, [image_vec_length-1],[1]),tf.int32)

# Extract image
   sliced=tf.slice(record_bytes, [0],[image_vec_length])
   image_extracted = tf.reshape(sliced, [num_channels, image_height,image_width])

# Reshape image
   image_uint8image = tf.transpose(image_extracted, [1, 2, 0])
   reshaped_image = tf.cast(image_uint8image, tf.float32)

# Randomly Crop image
   final_image = tf.image.resize_image_with_crop_or_pad(reshaped_image, crop_width, crop_height)
   if distort_images:

# Randomly flip the image horizontally, change the brightness and contrast
     final_image = tf.image.random_flip_left_right(final_image)
     final_image = tf.image.random_brightness(final_image,max_delta=63)
     final_image = tf.image.random_contrast(final_image,lower=0.2, upper=1.8)

# standardization
     final_image = tf.image.per_image_standardization(final_image)
     return  final_image, image_label
当我在没有tf.train.shuffle\u batch()的情况下运行下面的input_pipeline()函数时,它会给我一个带形状(24,24,3)的图像张量

但当我用tf.train.shuffle\u batch()函数运行相同的input\u pipeline()函数时,它会给我图像张量,其中包含128个具有形状的图像(128、24、24、3)


这怎么可能呢。似乎tf.train.shuffle\u batch()从read\u cifar\u files()中获取一个图像张量,并返回有128个图像的张量。那么tf.train.shuffle\u batch()函数的作用是什么呢。

在Tensorflow中,张量只是图形的一个节点。函数tf.train.shuffle_batch()将两个节点作为输入,它们通过图形连接到数据

因此,它不是以“单个图像”作为输入,而是一个能够加载图像的图形。然后,它向图中添加一个新操作,该操作将在输入图的时间执行n=batch\u size,洗牌该批并返回大小为[bach\u size,input\u shape]的输出张量

然后,当您在会话中运行该函数时,数据将根据图表加载,这意味着每次调用tf.train.shuffle\u batch(),您都将在磁盘上读取n=batch\u大小的图像

def input_pipeline(batch_size, train_logical=True):
    files=[os.path.join(data_dir,"data_batch_{}.bin".format(i)) for i in range(1,6)]
    filename_queue = tf.train.string_input_producer(files)
    image,label = read_cifar_files(filename_queue)
    return(image,label)


example_batch,label_batch=input_pipeline(batch_size)
threads = tf.train.start_queue_runners(sess=sess)
img,label=sess.run([example_batch, label_batch])

#output=(24,24,3) 
print(img.shape) 
def input_pipeline(batch_size, train_logical=True):
    files=[os.path.join(data_dir,"data_batch_{}.bin".format(i)) for i in range(1,6)]
    filename_queue = tf.train.string_input_producer(files)
    image,label = read_cifar_files(filename_queue)

    min_after_dequeue = 1000
    capacity = min_after_dequeue + 3 * batch_size
    example_batch, label_batch = tf.train.shuffle_batch([image,label], batch_size, capacity, min_after_dequeue)
    return(example_batch, label_batch)