This part of the code needs to be focused on how to build the queue and read the data from it.
From __future__ import Absolute_import to
__future__ Import division from
__future__ import print_function
import OS from
six.moves import xrange # pylint:disable=redefined-builtin
import TensorFlow as TF
# Process Images of this size. This is the differs from the original Cifar
# image size of x 32. If one alters this number, then the entire model # architecture'll change and any
model would need to be retrained.< C9/>image_size =
# Global Constants describing the CIFAR-10 data set.
Num_classes = Ten
Num_examples_per_epoch_for_train = 50000
Num_examples_per_epoch_for_eval = 10000
First look at the definition and description of the function, input is filename_queue, output is a single image and its label. Note also prompts you to have n-way parallel reads, you can call the function n times, so that read more disorderly order.
def read_cifar10 (filename_queue): "" "
reads and parses examples from CIFAR10 data files.
Recommendation:if you want n-way read parallelism, call this function
N times. This is give you N independent Readers reading different
files & positions within files, those'll which B Etter mixing of
examples.
Args:
filename_queue:a Queue of strings with the filenames to read from.
Returns: An
object representing a single example, with the following fields:
Height:number of rows in the (a)
Width:number of columns in (i) Depth:number of
color channels in the result (3) key:a scalar
stri ng Tensor describing the filename & record number of this
example.
Label:an int32 Tensor with the label in the range 0..9.
uint8image:a [Height, width, depth] uint8 Tensor with the image data ""
The result record stores the information to be exported.
Class Cifar10record (object): Pass result
= Cifar10record ()
# Dimensions of the images in the CIFAR-10 dataset .
# http://www.cs.toronto.edu/~kriz/cifar.html for a description of the
# input format.
Label_bytes = 1 # 2 for CIFAR-100
result.height =
Result.width =
result.depth = 3
image_bytes = Result.height * Result.width * result.depth
# Every record consists of a label followed by the image, with a
# fixed number of bytes for each.
Record_bytes = label_bytes + image_bytes
The following code is important and is a common way to generate batch with queues. A reader (here the reader is fixed), and then read the file name of a single file and its corresponding data (key, value) from the queue.
# Read A, getting filenames from the filename_queue. No
# Header or footer in the CIFAR-10 format, so we leave Header_bytes
# and Footer_bytes at their default of 0.
reader = tf. Fixedlengthrecordreader (record_bytes=record_bytes)
result.key, value = Reader.read (filename_queue)
The read data is processed below to produce the desired form (image, label) and ultimately result.
# Convert from ' A string to a vector of ' uint8 ' is record_bytes long.
Record_bytes = Tf.decode_raw (value, tf.uint8)
# The bytes represent the label, which we convert from Uint8->i Nt32.
Result.label = Tf.cast (
tf.strided_slice (Record_bytes, [0], [label_bytes]), Tf.int32)
# The remaining bytes After the label represent the image, which we reshape
# from [depth * height * width] to [depth, height, width].
Depth_major = Tf.reshape (
tf.strided_slice (record_bytes, [label_bytes],
[Label_bytes + image_bytes]),
[Result.depth, Result.height, Result.width])
# Convert from [depth, height, width] to [height, width, depth].
Result.uint8image = Tf.transpose (Depth_major, [1, 2, 0]) return result
The following function uses a queue to generate batch.
def _generate_image_and_label_batch (image, label, Min_queue_examples,
batch_size, Shuffle): "" "
construct a Queued batch of images and labels.
Args:
image:3-d Tensor of [Height, width, 3] of Type.float32.
label:1-d Tensor of Type.int32
Min_queue_examples:int32, minimum number of samples to retain in the
queue that P Rovides of batches of examples.
Batch_size:number of images per batch.
Shuffle:boolean indicating whether to use a shuffling queue.
Returns:
images:images 4D tensor of [batch_size, Height, width, 3] size.
Labels:labels. 1D tensor of [batch_size] size. "" "
Tf.train.shuffle_batch/tf.train.batch from queue/Direct fetch batch, although input [image, label] refers to the need for batch tensor at the same time [image, label] also contains Data processing, so the removed is handled by the batch. For an explanation of queue, see here.
# Create A queue that shuffles the examples, and then
# read ' batch_size ' images + labels to the example queue.
Num_preprocess_threads =
If shuffle:
images, Label_batch = Tf.train.shuffle_batch (
[Image, label],
batch_size=batch_size,
num_threads=num_preprocess_threads,
capacity=min_queue_examples + 3 * batch_ Size,
min_after_dequeue=min_queue_examples)
else:
images, Label_batch = Tf.train.batch (
[Image , label],
batch_size=batch_size,
num_threads=num_preprocess_threads,
capacity=min_queue_examples + 3 * batch_size)
# Display The training images in the visualizer.
Tf.summary.image (' images ', images) return
images, Tf.reshape (Label_batch, [batch_size])
This function generates a distorted image to augment the training set. Tf.train.string_input_producer (filename) generates a file name queue for reading data.
def distorted_inputs (Data_dir, batch_size): ""
"construct distorted input for CIFAR using the Reader training >args:
Data_dir:path to the CIFAR-10 data directory.
Batch_size:number of images per batch.
Returns:
images:images 4D tensor of [Batch_size, Image_size, Image_size, 3] size.
Labels:labels. 1D tensor of [batch_size] size.
"" " filenames = [Os.path.join (Data_dir, ' data_batch_%d.bin '% i) for I-in
-xrange (1, 6)] for
F in filenames:
if n OT tf.gfile.Exists (f):
raise ValueError (' Failed to find file: ' + F ')
# Create A queue that produces the filename s to read.
Filename_queue = Tf.train.string_input_producer (filenames)
# Read examples from to files in the filename queue.
Read_input = Read_cifar10 (filename_queue)
reshaped_image = Tf.cast (Read_input.uint8image, Tf.float32)
Distort the original image.
Height = image_size width = image_size # IMAGE processing for training the network.
The many random # distortions applied to the image.
# randomly crop a [height, Width] section of the image.
Distorted_image = Tf.random_crop (reshaped_image, [Height, width, 3]) # Randomly flip the image horizontally. Distorted_image = Tf.image.random_flip_left_right (distorted_image) # Because these operations are not commutative, cons
Ider Randomizing # The order their operation.
Distorted_image = Tf.image.random_brightness (Distorted_image, max_delta=63) Distorted_image = Tf.image.random_contrast (Distorted_image, lower=0.2, UPP
er=1.8) # Subtract off the mean and divide by the variance of the pixels.
Float_image = Tf.image.per_image_standardization (distorted_image) # Set the shapes of tensors. Float_image.set_shape ([Height, width, 3]) Read_input.label.set_shape ([1])
Sets the parameters of the desired queue, which always maintains a minimum number of times and is not read all at a time.
# Ensure that the random shuffling has good mixing properties.
Min_fraction_of_examples_in_queue = 0.4
min_queue_examples = Int (Num_examples_per_epoch_for_train *
min_ Fraction_of_examples_in_queue)
print (' Filling queue with%d Cifar images before to starting. ' This'll
take a few minutes. '% min_queue_examples)
# Generate A batch of images and labels by building up a Q Ueue of examples.
Return _generate_image_and_label_batch (Float_image, Read_input.label,
min_queue_examples, Batch_size,
Shuffle=true)
The following input is basically the same as the distorted_input above.
def inputs (Eval_data, Data_dir, batch_size): "" "construct input for CIFAR evaluation using the Reader OPS.
Args:eval_data:bool, indicating if one should use the train or eval data set.
Data_dir:path to the CIFAR-10 data directory.
Batch_size:number of images per batch. Returns:images:Images.
4D tensor of [Batch_size, Image_size, Image_size, 3] size. Labels:labels.
1D tensor of [batch_size] size. "" If not eval_data:filenames = [Os.path.join (Data_dir, ' data_batch_%d.bin '% i) for I in Xrange ( 1, 6)] Num_examples_per_epoch = Num_examples_per_epoch_for_train Else:filenames = [Os.path.join (data_dir, ' test _batch.bin ')] Num_examples_per_epoch = Num_examples_per_epoch_for_eval for f in filenames:if not Tf.gfile.Exis
TS (f): Raise ValueError (' Failed to find file: ' + F ') # Create a \ Produces the filenames to read. Filename_queue = Tf.train.string_input_producer (filenames) # Read examples from Files in the filename queue. Read_input = Read_cifar10 (filename_queue) reshaped_image = Tf.cast (read_input.uint8image, tf.float32) height = IMAGE_
SIZE width = image_size # IMAGE processing for evaluation.
# crop The "the" (height, width) of the image.
Resized_image = Tf.image.resize_image_with_crop_or_pad (Reshaped_image,
Height, width) # Subtract off the mean and divide by the variance of the pixels.
Float_image = Tf.image.per_image_standardization (resized_image) # Set the shapes of tensors. Float_image.set_shape ([Height, width, 3]) Read_input.label.set_shape ([1]) # Ensure that random shuffling has good
Mixing properties. Min_fraction_of_examples_in_queue = 0.4 Min_queue_examples = Int (Num_examples_per_epoch * min
_fraction_of_examples_in_queue) # Generate A batch of images and labels by building up a queue of examples. Return _generate_image_and_label_batch(Float_image, Read_input.label, Min_queue_examples, Batch_size, Shuffle=false)