Batching in Inception. Willing to help implement, need to know what is required.
I noticed that the Inception example does not yet support batching, which would offer a considerable speedup (and serve as a helpful tutorial on how to implement batching in TF Serving in general!). I'm very willing to help implement this, but I'm confused as to what barriers are in place to its implementation; why wasn't it implemented in the first place? It seems like a relatively trivial change.
The server file refers to the export python script "for details" but this appears only to say:
# TODO(b/27776734): Add batching support.
I'm not sure what the issue is here though. IIRC, the model doesn't care if the inputs are variable-sized in the first dimension. If it does, would it be helpful or possible to add in "dummy" images that will stand in for available, but unused, slots in the batch? Presumably this would negatively affect performance when the requests are coming in at some low number per batch-job sent to the GPU, but this performance cost would be bounded by the fact that subsequent batch-jobs sent to the GPU would consist of more true images per batch as requests from clients piled up.
The inception inference model (inception_export.py) consist of two parts: input transformation graph and original inception graph. You are correct that the original inception graph supports batching already. However, the new input transformation graph does not -- as most ops in it (tf.image.decode_jpeg, tf.image.central_crop, etc.) support only single image as input.
To support batching, a queue runner can be added to run the input transform part in batch/parallel -- similar to how the original inception model does it in pre-training processing. An alternative will be to implement the transformation logic on client side, instead of as part of the graph.
Either way, you don't need to add "dummy/padding" images. As the original inception model support dynamic batch size.
So as I understand it the problem is applying operations that take as input a single tensor to a variable-sized list of tensors, which is (I infer) not possible post export (and hence requiring a queue) or moving the single-tensor transformation ops out of the server altogether and into the client.
I think the client-side ops are not tenable; applying them would mean sending raw arrays (I think?) to the server which would increase the bandwidth requirements by an order of magnitude. Further, re-encoding them would mean we'd need a queue on the server side anyway since, as you say, decode_jpeg is single tensor.
However, I'm not exactly sure how the alternative would work. As I understand it the server invokes the exported model with whatever the c++ equivalent of sess.run(feed_dict={input_jpeg=foo}), which I think is this here in signature.cc:
const Status run_status =
session->Run({{signature.input().tensor_name(), input}},
output_tensor_names, {}, &outputs);
We would have to separate these into something that transformed and enqueued requests as they came in and something else that ran the graph one some interval timer yes?
I feel there may be some confusions, so let me clarify batching first.
In order for inception example to support batching, both things are required:
- The gRPC server
inception_inference.ccto support batching multiple user requests together and feed them as a single input to the inference graph. This we already have, with the use ofStreamingBatchScheduler. - The exported inference graph defined in
inception_export.pyto support running with such batched input. This is what’s missing, and the TODO is about.
The inference graph consists of two sub-graphs:
- The transformation graph, which takes a jpeg encoded buffer, decodes it, and properly cropped/centered/scaled the image.
- The inception graph, which takes the output of transformation graph, and does classification.
Although the inception graph supports batched input, the transformation graph does not -- because major ops in it do not support batched processing as I explained previously. As a result, the inference graph as whole does not support running with batched input.
Therefore, several temporary workarounds/place-holders are currently in place. And these could look confusing:
scheduler_options.max_batch_size = 1ininception_inference.ccto always generate input batch of size 1.- Such input is fed to
jpeg = tf.placeholder(tf.string, shape=(1))of the inference graph. - The “batch dimension” of the input is removed with
tf.squeeze(jpegs, [0])before passing to the transformation graph. - The “batch dimension” is added back with
tf.expand_dims(image, 0)to the output of transformation graph before passing to the inception graph.
To fix the issue, there are in general two options:
- Implement transformation logic outside of inference graph.
- Or, as the TODO intended, change transformation graph so that it supports running with batched input.
To implement transformation logic outside of inference graph, again we have two alternatives:
- Implement transformation logic on client side. This is to decode/crop/center/scale jpeg image before sending it to sever. And you are correct that this increases bandwidth - however it decreases server CPU load.
- Or, implement transformation logic on server side, as part of gRPC server. This is to spawn multiple threads to decode/crop/center/scale for each batch of jpegs images, before doing
session->Run().
Either way, the transformation graph can then be removed from inception_export.py. The inference graph will be equivalent to the inception graph, which takes a batch of decoded/transformed image data buffer as input.
Now that I think of it, the first option, especially if implemented on client side with help of PIL, is the easiest to do. Therefore I recommend it. The bandwidth will not be a big issues, as the bitmap is scaled down to a fixed size before send.
To implement batched input support of the transformation graph will be, I think, non-trivial:
- Add ops to enqueue batched input to a queue A.
- Duplicate multiple copies of the the transformation graph, with each taking single input from A, and enqueuing its output to queue B.
- Inception graph to wait and take a batch from B.
- Tricky issues include the number of transformation graph duplicas, control logic for dynamic batch size, etc.
Therefore, again, I recommend implementing the first option if possible.
Okay sounds good. Fortunately I don't think I was as confused as I made it seem when I posted (d'oh).
I agree that the first option is the most easy to implement, I was just worried about the bandwidth overhead (as I mentioned) of transmitting bitmaps instead of JPEGs.
While it's been a while since I've done anything in C++, I think this should be relatively easy to implement. The DoClassifyInBatch seems heavily borrowed from it's equivalent in the MNIST tutorial, with the primary objective being we have to modify
tensorflow::Tensor batched_input(tensorflow::DT_STRING, {batch_size});
for (int i = 0; i < batch_size; ++i) {
batched_input.vec<string>()(i) =
batch->mutable_task(i)->calldata->request().jpeg_encoded();
}
to perform the copy operations on vectors of floats in the same vein as:
Tensor input(tensorflow::DT_FLOAT, {batch_size, kImageDataSize});
auto dst = input.flat_outer_dims<float>().data();
for (int i = 0; i < batch_size; ++i) {
std::copy_n(
batch->mutable_task(i)->calldata->request().image_data().begin(),
kImageDataSize, dst);
dst += kImageDataSize;
}
Without the flattening and making sure it has placeholders for 4-dimensional floats in stead of a string, etc etc.
The preprocessing in PIL should be trivial. I'll start on this.
@fangweili I finished a first pass; will test it tomorrow. It's here.
As you suggested clientside preprocessing is done using PIL. Other resizing options (crop, padding) are also implemented. Data are passed via numpy.ndarray.tobytes, resizing the flattened arrays is done in inception_export.
Clientside concurrency is pending.
I submitted a pull request; I'm not sure if I should wait for one of the devs to take the next step or if there is something else I should be doing to help. Let me know what else is needed from me!
Thanks for the pull request! We will take a look soon.
https://github.com/tensorflow/serving/commit/74298891bc940bb592bf728721f83c34e8d9c35a should have fixed the batching for export and inference.
Hmm...what is the dimensionality of your input batches?
On Mon, Aug 8, 2016 at 6:14 PM, LiberiFatali [email protected] wrote:
When I use #7429889, I get this error: ValueError: Shape of a new variable (conv0/weights) must be fully defined, but instead was (3, 3, ?, 32).
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/tensorflow/serving/issues/38#issuecomment-238425837, or mute the thread https://github.com/notifications/unsubscribe-auth/AGICNQb6RG3jbTL6m1nRgNXu4Tj05Xexks5qd9RzgaJpZM4IHl2S .
Nick Dufour Machine Learning Engineer Neon Labs https://www.neon-lab.com/
@eriophora It was my mistake, not related to #7429889. I managed to fix it. Thanks.
@eriophora Have you managed batching to be done? I have tried to use your changes, but persistently getting grpc and numpy errors.