preprocessing_factory.py 3.32 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Contains a factory for building various models."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf

23
24
25
26
from preprocessing import cifarnet_preprocessing
from preprocessing import inception_preprocessing
from preprocessing import lenet_preprocessing
from preprocessing import vgg_preprocessing
27
28
29
30

slim = tf.contrib.slim


31
def get_preprocessing(name, is_training=False, use_grayscale=False):
32
33
34
35
36
37
  """Returns preprocessing_fn(image, height, width, **kwargs).

  Args:
    name: The name of the preprocessing function.
    is_training: `True` if the model is being used for training and `False`
      otherwise.
38
    use_grayscale: Whether to convert the image from RGB to grayscale.
39
40
41
42
43
44
45
46
47
48

  Returns:
    preprocessing_fn: A function that preprocessing a single image (pre-batch).
      It has the following signature:
        image = preprocessing_fn(image, output_height, output_width, ...).

  Raises:
    ValueError: If Preprocessing `name` is not recognized.
  """
  preprocessing_fn_map = {
49
      'cifarnet': cifarnet_preprocessing,
50
51
52
53
      'inception': inception_preprocessing,
      'inception_v1': inception_preprocessing,
      'inception_v2': inception_preprocessing,
      'inception_v3': inception_preprocessing,
Alex Kurakin's avatar
Alex Kurakin committed
54
      'inception_v4': inception_preprocessing,
55
      'inception_resnet_v2': inception_preprocessing,
56
      'lenet': lenet_preprocessing,
andrewghoward's avatar
andrewghoward committed
57
      'mobilenet_v1': inception_preprocessing,
58
59
60
      'mobilenet_v2': inception_preprocessing,
      'mobilenet_v2_035': inception_preprocessing,
      'mobilenet_v2_140': inception_preprocessing,
61
62
      'nasnet_mobile': inception_preprocessing,
      'nasnet_large': inception_preprocessing,
63
      'pnasnet_mobile': inception_preprocessing,
maximneumann's avatar
maximneumann committed
64
      'pnasnet_large': inception_preprocessing,
65
66
67
      'resnet_v1_50': vgg_preprocessing,
      'resnet_v1_101': vgg_preprocessing,
      'resnet_v1_152': vgg_preprocessing,
68
69
70
71
72
      'resnet_v1_200': vgg_preprocessing,
      'resnet_v2_50': vgg_preprocessing,
      'resnet_v2_101': vgg_preprocessing,
      'resnet_v2_152': vgg_preprocessing,
      'resnet_v2_200': vgg_preprocessing,
73
74
75
76
77
78
79
80
81
82
83
      'vgg': vgg_preprocessing,
      'vgg_a': vgg_preprocessing,
      'vgg_16': vgg_preprocessing,
      'vgg_19': vgg_preprocessing,
  }

  if name not in preprocessing_fn_map:
    raise ValueError('Preprocessing name [%s] was not recognized' % name)

  def preprocessing_fn(image, output_height, output_width, **kwargs):
    return preprocessing_fn_map[name].preprocess_image(
84
85
86
87
88
89
        image,
        output_height,
        output_width,
        is_training=is_training,
        use_grayscale=use_grayscale,
        **kwargs)
90
91

  return preprocessing_fn