|
| 1 | +# Copyright 2016 The TensorFlow Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================== |
| 15 | + |
| 16 | +"""CIFAR dataset input module. |
| 17 | +""" |
| 18 | + |
| 19 | +import tensorflow as tf |
| 20 | + |
| 21 | + |
| 22 | +def build_input(dataset, data_path, batch_size, mode): |
| 23 | + """Build CIFAR image and labels. |
| 24 | +
|
| 25 | + Args: |
| 26 | + dataset: Either 'cifar10' or 'cifar100'. |
| 27 | + data_path: Filename for data. |
| 28 | + batch_size: Input batch size. |
| 29 | + mode: Either 'train' or 'eval'. |
| 30 | + Returns: |
| 31 | + images: Batches of images. [batch_size, image_size, image_size, 3] |
| 32 | + labels: Batches of labels. [batch_size, num_classes] |
| 33 | + Raises: |
| 34 | + ValueError: when the specified dataset is not supported. |
| 35 | + """ |
| 36 | + image_size = 32 |
| 37 | + if dataset == 'cifar10': |
| 38 | + label_bytes = 1 |
| 39 | + label_offset = 0 |
| 40 | + num_classes = 10 |
| 41 | + elif dataset == 'cifar100': |
| 42 | + label_bytes = 1 |
| 43 | + label_offset = 1 |
| 44 | + num_classes = 100 |
| 45 | + else: |
| 46 | + raise ValueError('Not supported dataset %s', dataset) |
| 47 | + |
| 48 | + depth = 3 |
| 49 | + image_bytes = image_size * image_size * depth |
| 50 | + record_bytes = label_bytes + label_offset + image_bytes |
| 51 | + |
| 52 | + file_queue = tf.train.string_input_producer([data_path], shuffle=True) |
| 53 | + # Read examples from files in the filename queue. |
| 54 | + reader = tf.FixedLengthRecordReader(record_bytes=record_bytes) |
| 55 | + _, value = reader.read(file_queue) |
| 56 | + |
| 57 | + # Convert these examples to dense labels and processed images. |
| 58 | + record = tf.reshape(tf.decode_raw(value, tf.uint8), [record_bytes]) |
| 59 | + label = tf.cast(tf.slice(record, [label_offset], [label_bytes]), tf.int32) |
| 60 | + # Convert from string to [depth * height * width] to [depth, height, width]. |
| 61 | + depth_major = tf.reshape(tf.slice(record, [label_bytes], [image_bytes]), |
| 62 | + [depth, image_size, image_size]) |
| 63 | + # Convert from [depth, height, width] to [height, width, depth]. |
| 64 | + image = tf.cast(tf.transpose(depth_major, [1, 2, 0]), tf.float32) |
| 65 | + |
| 66 | + if mode == 'train': |
| 67 | + image = tf.image.resize_image_with_crop_or_pad( |
| 68 | + image, image_size+4, image_size+4) |
| 69 | + image = tf.random_crop(image, [image_size, image_size, 3]) |
| 70 | + image = tf.image.random_flip_left_right(image) |
| 71 | + # Brightness/saturation/constrast provides small gains .2%~.5% on cifar. |
| 72 | + # image = tf.image.random_brightness(image, max_delta=63. / 255.) |
| 73 | + # image = tf.image.random_saturation(image, lower=0.5, upper=1.5) |
| 74 | + # image = tf.image.random_contrast(image, lower=0.2, upper=1.8) |
| 75 | + image = tf.image.per_image_whitening(image) |
| 76 | + |
| 77 | + example_queue = tf.RandomShuffleQueue( |
| 78 | + capacity=16 * batch_size, |
| 79 | + min_after_dequeue=8 * batch_size, |
| 80 | + dtypes=[tf.float32, tf.int32], |
| 81 | + shapes=[[image_size, image_size, depth], [1]]) |
| 82 | + num_threads = 16 |
| 83 | + else: |
| 84 | + image = tf.image.resize_image_with_crop_or_pad( |
| 85 | + image, image_size, image_size) |
| 86 | + image = tf.image.per_image_whitening(image) |
| 87 | + |
| 88 | + example_queue = tf.FIFOQueue( |
| 89 | + 3 * batch_size, |
| 90 | + dtypes=[tf.float32, tf.int32], |
| 91 | + shapes=[[image_size, image_size, depth], [1]]) |
| 92 | + num_threads = 1 |
| 93 | + |
| 94 | + example_enqueue_op = example_queue.enqueue([image, label]) |
| 95 | + tf.train.add_queue_runner(tf.train.queue_runner.QueueRunner( |
| 96 | + example_queue, [example_enqueue_op] * num_threads)) |
| 97 | + |
| 98 | + # Read 'batch' labels + images from the example queue. |
| 99 | + images, labels = example_queue.dequeue_many(batch_size) |
| 100 | + labels = tf.reshape(labels, [batch_size, 1]) |
| 101 | + indices = tf.reshape(tf.range(0, batch_size, 1), [batch_size, 1]) |
| 102 | + labels = tf.sparse_to_dense( |
| 103 | + tf.concat(1, [indices, labels]), |
| 104 | + [batch_size, num_classes], 1.0, 0.0) |
| 105 | + |
| 106 | + assert len(images.get_shape()) == 4 |
| 107 | + assert images.get_shape()[0] == batch_size |
| 108 | + assert images.get_shape()[-1] == 3 |
| 109 | + assert len(labels.get_shape()) == 2 |
| 110 | + assert labels.get_shape()[0] == batch_size |
| 111 | + assert labels.get_shape()[1] == num_classes |
| 112 | + |
| 113 | + # Display the training images in the visualizer. |
| 114 | + tf.image_summary('images', images) |
| 115 | + return images, labels |
0 commit comments