cifarnet_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 in CIFAR-10.
  16. """
  17. from __future__ import absolute_import
  18. from __future__ import division
  19. from __future__ import print_function
  20. import tensorflow as tf
  21. _PADDING = 4
  22. slim = tf.contrib.slim
  23. def preprocess_for_train(image,
  24. output_height,
  25. output_width,
  26. padding=_PADDING):
  27. """Preprocesses the given image for training.
  28. Note that the actual resizing scale is sampled from
  29. [`resize_size_min`, `resize_size_max`].
  30. Args:
  31. image: A `Tensor` representing an image of arbitrary size.
  32. output_height: The height of the image after preprocessing.
  33. output_width: The width of the image after preprocessing.
  34. padding: The amound of padding before and after each dimension of the image.
  35. Returns:
  36. A preprocessed image.
  37. """
  38. tf.summary.image('image', tf.expand_dims(image, 0))
  39. # Transform the image to floats.
  40. image = tf.to_float(image)
  41. if padding > 0:
  42. image = tf.pad(image, [[padding, padding], [padding, padding], [0, 0]])
  43. # Randomly crop a [height, width] section of the image.
  44. distorted_image = tf.random_crop(image,
  45. [output_height, output_width, 3])
  46. # Randomly flip the image horizontally.
  47. distorted_image = tf.image.random_flip_left_right(distorted_image)
  48. tf.summary.image('distorted_image', tf.expand_dims(distorted_image, 0))
  49. # Because these operations are not commutative, consider randomizing
  50. # the order their operation.
  51. distorted_image = tf.image.random_brightness(distorted_image,
  52. max_delta=63)
  53. distorted_image = tf.image.random_contrast(distorted_image,
  54. lower=0.2, upper=1.8)
  55. # Subtract off the mean and divide by the variance of the pixels.
  56. return tf.image.per_image_standardization(distorted_image)
  57. def preprocess_for_eval(image, output_height, output_width):
  58. """Preprocesses the given image for evaluation.
  59. Args:
  60. image: A `Tensor` representing an image of arbitrary size.
  61. output_height: The height of the image after preprocessing.
  62. output_width: The width of the image after preprocessing.
  63. Returns:
  64. A preprocessed image.
  65. """
  66. tf.summary.image('image', tf.expand_dims(image, 0))
  67. # Transform the image to floats.
  68. image = tf.to_float(image)
  69. # Resize and crop if needed.
  70. resized_image = tf.image.resize_image_with_crop_or_pad(image,
  71. output_width,
  72. output_height)
  73. tf.summary.image('resized_image', tf.expand_dims(resized_image, 0))
  74. # Subtract off the mean and divide by the variance of the pixels.
  75. return tf.image.per_image_standardization(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)