lenet_preprocessing.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 for preprocessing."""
  16. from __future__ import absolute_import
  17. from __future__ import division
  18. from __future__ import print_function
  19. import tensorflow as tf
  20. slim = tf.contrib.slim
  21. def preprocess_image(image, output_height, output_width, is_training):
  22. """Preprocesses the given image.
  23. Args:
  24. image: A `Tensor` representing an image of arbitrary size.
  25. output_height: The height of the image after preprocessing.
  26. output_width: The width of the image after preprocessing.
  27. is_training: `True` if we're preprocessing the image for training and
  28. `False` otherwise.
  29. Returns:
  30. A preprocessed image.
  31. """
  32. image = tf.to_float(image)
  33. image = tf.image.resize_image_with_crop_or_pad(
  34. image, output_width, output_height)
  35. image = tf.subtract(image, 128.0)
  36. image = tf.div(image, 128.0)
  37. return image