Depth = torch.zeros(batch_size, img.shape[1], 1, *self.image_size).to(points[0].device)
画像の深度情報を表すすべてゼロのテンソルを作成します。各パラメータの意味:
batch_size
:バッチサイズ。同時に処理される画像の数を示します。
img.shape[1]
: 画像テンソルの 2 番目の次元のサイズを取得します。一般に、これは通常、画像のチャンネル数を表します。たとえば、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: 「tools」という名前のモジュールがありません
export PYTHONPATH=./ 定位到当前路径
1. NMSプロセス
1: 検出されたすべての Output_bbox を cls スコアで分割します (たとえば、pascal voc は 20 のカテゴリに分割されます。つまり、output_bbox は、対応する cls スコアに従って 21 セットと 1 つの bg カテゴリに分割されますが、bg カテゴリは NMS である必要はありません)。
2: 各セット内の各 bbox の cls スコアに従って降順に並べ、降順の list_k を取得します。
3: list_k の最大 cls スコアから開始して、リスト内の bbox_x と他の bbox_y の IoU を計算し、IoU がしきい値 T より大きい場合、bbox_y を削除し、最後に bbox_x を保持して list_k から削除します。
4: list_k 内の上位 2 cls スコアを選択し、list_k 内のすべての bbox がスクリーニングされるまでステップ 3 の反復操作を繰り返します。
5: すべての list_k がフィルタリングされるまで、各コレクションの list_k に対してステップ 3 と 4 の反復操作を繰り返します。
コード:
# --------------------------------------------------------
# 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)