# Net.forward( ) is taking much time to compute while detecting faces

**URL:** <https://forum.opencv.org/t/net-forward-is-taking-much-time-to-compute-while-detecting-faces/1374>\
**Category:** Uncategorized\
**Tags:** dnn, cuda\
**Created:** [February 6, 2021, 6:29am UTC](https://forum.opencv.org/t/net-forward-is-taking-much-time-to-compute-while-detecting-faces/1374 "2021-02-06T06:29:56Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maheswari.R](https://avatars.discourse-cdn.com/v4/letter/m/839c29/32.png) [@Maheswari.R](https://forum.opencv.org/u/Maheswari.R)\
**Post date:** [February 6, 2021, 6:29am UTC](https://forum.opencv.org/t/net-forward-is-taking-much-time-to-compute-while-detecting-faces/1374/1 "2021-02-06T06:29:56Z")

</div>

Hi…I am trying to find faces of human using cfg(yolov3-face.cfg) and weight files(yolov3-wider\_16000.weights) in realtime. I can get approximately 5 FPS in CPU. But it is not sufficient for my application. I noticed that single frame execution takes 200 ms. Out of it nearly 170ms spent for net.forward() line to execute. How to reduce the time taken for this line. Accuracy is good. I have attached part of the code below. Anyone please help me to come out from this issue.

while True:  
ret, frame = cap.read()  
[R, C, D] = frame.shape  
blob = cv2.dnn.blobFromImage(frame, 1 / 255, (IMG\_WIDTH, IMG\_HEIGHT), [0, 0, 0], 1, crop = False)  
net.setInput(blob)  
#start = timer()  
outs = net.forward(layer\_names) #this is taking around 170ms  
#print(“loop time: %.5f” % (timer() - start))

---

<div class="post-metadata">

**Author:** ![naser\_piltan](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/naser_piltan/32/391_2.png) [@naser\_piltan](https://forum.opencv.org/u/naser_piltan)\
**Post date:** [February 7, 2021, 3:26pm UTC](https://forum.opencv.org/t/net-forward-is-taking-much-time-to-compute-while-detecting-faces/1374/2 "2021-02-07T15:26:14Z")

</div>

Hi .  
You should build your OpenCV with CUDA support . Then you will gain a huge prformance from your application. You can refer to this link : [Build OpenCV with CUDA](https://www.pyimagesearch.com/2016/07/11/compiling-OpenCV-with-CUDA-support/)  
An other option is using OpenVINO as your inference backend. You will gain a good performance with OpenVINO , but not as much as CUDA.  
You can refer to this link for OpenCV and OpenVINO configuration :[OpenCV with OpenVINO](http://learnopencv.com/using-openvino-with-opencv/)

---

<div class="post-metadata">

**Author:** ![Maheswari.R](https://avatars.discourse-cdn.com/v4/letter/m/839c29/32.png) [@Maheswari.R](https://forum.opencv.org/u/Maheswari.R)\
**Post date:** [February 9, 2021, 11:27am UTC](https://forum.opencv.org/t/net-forward-is-taking-much-time-to-compute-while-detecting-faces/1374/3 "2021-02-09T11:27:16Z")

</div>

thanks for the reply @naser_piltan , I am using windows 10 which has nvidia GPU support. Is it possible to compile opencv with cuda?

---

<div class="post-metadata">

**Author:** ![Maheswari.R](https://avatars.discourse-cdn.com/v4/letter/m/839c29/32.png) [@Maheswari.R](https://forum.opencv.org/u/Maheswari.R)\
**Post date:** [February 9, 2021, 11:29am UTC](https://forum.opencv.org/t/net-forward-is-taking-much-time-to-compute-while-detecting-faces/1374/4 "2021-02-09T11:29:45Z")

</div>

I already tried installing opencv with cuda. But I cant…My CPU has cuda 10.2 and cudnn 7.6.05

---

<div class="post-metadata">

**Author:** ![naser\_piltan](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/naser_piltan/32/391_2.png) [@naser\_piltan](https://forum.opencv.org/u/naser_piltan)\
**Post date:** [February 9, 2021, 11:50am UTC](https://forum.opencv.org/t/net-forward-is-taking-much-time-to-compute-while-detecting-faces/1374/5 "2021-02-09T11:50:00Z")

</div>

@Maheswari.R Yes , sure . You can do it easily. I haven’t try it on windows, but there are much topics on the internet that you can use them.  
For example : [link1](https://medium.com/analytics-vidhya/build-opencv-from-source-with-cuda-for-gpu-access-on-windows-5cd0ce2b9b37) and [link2](https://haroonshakeel.medium.com/build-opencv-4-4-0-with-cuda-gpu-support-on-windows-10-without-tears-aa85d470bcd0) and [link3](https://programming.vip/docs/compile-opencv-with-cuda-support-on-windows-10.html)

---

<div class="post-metadata">

**Author:** ![Maheswari.R](https://avatars.discourse-cdn.com/v4/letter/m/839c29/32.png) [@Maheswari.R](https://forum.opencv.org/u/Maheswari.R)\
**Post date:** [February 9, 2021, 12:00pm UTC](https://forum.opencv.org/t/net-forward-is-taking-much-time-to-compute-while-detecting-faces/1374/6 "2021-02-09T12:00:04Z")

</div>

Thanks @naser_piltan. I will try it…

---

<div class="post-metadata">

**Author:** ![Roye](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@Roye](https://forum.opencv.org/u/Roye)\
**Post date:** [February 11, 2021, 2:29pm UTC](https://forum.opencv.org/t/net-forward-is-taking-much-time-to-compute-while-detecting-faces/1374/7 "2021-02-11T14:29:06Z")

</div>

Do you compile your code as debug profile, or release profile?  
Both the OpenCV and other libraries ( like Dlib ) have awful performance under debug.  
If this is the case, try to build in release profile, and check performance.

---

<div class="post-metadata">

**Author:** ![Maheswari.R](https://avatars.discourse-cdn.com/v4/letter/m/839c29/32.png) [@Maheswari.R](https://forum.opencv.org/u/Maheswari.R)\
**Post date:** [February 12, 2021, 5:38am UTC](https://forum.opencv.org/t/net-forward-is-taking-much-time-to-compute-while-detecting-faces/1374/8 "2021-02-12T05:38:10Z")

</div>

I compiled in release mode. Still problem exists.

---

<div class="post-metadata">

**Author:** ![cudawarped](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@cudawarped](https://forum.opencv.org/u/cudawarped)\
**Post date:** [February 12, 2021, 9:39am UTC](https://forum.opencv.org/t/net-forward-is-taking-much-time-to-compute-while-detecting-faces/1374/9 "2021-02-12T09:39:13Z")

</div>

Did you compile with CUDA? If not what do you mean by

```auto
I already tried installing opencv with cuda. But I cant…

```

Do you have a GPU? If not then your CPU times look consistent with [those reported by the author](https://gist.github.com/YashasSamaga/a84cf2826ab2dc755005321fe17cd15d#file-d2_backend_comparision-md).  
Which OS are you using?

---

<div class="post-metadata">

**Author:** ![Maheswari.R](https://avatars.discourse-cdn.com/v4/letter/m/839c29/32.png) [@Maheswari.R](https://forum.opencv.org/u/Maheswari.R)\
**Post date:** [February 12, 2021, 10:26am UTC](https://forum.opencv.org/t/net-forward-is-taking-much-time-to-compute-while-detecting-faces/1374/10 "2021-02-12T10:26:07Z")

</div>

I tried compiling opencv with cuda. But i can’t do it successfully due to errors. I am using windows 10. And i have GPU Geforce GTX 1650

CUDA Version: 10.2

cuDNN:8.0.0

---

<div class="post-metadata">

**Author:** ![naser\_piltan](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/naser_piltan/32/391_2.png) [@naser\_piltan](https://forum.opencv.org/u/naser_piltan)\
**Post date:** [February 15, 2021, 9:03am UTC](https://forum.opencv.org/t/net-forward-is-taking-much-time-to-compute-while-detecting-faces/1374/11 "2021-02-15T09:03:26Z")

</div>

What is the problem ? Can you give us more information ?

---

<div class="post-metadata">

**Author:** ![cudawarped](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@cudawarped](https://forum.opencv.org/u/cudawarped)\
**Post date:** [February 15, 2021, 10:16am UTC](https://forum.opencv.org/t/net-forward-is-taking-much-time-to-compute-while-detecting-faces/1374/12 "2021-02-15T10:16:16Z")

</div>

If you downgrade your version of cuDNN you could try using a pre-compiled version from [here](https://jamesbowley.co.uk/downloads/#OpenCV4.3.0).
