我正在尝试将虚拟机的GPU与OpenCV库(4.2)和Python 3.7一起使用
我已使用CUDA安装opencv,以下命令返回1:
import cv2
count = cv2.cuda.getCudaEnabledDeviceCount()
print(count)
我尝试在使用和不使用GPU的情况下运行代码:
start = time.time()
network = cv.dnn.readNetFromDarknet(config_path, weights_path)
if IS_CUDA:
network.setPreferableBackend(cv.dnn.DNN_BACKEND_CUDA)
network.setPreferableTarget(cv.dnn.DNN_TARGET_CUDA)
my_function(network, frame)
end = time.time()
print(end - start)
但运行时间是相同的
我的问题:使用setPreferableBackend()和setPreferableTarget()是否足以利用GPU的能力
这应该足以使它使用CUDA,你应该看到一个明显的加速。检查任务管理器的“性能”选项卡,查看是否正在使用Nvidia GPU。我不知道在我的函数()的幕后发生了什么,所以我无法判断您使用它的方式是否有所不同
当我想要快速设置Yolo网络时,我通常会复制这个类
你可以这样使用它
编辑:哦,好的,哈哈。我找到了我的安装说明,第一条说明是:
网址是:
https://www.pyimagesearch.com/2020/02/03/how-to-use-opencvs-dnn-module-with-nvidia-gpus-cuda-and-cudnn/
不确定这会有多大帮助,但我显然认为教程已经足够清楚了,我不需要为这一步做任何额外的注释或更正
首先,在运行代码之前,您必须检查您的工作环境
您使用的是什么虚拟机?通常,虚拟机不支持GPU虚拟化。您必须添加PCI直通才能获得此功能
如果为true,您是否可以首先通过运行
nvidia-smi
(在您喜爱的bash/cmd环境中)来检查nvidia驱动程序,以检查您的设备是否已成功检测到然后通过在python shell
cv2.cuda.getCudaEnabledDeviceCount()
中运行来检查opencv python是否支持gpu如果结果为0,并且nvidia smi检测到您的gpu,则必须在特定gpu模型的支持下编译python opencv控制盘,并将其安装到python环境中
相关问题 更多 >
编程相关推荐