深度信息+VSCode(ctrl+单击)跳转到指定函数

depth = torch.zeros(batch_size, img.shape[1], 1, *self.image_size).to(points[0].device)

创建一个全零张量,表示图像的深度信息。每个参数含义:

  • batch_size:批量大小,表示同时处理的图像数量。

  • img.shape[1]:取图像张量的第二个维度的大小。在一般的情况下,这通常代表了图像的通道数量,如RGB图像有3个通道。

  • 1:创建一个额外的维度,大小为1。

  • *self.image_size:这是一个元组解包操作,将图像的高度和宽度作为后续的维度。例如,如果self.image_size(H, W),那么*self.image_size会解包为H, W

  • .to(points[0].device):将创建的零张量移动到与points[0]在同一设备上(CPU或GPU)。

因此,如果图像张量img的形状是(B, C, H, W),并且self.image_size(H', W'),那么depth张量的形状将是(B, C, 1, H', W')

ModuleNotFoundError: No module named 'tools' 

export PYTHONPATH=./
定位到当前路径

一.NMS过程

1:将所有检出的output_bbox按cls score划分(如pascal voc分20个类,也即将output_bbox按照其对应的cls score划分为21个集合,1个bg类,只不过bg类就没必要做NMS而已);

2:在每个集合内根据各个bbox的cls score做降序排列,得到一个降序的list_k;

3:从list_k中最大cls score开始,计算该bbox_x与list中其他bbox_y的IoU,若IoU大于阈值T,则剔除该bbox_y,最终保留bbox_x,从list_k中取出;

4:选择list_k中top2 cls score,重复step-3中的迭代操作,直至list_k中所有bbox都完成筛选;

5:对每个集合的list_k,重复step-3、4中的迭代操作,直至所有list_k都完成筛选;
 

代码:

# --------------------------------------------------------
# Fast R-CNN
# Copyright (c) 2015 Microsoft
# Licensed under The MIT License [see LICENSE for details]
# Written by Ross Girshick
# --------------------------------------------------------
 
import numpy as np
 
def py_cpu_nms(dets, thresh):
    """Pure Python NMS baseline."""
    x1 = dets[:, 0]                     # pred bbox top_x
    y1 = dets[:, 1]                     # pred bbox top_y
    x2 = dets[:, 2]                     # pred bbox bottom_x
    y2 = dets[:, 3]                     # pred bbox bottom_y
    scores = dets[:, 4]              # pred bbox cls score
 
    areas = (x2 - x1 + 1) * (y2 - y1 + 1)    # pred bbox areas
    order = scores.argsort()[::-1]              # 对pred bbox按score做降序排序,对应step-2
 
    keep = []    # NMS后,保留的pred bbox
    while order.size > 0:
        i = order[0]          # top-1 score bbox
        keep.append(i)   # top-1 score的话,自然就保留了
        xx1 = np.maximum(x1[i], x1[order[1:]])   # top-1 bbox(score最大)与order中剩余bbox计算NMS
        yy1 = np.maximum(y1[i], y1[order[1:]])
        xx2 = np.minimum(x2[i], x2[order[1:]])
        yy2 = np.minimum(y2[i], y2[order[1:]])
 
        w = np.maximum(0.0, xx2 - xx1 + 1)
        h = np.maximum(0.0, yy2 - yy1 + 1)
        inter = w * h
        ovr = inter / (areas[i] + areas[order[1:]] - inter)      # 无处不在的IoU计算~~~
 
        inds = np.where(ovr <= thresh)[0]     # 这个操作可以对代码断点调试理解下,结合step-3,我们希望剔除所有与当前top-1 bbox IoU > thresh的冗余bbox,那么保留下来的bbox,自然就是ovr <= thresh的非冗余bbox,其inds保留下来,作进一步筛选
        order = order[inds + 1]   # 保留有效bbox,就是这轮NMS未被抑制掉的幸运儿,为什么 + 1?因为ind = 0就是这轮NMS的top-1,剩余有效bbox在IoU计算中与top-1做的计算,inds对应回原数组,自然要做 +1 的映射,接下来就是step-4的循环
 
    return keep    # 最终NMS结果返回
 
if __name__ == '__main__':
    dets = np.array([[100,120,170,200,0.98],
                     [20,40,80,90,0.99],
                     [20,38,82,88,0.96],
                     [200,380,282,488,0.9],
                     [19,38,75,91, 0.8]])
 
    py_cpu_nms(dets, 0.5)

猜你喜欢

转载自blog.csdn.net/weixin_64043217/article/details/131764223
今日推荐