serving icon indicating copy to clipboard operation
serving copied to clipboard

Tf serving - initialize dataset iterator at prediction time

Open phisch124 opened this issue 8 years ago • 13 comments

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?

phisch124 avatar Feb 20 '18 08:02 phisch124

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.

phisch124 avatar Feb 20 '18 09:02 phisch124

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 avatar Aug 13 '18 09:08 FerdinandvL

@FerdinandvL have you successfully deployed serving model with data iterator?

sdukaka avatar Aug 19 '18 13:08 sdukaka

Actually not with a Dataset iterator. No. But with an index_table_from_tensor

FerdinandvL avatar Aug 19 '18 14:08 FerdinandvL

anyone solved this problem?

@phisch124 did you solve this problem? can you please direct me to right direction?

ganni avatar Oct 11 '18 01:10 ganni

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.

FerdinandvL avatar Oct 11 '18 06:10 FerdinandvL

Is this still an issue ?

Harshini-Gadige avatar Oct 25 '18 17:10 Harshini-Gadige

Closing due to lack of activity.

gautamvasudevan avatar Nov 21 '18 19:11 gautamvasudevan

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 [?]'

suziW avatar Nov 30 '18 09:11 suziW

I have the same issue and legacy_init_op does not solve it. Can you help, please.

saeedaghabozorgi avatar Dec 02 '18 14:12 saeedaghabozorgi

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

emmettng avatar Dec 03 '18 13:12 emmettng

@gautamvasudevan Any inputs please ?

Harshini-Gadige avatar Dec 03 '18 17:12 Harshini-Gadige

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!

gautamvasudevan avatar Dec 03 '18 18:12 gautamvasudevan