首先吐槽一下,mlgbdgdx,cuda、torch、torchvision版本全都是跟官网匹配上的,python3.8、3.9、3.10的版本全都试了,结果一跑模型,给我来个报错:
NotImplementedError: Could not run 'torchvision::nms' with arguments from the 'CUDA' backend. This could be because the operator doesn't exist for this backend, or was omitted during the selective/custom build process (if using custom build). If you are a Facebook employee using PyTorch on mobile, please visit https://fburl.com/ptmfixes for possible resolutions. 'torchvision::nms' is only available for these backends: [CPU, Meta, QuantizedCPU, BackendSelect, Python, FuncTorchDynamicLayerBackMode, Functionalize, Named, Conjugate, Negative, ZeroTensor, ADInplaceOrView, AutogradOther, AutogradCPU, AutogradCUDA, AutogradXLA, AutogradMPS, AutogradXPU, AutogradHPU, AutogradLazy, AutogradMeta, Tracer, AutocastCPU, AutocastCUDA, FuncTorchBatched, BatchedNestedTensor, FuncTorchVmapMode, Batched, VmapMode, FuncTorchGradWrapper, PythonTLSSnapshot, FuncTorchDynamicLayerFrontMode, PreDispatch, PythonDispatcher].
然后我参考了一位另外以为博主的解决方案,原链接如下:
我照做了,但是又出现了个问题,在同目录下的两个文件,居然不能import过去,提示找不到声明,我就很无语…………,下面直接说解决方法了(可能有点废话,但就当吐槽+总结吧)
第一步:
在报错界面找到下图所示的蓝色内容:
罪魁祸首就是它!!!这个方法调用出现了问题。
第二步:
点击蓝色路径可跳转到对于文件夹,找到框选出的函数,ctrl+f可直接搜索nms,找到和下图内容对应的就行。
这个函数返回值是通过调用torchvision下的nms方法实现的,但是实际上你会发现.nms是没有声明的,也就是它根本没有以正确的形式出现,或者说根本没有。
所以需要我们解决此问题
方法有两种
方法一:上述链接中内容比较详细,我这里简要提一下,在第一步框选的路径下,创建新的文件夹,内容放在文章末尾,然后命名xx(随意),然后在上图return返回值部分,改为 return xx.nms就行了(但是我自己的这样改,还是报错,没有找到声明,我不知道如何解决qaq,浪费了n小时时间,所以有了第二个方案)
方法二:直接将下面代码中的3个函数复制到boxes.py文件中,然后注意复制的nms要改一下,不然和原文件中函数名冲突了,比如改成nms1,我是直接将最后3个函数复制过去的,如下所示,然后就成功解决了,希望遇到同样问题的伙伴能顺利解决,早日顺利完成作业!!!!
from torch import Tensor import torch def box_area(boxes: Tensor) -> Tensor: """ Computes the area of a set of bounding boxes, which are specified by its (x1, y1, x2, y2) coordinates. Arguments: boxes (Tensor[N, 4]): boxes for which the area will be computed. They are expected to be in (x1, y1, x2, y2) format Returns: area (Tensor[N]): area for each box """ return (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1]) def box_iou(boxes1: Tensor, boxes2: Tensor) -> Tensor: """ Return intersection-over-union (Jaccard index) of boxes. Both sets of boxes are expected to be in (x1, y1, x2, y2) format. Arguments: boxes1 (Tensor[N, 4]) boxes2 (Tensor[M, 4]) Returns: iou (Tensor[N, M]): the NxM matrix containing the pairwise IoU values for every element in boxes1 and boxes2 """ area1 = box_area(boxes1) # 每个框的面积 (N,) area2 = box_area(boxes2) # (M,) lt = torch.max(boxes1[:, None, :2], boxes2[:, :2]) # [N,M,2] # N中一个和M个比较; 所以由N,M 个 rb = torch.min(boxes1[:, None, 2:], boxes2[:, 2:]) # [N,M,2] wh = (rb - lt).clamp(min=0) # [N,M,2] #小于0的为0 clamp 钳;夹钳; inter = wh[:, :, 0] * wh[:, :, 1] # [N,M] iou = inter / (area1[:, None] + area2 - inter) return iou # NxM, boxes1中每个框和boxes2中每个框的IoU值; def nms(boxes: Tensor, scores: Tensor, iou_threshold: float): """ :param boxes: [N, 4], 此处传进来的框,是经过筛选(NMS之前选取过得分TopK)之后, 在传入之前处理好的; :param scores: [N] :param iou_threshold: 0.7 :return: """ keep = [] # 最终保留的结果, 在boxes中对应的索引; idxs = scores.argsort() # 值从小到大的 索引 while idxs.numel() > 0: # 循环直到null; numel(): 数组元素个数 # 得分最大框对应的索引, 以及对应的坐标 max_score_index = idxs[-1] max_score_box = boxes[max_score_index][None, :] # [1, 4] keep.append(max_score_index) if idxs.size(0) == 1: # 就剩余一个框了; break idxs = idxs[:-1] # 将得分最大框 从索引中删除; 剩余索引对应的框 和 得分最大框 计算IoU; other_boxes = boxes[idxs] # [?, 4] ious = box_iou(max_score_box, other_boxes) # 一个框和其余框比较 1XM idxs = idxs[ious[0] <= iou_threshold] keep = idxs.new(keep) # Tensor return keep