In a previous question the purpose and structure of the serving_input_receiver_fn
is explored and in the answer:
def serving_input_receiver_fn():
"""For the sake of the example, let's assume your input to the network will be a 28x28 grayscale image that you'll then preprocess as needed"""
input_images = tf.placeholder(dtype=tf.uint8,
shape=[None, 28, 28, 1],
name='input_images')
# here you do all the operations you need on the images before they can be fed to the net (e.g., normalizing, reshaping, etc). Let's assume "images" is the resulting tensor.
features = {'input_data' : images} # this is the dict that is then passed as "features" parameter to your model_fn
receiver_tensors = {'input_data': input_images} # As far as I understand this is needed to map the input to a name you can retrieve later
return tf.estimator.export.ServingInputReceiver(features, receiver_tensors)
the answer's author states (in regards to receiver_tensors
):
As far as I understand this is needed to map the input to a name you can retrieve later
This distinction is unclear to me. In practice, (see this colab), the same dictionary can be passed to both features
and receiver_tensors
.
From the source code of @estimator_export('estimator.export.ServingInputReceiver')
(or the ServingInputReceiver docs:
- features: A
Tensor
,SparseTensor
, or dict of string toTensor
orSparseTensor
, specifying the features to be passed to the model. Note: iffeatures
passed is not a dict, it will be wrapped in a dict with a single entry, using 'feature' as the key. Consequently, the model must accept a feature dict of the form {'feature': tensor}. You may useTensorServingInputReceiver
if you want the tensor to be passed as is.- receiver_tensors: A
Tensor
,SparseTensor
, or dict of string toTensor
orSparseTensor
, specifying input nodes where this receiver expects to be fed by default. Typically, this is a single placeholder expecting serializedtf.Example
protos.
After reading, it is clear to me what the purposes of features
is. features
is a dictionary of inputs that I then send through the graph. Many common models have just a single input, but you can or course have more.
So then the statement regarding receiver_tensors
which "Typically, this is a single placeholder expecting serialized tf.Example
protos.", to me, suggests that receiver_tensors
want a singular batched placeholder for (Sequence)Example
s parsed from TF Record
s.
Why? If the TF Record
s is fully preprocessed, then this is redundant? if it is not fully pre-processed, why would one pass it? Should the keys in the features
and receiver_tensors
dictionaries be the same?
Can someone please provide me with a more concrete example of the difference and what goes where, as right now
input_tensors = tf.placeholder(tf.float32, <shape>, name="input_tensors")
features = receiver_tensors = {'input_tensors': input_tensors}
works... (even if maybe it shouldn't...)