cifar10_preprocessing.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. """Provides utilities to preprocess images.
  16. The preprocessing steps for VGG were introduced in the following technical
  17. report:
  18. Very Deep Convolutional Networks For Large-Scale Image Recognition
  19. Karen Simonyan and Andrew Zisserman
  20. arXiv technical report, 2015
  21. PDF: http://arxiv.org/pdf/1409.1556.pdf
  22. ILSVRC 2014 Slides: http://www.robots.ox.ac.uk/~karen/pdf/ILSVRC_2014.pdf
  23. CC-BY-4.0
  24. More information can be obtained from the VGG website:
  25. www.robots.ox.ac.uk/~vgg/research/very_deep/
  26. """
  27. from __future__ import absolute_import
  28. from __future__ import division
  29. from __future__ import print_function
  30. import tensorflow as tf
  31. _PADDING = 2
  32. slim = tf.contrib.slim
  33. def preprocess_for_train(image,
  34. output_height,
  35. output_width,
  36. padding=_PADDING):
  37. """Preprocesses the given image for training.
  38. Note that the actual resizing scale is sampled from
  39. [`resize_size_min`, `resize_size_max`].
  40. Args:
  41. image: A `Tensor` representing an image of arbitrary size.
  42. output_height: The height of the image after preprocessing.
  43. output_width: The width of the image after preprocessing.
  44. padding: The amound of padding before and after each dimension of the image.
  45. Returns:
  46. A preprocessed image.
  47. """
  48. padded_image = tf.pad(image, [[padding, padding], [padding, padding], [0, 0]])
  49. # Randomly crop a [height, width] section of the image.
  50. distorted_image = tf.random_crop(padded_image,
  51. [output_height, output_width, 3])
  52. # Randomly flip the image horizontally.
  53. distorted_image = tf.image.random_flip_left_right(distorted_image)
  54. # Because these operations are not commutative, consider randomizing
  55. # the order their operation.
  56. distorted_image = tf.image.random_brightness(distorted_image,
  57. max_delta=63)
  58. distorted_image = tf.image.random_contrast(distorted_image,
  59. lower=0.2, upper=1.8)
  60. # Subtract off the mean and divide by the variance of the pixels.
  61. return tf.image.per_image_whitening(distorted_image)
  62. def preprocess_for_eval(image, output_height, output_width):
  63. """Preprocesses the given image for evaluation.
  64. Args:
  65. image: A `Tensor` representing an image of arbitrary size.
  66. output_height: The height of the image after preprocessing.
  67. output_width: The width of the image after preprocessing.
  68. Returns:
  69. A preprocessed image.
  70. """
  71. resized_image = tf.image.resize_image_with_crop_or_pad(image,
  72. output_width,
  73. output_height)
  74. # Subtract off the mean and divide by the variance of the pixels.
  75. return tf.image.per_image_whitening(resized_image)
  76. def preprocess_image(image, output_height, output_width, is_training=False):
  77. """Preprocesses the given image.
  78. Args:
  79. image: A `Tensor` representing an image of arbitrary size.
  80. output_height: The height of the image after preprocessing.
  81. output_width: The width of the image after preprocessing.
  82. is_training: `True` if we're preprocessing the image for training and
  83. `False` otherwise.
  84. Returns:
  85. A preprocessed image.
  86. """
  87. if is_training:
  88. return preprocess_for_train(image, output_height, output_width)
  89. else:
  90. return preprocess_for_eval(image, output_height, output_width)