# How to perform batch inference object detection using DNN module?

**URL:** <https://forum.opencv.org/t/how-to-perform-batch-inference-object-detection-using-dnn-module/2648>\
**Category:** Python\
**Tags:** dnn\
**Created:** [April 7, 2021, 12:15pm UTC](https://forum.opencv.org/t/how-to-perform-batch-inference-object-detection-using-dnn-module/2648 "2021-04-07T12:15:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nekro](https://avatars.discourse-cdn.com/v4/letter/n/46a35a/32.png) [@nekro](https://forum.opencv.org/u/nekro)\
**Post date:** [April 7, 2021, 12:15pm UTC](https://forum.opencv.org/t/how-to-perform-batch-inference-object-detection-using-dnn-module/2648/1 "2021-04-07T12:15:25Z")

</div>

I’m currently reading single images from a directory and doing a feed forward pass through my network sequentially using the DNN module.

Please can someone provide guidance on how to perform batch inference using the DNN module?

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [April 7, 2021, 12:32pm UTC](https://forum.opencv.org/t/how-to-perform-batch-inference-object-detection-using-dnn-module/2648/2 "2021-04-07T12:32:28Z")

</div>

there is not much magic there, you make a list of images and use

```
cv2.dnn.blobFromImages(imagelist, ...)

```

[see docs](https://docs.opencv.org/master/d6/d0f/group__dnn.html#ga0b7b7c3c530b747ef738178835e1e70f)

then you’ll receive a 4d tensor from `net.forward()` in the format `[N,C,W,H]` , where N is the batch count

---

<div class="post-metadata">

**Author:** ![nekro](https://avatars.discourse-cdn.com/v4/letter/n/46a35a/32.png) [@nekro](https://forum.opencv.org/u/nekro)\
**Post date:** [April 8, 2021, 10:22am UTC](https://forum.opencv.org/t/how-to-perform-batch-inference-object-detection-using-dnn-module/2648/3 "2021-04-08T10:22:32Z")

</div>

@berak Thanks for your reply.

How many images can blobFromImages accept? Does it determine the optimal number of images to run through inference itself or do we need to find this ourselves somehow?

Also, is there a relation to the backend used, e.g. dnn.DNN\_TARGET\_CUDA\_FP16?

---

<div class="post-metadata">

**Author:** ![SanSancr777](https://avatars.discourse-cdn.com/v4/letter/s/ebca7d/32.png) [@SanSancr777](https://forum.opencv.org/u/SanSancr777)\
**Post date:** [February 18, 2022, 8:32am UTC](https://forum.opencv.org/t/how-to-perform-batch-inference-object-detection-using-dnn-module/2648/4 "2022-02-18T08:32:35Z")

</div>

The batch size should be created according to the requirement and taking care of the resources.
