preprocessing_factory.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. """Contains a factory for building various models."""
  16. from __future__ import absolute_import
  17. from __future__ import division
  18. from __future__ import print_function
  19. import tensorflow as tf
  20. from preprocessing import cifarnet_preprocessing
  21. from preprocessing import inception_preprocessing
  22. from preprocessing import lenet_preprocessing
  23. from preprocessing import vgg_preprocessing
  24. slim = tf.contrib.slim
  25. def get_preprocessing(name, is_training=False):
  26. """Returns preprocessing_fn(image, height, width, **kwargs).
  27. Args:
  28. name: The name of the preprocessing function.
  29. is_training: `True` if the model is being used for training and `False`
  30. otherwise.
  31. Returns:
  32. preprocessing_fn: A function that preprocessing a single image (pre-batch).
  33. It has the following signature:
  34. image = preprocessing_fn(image, output_height, output_width, ...).
  35. Raises:
  36. ValueError: If Preprocessing `name` is not recognized.
  37. """
  38. preprocessing_fn_map = {
  39. 'cifarnet': cifarnet_preprocessing,
  40. 'inception': inception_preprocessing,
  41. 'inception_v1': inception_preprocessing,
  42. 'inception_v2': inception_preprocessing,
  43. 'inception_v3': inception_preprocessing,
  44. 'inception_v4': inception_preprocessing,
  45. 'inception_resnet_v2': inception_preprocessing,
  46. 'lenet': lenet_preprocessing,
  47. 'resnet_v1_50': vgg_preprocessing,
  48. 'resnet_v1_101': vgg_preprocessing,
  49. 'resnet_v1_152': vgg_preprocessing,
  50. 'resnet_v2_50': vgg_preprocessing,
  51. 'resnet_v2_101': vgg_preprocessing,
  52. 'resnet_v2_152': vgg_preprocessing,
  53. 'vgg': vgg_preprocessing,
  54. 'vgg_a': vgg_preprocessing,
  55. 'vgg_16': vgg_preprocessing,
  56. 'vgg_19': vgg_preprocessing,
  57. }
  58. if name not in preprocessing_fn_map:
  59. raise ValueError('Preprocessing name [%s] was not recognized' % name)
  60. def preprocessing_fn(image, output_height, output_width, **kwargs):
  61. return preprocessing_fn_map[name].preprocess_image(
  62. image, output_height, output_width, is_training=is_training, **kwargs)
  63. return preprocessing_fn