preprocessing_factory.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 slim.models import cifar10_preprocessing
  21. from slim.models import inception_preprocessing
  22. from slim.models import lenet_preprocessing
  23. from slim.models 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. 'cifar10': cifar10_preprocessing,
  40. 'inception': inception_preprocessing,
  41. 'inception_v1': inception_preprocessing,
  42. 'inception_v2': inception_preprocessing,
  43. 'inception_v3': inception_preprocessing,
  44. 'lenet': lenet_preprocessing,
  45. 'resnet_v1_50': vgg_preprocessing,
  46. 'resnet_v1_101': vgg_preprocessing,
  47. 'resnet_v1_152': vgg_preprocessing,
  48. 'vgg': vgg_preprocessing,
  49. 'vgg_a': vgg_preprocessing,
  50. 'vgg_16': vgg_preprocessing,
  51. 'vgg_19': vgg_preprocessing,
  52. }
  53. if name not in preprocessing_fn_map:
  54. raise ValueError('Preprocessing name [%s] was not recognized' % name)
  55. def preprocessing_fn(image, output_height, output_width, **kwargs):
  56. return preprocessing_fn_map[name].preprocess_image(
  57. image, output_height, output_width, is_training=is_training, **kwargs)
  58. return preprocessing_fn