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 
    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跑的。时间较长。

如下图,是【删除】了MODEL.DEVICE cpu跑的。时间较短。

用cpu跑的和用gpu跑的目标检测结果,各种物体的预测种类及其可能性没有变化。
Could not run ‘torchvision::nms‘ with arguments from the ‘CUDA‘ backend的解决;用GPU跑Detectron2的demo_第1张图片

你可能感兴趣的:(Detectron2,深度学习,pytorch,gpu)