Does opencv_dnn use gpu?
I tried to run the code in Jetson GPU but I can't?
I just obtained 5 fps, this means that use CPU not GPU
this is my full code
from imutils.video import VideoStream
from imutils.video import FPS
import numpy as np
import argparse
import imutils
import time
import cv2
CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat",
"bottle", "bus", "car", "cat", "chair", "cow", "diningtable",
"dog", "horse", "motorbike", "person", "pottedplant", "sheep",
"sofa", "train", "tvmonitor"]
COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3))
print("[INFO] loading model...")
net = cv2.dnn.readNetFromCaffe('prototxt.txt', 'caffemodel') # deep neural network ( dnn )
print("[INFO] starting video stream...")
vs = cv2.VideoCapture(0)
vs.release()
vs = cv2.VideoCapture(0)
time.sleep(2.0)
fps = FPS().start()
while True:
ret, frame = vs.read() # add ret,
frame = imutils.resize(frame, width=400)
(h, w) = frame.shape[:2]
blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)),
0.007843, (300, 300), 127.5)
net.setInput(blob)
detections = net.forward()
big_area = 0
big_center = 320
detected = 0
# loop over the detections
for i in np.arange(0, detections.shape[2]):
confidence = detections[0, 0, i, 2]
object_type = int(detections[0, 0, i, 1])
if object_type == 15 and confidence > 0.2: # it was if confidence > args["confidence"]:
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")
label = "{}: {:.2f}%".format('person', confidence * 100)
cv2.rectangle(frame, (startX, startY), (endX, endY), [0, 0, 255], 2)
y = startY - 15 if startY - 15 > 15 else startY + 15
cv2.putText(frame, label, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, [0, 0, 255], 2)
rect_area = (endX - startX) * (endY - startY)
detected = 1
if rect_area > big_area: # big_area and big_center are used so that the TurtleBot always tracks the closest person
big_area = rect_area
cv2.imshow("Frame", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
fps.update()
# stop the timer and display FPS information
fps.stop()
vs.release()
cv2.destroyAllWindows()
any ideas or suggestions?
thank you in advance
I found this usueful: https://elinux.org/images/9/9e/Deep-L...
thanks, can I do it but it is bad, I saw that the CPU is faster than GPU with MobileNet SSD, right? https://gist.github.com/YashasSamaga/...
Note that the benchmarks were carried out on a desktop/mobile CPU. They might not be applicable to embedded boards. Jetson can probably perform better with the CUDA backend than the on-board CPU. CUDA support for the DNN module was merged into master a few days ago. You have to use
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
andnet.setPreferableTarget(cv2.dnn. DNN_TARGET_CUDA)
orcv2.dnn.DNN_TARGET_CUDA_FP16
if your device supports it.@Yashas, thank you so much for your help, I was frustrated, but when I read your response, change my feeling, for more details about my system, you can see here, what is wrong???
@redhwan your question was posted before the CUDA support was merged into master. I preassume that your OpenCV build doesn't have the CUDA backend. To use the CUDA backend, you need to build the master branch from OpenCV GitHub repository.
@Yashas, I have one question about building with CUDA based on your gist. The two first points
WITH_CUDA
andWITH_CUDNN
are Cmake parameters I guess. Then there is The CUDA backend is enabled by setting the following option:OPENCV_DNN_CUDA
Where in the build process do we specify that option?@Erik Langskjegg
OPENCV_DNN_CUDA
is also a CMake option just like the other two.@Yashas, I am confused about some things. So, I have some questions? first one: you mean that master is 4.x releases and branch is 3.4.x releases??? I got it here, right? Second one: do the steps of building a master differ from the branch? I followed this blog post hereon my PC with
"3.3.1 version"
and here on JetsonTX2. Third one: can I run theOpenCV dnn
with CUDA Version: 9 or not? you mentioned cuDNN (min: 7.5.0) here if not, I need to use everything similar your system on my PC.Sorry, @Yashas, I have other questions, you wrote above "you need to build the master branch from OpenCV GitHub repository", you mean these here and here or not ?? another issue I noticed here in your tables that MobileNet SSD is very bad with GPU, Inference Engine CPU was the best one?? can you give me some tips about how to use it? my PC: Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz and NVIDIA GTX 1050 Ti
@redhwan The CUDA backend hasn't been released yet. It's available on the master branch (where stuff for the next release are kept). You need to download (or clone) the OpenCV repository and build it. You can find more information about installing Inference Engine here.