Tf serving - initialize dataset iterator at prediction time
I would like to run a tensorflow/nmt model in production. My problem is that because of the data input pipeline tensorflow/nmt uses (https://github.com/tensorflow/nmt#data-input-pipeline) I need to initialise the dataset iterator with a new feed_dict.
batched_iterator = batched_dataset.make_initializable_iterator()
((source, source_lengths), (target, target_lengths)) = batched_iterator.get_next()
# At initialization time.
session.run(batched_iterator.initializer, feed_dict={...})
I am able to serve the model using a docker container (build using: https://github.com/tensorflow/serving/blob/master/tensorflow_serving/tools/docker/Dockerfile.devel),
2018-02-20 07:48:02.425253: I tensorflow_serving/model_servers/main.cc:315] Running ModelServer at 0.0.0.0:9000 ...
but prediction fails, as expected, with:
2018-02-20 07:48:40.409613: W external/org_tensorflow/tensorflow/core/framework/op_kernel.cc:1202] OP_REQUIRES failed at iterator_ops.cc:844 : Failed precondition: GetNext() failed because the iterator has not been initialized. Ensure that you have run the initializer operation for this iterator before getting the next element.
Is there a way to initialise the dataset iterator with the new input batches in tensorflow/serving?
Here is a google group talking about potential workarounds: https://groups.google.com/a/tensorflow.org/forum/#!msg/discuss/SxF8LMlQUHE/KZL-Sf-UAgAJ
In my opinion, there should be the possibility to add the iterator.initializer to the prediction signature:
prediction_signature = (
tf.saved_model.signature_def_utils.build_signature_def(
inputs={'sources': tensor_info_source_list, 'batch_size': tensor_info_batch_size},
outputs={'outputs': tensor_info_outputs, 'final_state': tensor_info_state},
method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME),
initializer=batched_iterator.initializer, # <-------- feature request
)
So it would run something like:
session.run(batched_iterator.initializer, feed_dict={'sources': ..., 'batch_size': ...})
and then the prediction.
Have you looked at the tf.saved_model.builder.SavedModelBuilder class?
It has the method add_meta_graph_and_variables(...) which takes the argument legacy_init_op. You can pass an operation or group of operations to this argument.
from tensorflow.python.ops import control_flow_ops
...
saver = tf.saved_model.builder.SavedModelBuilder('./export')
...
saver.add_meta_graph_and_variables(
...,
legacy_init_op=control_flow_ops.group(variables_init_op, tables_init_op),
... )
...
saver.save()
@FerdinandvL have you successfully deployed serving model with data iterator?
Actually not with a Dataset iterator. No. But with an index_table_from_tensor
anyone solved this problem?
@phisch124 did you solve this problem? can you please direct me to right direction?
As stated above, maybe try it by passing the initializer to legacy_init_op Argument in SavedModelBuilder.add_meta_graph_and_variables. I used it for initializing tables at serving time and it works. I do not know why a dataset iterator initializer should be different from any other initializer operation.
Is this still an issue ?
Closing due to lack of activity.
As stated above, maybe try it by passing the initializer to legacy_init_op Argument in SavedModelBuilder.add_meta_graph_and_variables. I used it for initializing tables at serving time and it works. I do not know why a dataset iterator initializer should be different from any other initializer operation.
no, doesn't work with op iterator.initializer, when u start server, it has error like 'You must feed a value for placeholder tensor 'Placeholder' with dtype string and shape [?]'
I have the same issue and legacy_init_op does not solve it. Can you help, please.
NO legacy_init_op doesn't work! performance guide suggests to avoid using feedict, so I use tfrecord and iterator. tensorflow serving suggests using SavedModel an I did And it turn out to be pretty ugly. REALLY REALLY frustrated
@gautamvasudevan Any inputs please ?
Please go to Stack Overflow for help and support - there are many more experts there to help with these kinds of issues.
https://stackoverflow.com/questions/tagged/tensorflow-serving
If you open a GitHub issue, it must be a bug, a feature request, or a significant problem with documentation (for small docs fixes please send a PR instead).
Thanks!