github/tensorflow/models/tutorials/image/cifar10/cifar10_input.py

Posted estellellll

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了github/tensorflow/models/tutorials/image/cifar10/cifar10_input.py相关的知识,希望对你有一定的参考价值。

# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

"""解码CIFAR-10二进制文件"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os

from six.moves import xrange  
import tensorflow as tf

# 处理图像为这个大小。注意这与原始的CIFAR图像大小32*32不同。 如果改变这个数字,那么整个模型结构会随之改变并需要重新训练。
IMAGE_SIZE = 24

# 描述CIFAR-10数据集的全局常量。
NUM_CLASSES = 10
NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN = 50000
NUM_EXAMPLES_PER_EPOCH_FOR_EVAL = 10000


def read_cifar10(filename_queue):
  """从CIFAR10数据文件中阅读并解析example。
  推荐:如果你想要N路并行阅读,那么调用这个函数N次。这样会返回N个独立的Reader用来阅读那些文件里不同的文件和位置,这样会返回更好的混合example。
  参数:
    filename_queue: 文件名字符串队列。
  返回:
    一个object代表一个example,包括以下内容:
      高: result的行数(32)
      宽: result的列数(32)
      深: result的色彩通道数(3)
      key: 一个标量字符串描述这个example的文件名和record number。
      标签: 一个带有标签(0..9)的int32 Tensor
      uint8image: 一个带有图像数据的[height, width, depth] uint8 Tensor
  """

  class CIFAR10Record(object):
    pass
  result = CIFAR10Record()

  # CIFAR-10数据集中图像的维度。
  label_bytes = 1  
  result.height = 32
  result.width = 32
  result.depth = 3
  image_bytes = result.height * result.width * result.depth
  # 每一个record包含一个标签和一个固定长度的用来描述图像的bytes。
  record_bytes = label_bytes + image_bytes

  # 阅读一个record,从filename_queue中获得filename。CIFAR-10格式没有header何footer,所以我们默认header——bytes和footer_bytes为0。
  reader = tf.FixedLengthRecordReader(record_bytes=record_bytes)
  result.key, value = reader.read(filename_queue)

  # 将一个字符串转换成一个uint8向量
  record_bytes = tf.decode_raw(value, tf.uint8)

  # 第一个bytes代表标签,我们将它转换成int32。
  result.label = tf.cast(
      tf.strided_slice(record_bytes, [0], [label_bytes]), tf.int32)

  # 剩下的bytes代表图像,我们将它reshape成[depth, height, width]。
  depth_major = tf.reshape(
      tf.strided_slice(record_bytes, [label_bytes],
                       [label_bytes + image_bytes]),
      [result.depth, result.height, result.width])
  # 转换成[height, width, depth]
  result.uint8image = tf.transpose(depth_major, [1, 2, 0])

  return result


def _generate_image_and_label_batch(image, label, min_queue_examples,
                                    batch_size, shuffle):
  """生成一个图像和标签batch队列。
  Args:
    image: float32类型的[height, width, 3]Tensor
    label: int32类型的Tensor
    min_queue_examples: int32,保留在队列中的samples的最小数量,用来提供example batch。
    batch_size: 每个batch的图像数量。
    shuffle: boolean 表示是否打乱队列。 
  Returns:
    images: Images. [batch_size, height, width, 3] tensor
    labels: Labels. [batch_size] tensor
  """
  # 创建一个队列来打乱example,然后阅读‘batch_size‘ images + labels
  num_preprocess_threads = 16
  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)

  # 在visualizer中展示训练图像。
  tf.summary.image(images, images)

  return images, tf.reshape(label_batch, [batch_size])


def distorted_inputs(data_dir, batch_size):
  """Construct distorted input for CIFAR training using the Reader ops.
  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 not tf.gfile.Exists(f):
      raise ValueError(Failed to find file:  + f)

  # Create a queue that produces the filenames to read.
  filename_queue = tf.train.string_input_producer(filenames)

  with tf.name_scope(data_augmentation):
    # 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 training the network. Note 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, consider randomizing
    # the order their operation.
    # NOTE: since per_image_standardization zeros the mean and makes
    # the stddev unit, this likely has no effect see tensorflow#1458.
    distorted_image = tf.image.random_brightness(distorted_image,
                                                 max_delta=63)
    distorted_image = tf.image.random_contrast(distorted_image,
                                               lower=0.2, upper=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])

    # 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 starting to train. 
           This will take a few minutes. % min_queue_examples)

  # 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=True)


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.Exists(f):
      raise ValueError(Failed to find file:  + f)

  with tf.name_scope(input):
    # Create a queue that 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 central [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 the 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)

 

以上是关于github/tensorflow/models/tutorials/image/cifar10/cifar10_input.py的主要内容,如果未能解决你的问题,请参考以下文章