Could not run ‘torchvision::nms‘ with arguments from the ‘CUDA‘ backend的解决;用GPU跑Detectron2的demo

Could not run ‘torchvision::nms’ with arguments from the ‘CUDA’ backend的解决;用GPU跑Detectron2的demo

在上一篇文章中,有一个问题没有解决。
Detectron2-入门1-极链AI云平台部署-多图好玩测试

Traceback (most recent call last):
  File "demo.py", line 112, in <module>
    predictions, visualized_output = demo.run_on_image(img)
  File "/root/detectron2/demo/predictor.py", line 48, in run_on_image
    predictions = self.predictor(image)
  File "/root/detectron2/detectron2/engine/defaults.py", line 320, in __call__
    predictions = self.model([inputs])[0]
  File "/opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/root/detectron2/detectron2/modeling/meta_arch/rcnn.py", line 146, in forward
    return self.inference(batched_inputs)
  File "/root/detectron2/detectron2/modeling/meta_arch/rcnn.py", line 204, in inference
    proposals, _ = self.proposal_generator(images, features, None)
  File "/opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/root/detectron2/detectron2/modeling/proposal_generator/rpn.py", line 477, in forward
    proposals = self.predict_proposals(
  File "/root/detectron2/detectron2/modeling/proposal_generator/rpn.py", line 503, in predict_proposals
    return find_top_rpn_proposals(
  File "/root/detectron2/detectron2/modeling/proposal_generator/proposal_utils.py", line 116, in find_top_rpn_proposals
    keep = batched_nms(boxes.tensor, scores_per_img, lvl, nms_thresh)
  File "/root/detectron2/detectron2/layers/nms.py", line 21, in batched_nms
    return box_ops.batched_nms(boxes.float(), scores, idxs, iou_threshold)
  File "/opt/conda/lib/python3.8/site-packages/torch/jit/_trace.py", line 1100, in wrapper
    return fn(*args, **kwargs)
  File "/opt/conda/lib/python3.8/site-packages/torchvision/ops/boxes.py", line 88, in batched_nms
    keep = nms(boxes_for_nms, scores, iou_threshold)
  File "/opt/conda/lib/python3.8/site-packages/torchvision/ops/boxes.py", line 42, in nms
    return torch.ops.torchvision.nms(boxes, scores, iou_threshold)
RuntimeError: Could not run 'torchvision::nms' with arguments from the 'CUDA' backend. 'torchvision::nms' is only available for these backends: [CPU, BackendSelect, Named, AutogradOther, AutogradCPU, AutogradCUDA, AutogradXLA, Tracer, Autocast, Batched, VmapMode].

参考了这篇博客:
【python】yolov5的torch与torchvision环境问题
非常感谢这位大佬!!!他肯定也是做了很多次尝试,然后得出可以正确运行的环境,为:

torch==1.7.1+cu110
torchvision==0.8.2+cu110

然后去pytorch的官网,在previous-versions板块,如下。
https://pytorch.org/get-started/previous-versions/
找到【v1.7.1】下的【Wheel】下的【Linux and Windows】下的【# CUDA 11.0】。
通过以下语句进行安装。

# CUDA 11.0
pip install torch==1.7.1+cu110 torchvision==0.8.2+cu110 torchaudio==0.7.2 -f https://download.pytorch.org/whl/torch_stable.html

安装成功后,再去掉MODEL.DEVICE cpu,即可用gpu跑了。速度会快很多。

成功用GPU跑Detectron2的demo

用同样的三张图进行cpu和gpu的测试。图片的检测结果是相同的,但是运行速度相差较大。
如下图,是用MODEL.DEVICE cpu跑的。时间较长。
cpu版本
如下图,是【删除】了MODEL.DEVICE cpu跑的。时间较短。
gpu版本
用cpu跑的和用gpu跑的目标检测结果,各种物体的预测种类及其可能性没有变化。
结果图

  • 6
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值