SciSharp / TensorFlow.NET

.NET Standard bindings for Google's TensorFlow for developing, training and deploying Machine Learning models in C# and F#.
https://scisharp.github.io/tensorflow-net-docs
Apache License 2.0
3.21k stars 516 forks source link

[Question]: Batch inference ? #1041

Closed Cilouche closed 1 year ago

Cilouche commented 1 year ago

Description

Hi;

I would like to make an inference on a number of images (Batch images inference), however I can't find an application example or documentation on that..

Suggestions please?

THANKS

Alternatives

No response

AsakusaRinne commented 1 year ago

I think the workflow is as following (let's assume imgs is am array of image tensors, in which tensors have shape [h, w, c]):

Tensor batched_image = tf.expand_dims(imgs[0], 0);
for(int i = 1; i < imgs.Length; i++){
    batched_image = tf.concat(new []{batched_image, tf.expand_dims(imgs[i], 0)}, 0);
}
var result = model.predict(bacthed_image);

Note that in tensorflow nhwc is the default layout of image input, which means the four dims are batchsize, width, height, channels respectively. Generally using different hwc in training and predicting phase may lead to error, but changing batchsize won't.

Cilouche commented 1 year ago

Thanks