• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"""Utilities for creating input_fns (deprecated).
16
17This module and all its submodules are deprecated. See
18[contrib/learn/README.md](https://www.tensorflow.org/code/tensorflow/contrib/learn/README.md)
19for migration instructions.
20
21Contents of this file are moved to tensorflow/python/estimator/export.py.
22InputFnOps is renamed to ServingInputReceiver.
23build_parsing_serving_input_fn is renamed to
24  build_parsing_serving_input_receiver_fn.
25build_default_serving_input_fn is renamed to
26  build_raw_serving_input_receiver_fn.
27"""
28
29from __future__ import absolute_import
30from __future__ import division
31from __future__ import print_function
32
33import collections
34
35from tensorflow.python.framework import dtypes
36from tensorflow.python.framework import tensor_shape
37from tensorflow.python.ops import array_ops
38from tensorflow.python.ops import parsing_ops
39from tensorflow.python.util.deprecation import deprecated
40
41
42class InputFnOps(collections.namedtuple('InputFnOps',
43                                        ['features',
44                                         'labels',
45                                         'default_inputs'])):
46  """A return type for an input_fn (deprecated).
47
48  THIS CLASS IS DEPRECATED. Please use tf.estimator.export.ServingInputReceiver
49  instead.
50
51  This return type is currently only supported for serving input_fn.
52  Training and eval input_fn should return a `(features, labels)` tuple.
53
54  The expected return values are:
55    features: A dict of string to `Tensor` or `SparseTensor`, specifying the
56      features to be passed to the model.
57    labels: A `Tensor`, `SparseTensor`, or a dict of string to `Tensor` or
58      `SparseTensor`, specifying labels for training or eval. For serving, set
59      `labels` to `None`.
60    default_inputs: a dict of string to `Tensor` or `SparseTensor`, specifying
61      the input placeholders (if any) that this input_fn expects to be fed.
62      Typically, this is used by a serving input_fn, which expects to be fed
63      serialized `tf.Example` protos.
64  """
65
66
67@deprecated(None, 'Please use '
68            'tf.estimator.export.build_parsing_serving_input_receiver_fn.')
69def build_parsing_serving_input_fn(feature_spec, default_batch_size=None):
70  """Build an input_fn appropriate for serving, expecting fed tf.Examples.
71
72  Creates an input_fn that expects a serialized tf.Example fed into a string
73  placeholder.  The function parses the tf.Example according to the provided
74  feature_spec, and returns all parsed Tensors as features.  This input_fn is
75  for use at serving time, so the labels return value is always None.
76
77  Args:
78    feature_spec: a dict of string to `VarLenFeature`/`FixedLenFeature`.
79    default_batch_size: the number of query examples expected per batch.
80        Leave unset for variable batch size (recommended).
81
82  Returns:
83    An input_fn suitable for use in serving.
84  """
85  def input_fn():
86    """An input_fn that expects a serialized tf.Example."""
87    serialized_tf_example = array_ops.placeholder(dtype=dtypes.string,
88                                                  shape=[default_batch_size],
89                                                  name='input_example_tensor')
90    inputs = {'examples': serialized_tf_example}
91    features = parsing_ops.parse_example(serialized_tf_example, feature_spec)
92    labels = None  # these are not known in serving!
93    return InputFnOps(features, labels, inputs)
94  return input_fn
95
96
97@deprecated(None, 'Please use '
98            'tf.estimator.export.build_raw_serving_input_receiver_fn.')
99def build_default_serving_input_fn(features, default_batch_size=None):
100  """Build an input_fn appropriate for serving, expecting feature Tensors.
101
102  Creates an input_fn that expects all features to be fed directly.
103  This input_fn is for use at serving time, so the labels return value is always
104  None.
105
106  Args:
107    features: a dict of string to `Tensor`.
108    default_batch_size: the number of query examples expected per batch.
109        Leave unset for variable batch size (recommended).
110
111  Returns:
112    An input_fn suitable for use in serving.
113  """
114  def input_fn():
115    """an input_fn that expects all features to be fed directly."""
116    features_placeholders = {}
117    for name, t in features.items():
118      shape_list = t.get_shape().as_list()
119      shape_list[0] = default_batch_size
120      shape = tensor_shape.TensorShape(shape_list)
121
122      features_placeholders[name] = array_ops.placeholder(
123          dtype=t.dtype, shape=shape, name=t.op.name)
124    labels = None  # these are not known in serving!
125    return InputFnOps(features_placeholders, labels, features_placeholders)
126  return input_fn
127