目標(biāo)檢測(cè)基礎(chǔ)
錨框
目標(biāo)檢測(cè)算法通常會(huì)在輸入圖像中采樣大量的區(qū)域,然后判斷這些區(qū)域中是否包含我們感興趣的目標(biāo)剧防,并調(diào)整區(qū)域邊緣從而更準(zhǔn)確地預(yù)測(cè)目標(biāo)的真實(shí)邊界框(ground-truth bounding box)植锉。不同的模型使用的區(qū)域采樣方法可能不同俊庇。這里我們介紹其中的一種方法:它以每個(gè)像素為中心生成多個(gè)大小和寬高比(aspect ratio)不同的邊界框拣展。這些邊界框被稱為錨框(anchor box)。
生成多個(gè)錨框
假設(shè)輸入圖像高為 按脚,寬為于毙。我們分別以圖像的每個(gè)像素為中心生成不同形狀的錨框。設(shè)大小為且寬高比為辅搬,那么錨框的寬和高將分別為和唯沮。當(dāng)中心位置給定時(shí),已知寬和高的錨框是確定的伞辛。
下面我們分別設(shè)定好一組大小和一組寬高比烂翰。如果以每個(gè)像素為中心時(shí)使用所有的大小與寬高比的組合,輸入圖像將一共得到個(gè)錨框蚤氏。雖然這些錨框可能覆蓋了所有的真實(shí)邊界框甘耿,但計(jì)算復(fù)雜度容易過高。因此竿滨,我們通常只對(duì)包含或的大小與寬高比的組合感興趣佳恬,即
也就是說,以相同像素為中心的錨框的數(shù)量為于游。對(duì)于整個(gè)輸入圖像毁葱,我們將一共生成個(gè)錨框。
代碼實(shí)現(xiàn)
numpy知識(shí)點(diǎn):
-
np.meshgrid(x, y)
:使用np.meshgrid()函數(shù)生成網(wǎng)格點(diǎn)坐標(biāo)矩陣贰剥,之后可以用np.stack()
指定軸axis還原各點(diǎn)坐標(biāo)倾剿。
代碼示例:>>>x = np.arange(2) >>>y = np.arange(3) >>>x_1, y_1 = np.meshgrid(x, y) >>>print(x_1) [[0 1] [0 1] [0 1]] >>>print(y_1) [[0 0] [1 1] [2 2]] >>>s_x = x_1.reshape(-1) >>>s_y = y_1.reshape(-1) >>>np.stack((s_x, s_y), axis=1) array([[0, 0], [1, 0], [0, 1], [1, 1], [0, 2], [1, 2]]) #得到了x列表中所有元素與y列表中所有元素組成的所有二維坐標(biāo)
生成錨框的函數(shù):指定輸入、一組大小和一組寬高比,該函數(shù)將返回輸入的所有錨框
def MultiBoxPrior(feature_map, sizes=[0.75, 0.5, 0.25], ratios=[1, 2, 0.5]):
"""
# 按照「9.4.1. 生成多個(gè)錨框」所講的實(shí)現(xiàn), anchor表示成(xmin, ymin, xmax, ymax).
https://zh.d2l.ai/chapter_computer-vision/anchor.html
Args:
feature_map: torch tensor, Shape: [N, C, H, W]. [輸入圖像的個(gè)數(shù)前痘,三個(gè)通道的值凛捏,高度,寬度]
sizes: List of sizes (0~1) of generated MultiBoxPriores.
ratios: List of aspect ratios (non-negative) of generated MultiBoxPriores.
Returns:
anchors of shape (1, num_anchors, 4). 由于batch里每個(gè)都一樣, 所以第一維為1 第三維是4:生成的錨框左上角和右下角坐標(biāo)值(xmin, ymin, xmax, ymax)共4個(gè)
"""
pairs = [] # pair of (size, sqrt(ration))
# 生成n + m -1個(gè)框
for r in ratios:
pairs.append([sizes[0], math.sqrt(r)])
for s in sizes[1:]:
pairs.append([s, math.sqrt(ratios[0])])
pairs = np.array(pairs)
# 生成相對(duì)于坐標(biāo)中心點(diǎn)的框(x,y,x,y)
ss1 = pairs[:, 0] * pairs[:, 1] # size * sqrt(ration)
ss2 = pairs[:, 0] / pairs[:, 1] # size / sqrt(ration)
base_anchors = np.stack([-ss1, -ss2, ss1, ss2], axis=1) / 2
#將坐標(biāo)點(diǎn)和anchor組合起來生成hw(n+m-1)個(gè)框輸出
h, w = feature_map.shape[-2:]
shifts_x = np.arange(0, w) / w #除以w和h是因?yàn)樽詈笠鄬?duì)于圖片大小為1來生成1芹缔,所以要標(biāo)準(zhǔn)化
shifts_y = np.arange(0, h) / h
shift_x, shift_y = np.meshgrid(shifts_x, shifts_y) #使用np.meshgrid()函數(shù)生成網(wǎng)格點(diǎn)坐標(biāo)矩陣坯癣,之后可以用np.stack()指定軸axis還原各點(diǎn)坐標(biāo)
shift_x = shift_x.reshape(-1) #保存了所有x軸坐標(biāo)
shift_y = shift_y.reshape(-1)
shifts = np.stack((shift_x, shift_y, shift_x, shift_y), axis=1) #shifts中保存了所有像素點(diǎn)的坐標(biāo)值(有兩個(gè)x兩個(gè)y是因?yàn)榉謩e是左上點(diǎn)和右下點(diǎn))
anchors = shifts.reshape((-1, 1, 4)) + base_anchors.reshape((1, -1, 4)) #第0維保存所有anchors個(gè)數(shù),第1維保存所有像素點(diǎn)個(gè)數(shù)最欠,相加時(shí)會(huì)自動(dòng)在設(shè)為-1的維上擴(kuò)展
return torch.tensor(anchors, dtype=torch.float32).view(1, -1, 4)
交并比(IoU)
一種衡量錨框和真實(shí)邊界框之間相似度的方法:兩個(gè)邊界框相交面積與相并面積之比示罗。
代碼實(shí)現(xiàn)
pytorch知識(shí)點(diǎn):
-
torch.unsqueeze(axis)
:在指定軸上擴(kuò)充維度 -
torch.clamp(input, min, max, out=None) → Tensor
:將輸入input張量每個(gè)元素的夾緊到區(qū)間 [min,max],并返回結(jié)果到一個(gè)新張量芝硬。(原張量中超出最小或最大限制的元素蚜点,將被置為最小或最大值)
求得交并比的函數(shù):
def compute_intersection(set_1, set_2):
"""
計(jì)算anchor之間的交集
Args:
set_1: a tensor of dimensions (n1, 4), anchor表示成(xmin, ymin, xmax, ymax)
set_2: a tensor of dimensions (n2, 4), anchor表示成(xmin, ymin, xmax, ymax)
Returns:
intersection of each of the boxes in set 1 with respect to each of the boxes in set 2, shape: (n1, n2)
"""
# PyTorch auto-broadcasts singleton dimensions
lower_bounds = torch.max(set_1[:, :2].unsqueeze(1), set_2[:, :2].unsqueeze(0)) # (n1, n2, 2)
upper_bounds = torch.min(set_1[:, 2:].unsqueeze(1), set_2[:, 2:].unsqueeze(0)) # (n1, n2, 2)
intersection_dims = torch.clamp(upper_bounds - lower_bounds, min=0) # (n1, n2, 2) #利用clamp()函數(shù)限制upper-lower大于等于0(若小于0則置為0)
return intersection_dims[:, :, 0] * intersection_dims[:, :, 1] # (n1, n2)
def compute_jaccard(set_1, set_2):
"""
計(jì)算anchor之間的Jaccard系數(shù)(IoU)
Args:
set_1: a tensor of dimensions (n1, 4), anchor表示成(xmin, ymin, xmax, ymax)
set_2: a tensor of dimensions (n2, 4), anchor表示成(xmin, ymin, xmax, ymax)
Returns:
Jaccard Overlap of each of the boxes in set 1 with respect to each of the boxes in set 2, shape: (n1, n2)
"""
# Find intersections
intersection = compute_intersection(set_1, set_2) # (n1, n2)
# Find areas of each box in both sets
areas_set_1 = (set_1[:, 2] - set_1[:, 0]) * (set_1[:, 3] - set_1[:, 1]) # (n1)
areas_set_2 = (set_2[:, 2] - set_2[:, 0]) * (set_2[:, 3] - set_2[:, 1]) # (n2)
# Find the union
# PyTorch auto-broadcasts singleton dimensions
union = areas_set_1.unsqueeze(1) + areas_set_2.unsqueeze(0) - intersection # (n1, n2) #直接用兩個(gè)框的面積減去交集的面積就得到并集的面積
return intersection / union # (n1, n2)
標(biāo)注訓(xùn)練集的錨框
-
在訓(xùn)練集中,我們將每個(gè)錨框視為一個(gè)訓(xùn)練樣本吵取。為了訓(xùn)練目標(biāo)檢測(cè)模型禽额,我們需要為每個(gè)錨框標(biāo)注兩類標(biāo)簽:
- 錨框所含目標(biāo)的類別,簡(jiǎn)稱類別皮官。
- 真實(shí)邊界框相對(duì)錨框的偏移量,簡(jiǎn)稱偏移量(offset)实辑。
在目標(biāo)檢測(cè)時(shí)捺氢,我們首先生成多個(gè)錨框,然后為每個(gè)錨框預(yù)測(cè)類別以及偏移量剪撬,接著根據(jù)預(yù)測(cè)的偏移量調(diào)整錨框位置從而得到預(yù)測(cè)邊界框摄乒,最后篩選需要輸出的預(yù)測(cè)邊界框。
-
為錨框分配與其相似的真實(shí)邊界框的方法:
- 假設(shè)圖像中錨框分別為残黑,真實(shí)邊界框分別為馍佑,且。定義矩陣梨水,其中第行第列的元素為錨框與真實(shí)邊界框的交并比拭荤。
- 首先,找出矩陣中最大元素疫诽,并將該元素的行索引與列索引分別記為舅世。為錨框分配真實(shí)邊界框。顯然奇徒,錨框和真實(shí)邊界框在所有的“錨框—真實(shí)邊界框”的配對(duì)中相似度最高雏亚。接下來,將矩陣中第行和第列上的所有元素丟棄摩钙。找出矩陣中剩余的最大元素罢低,并將該元素的行索引與列索引分別記為。為錨框分配真實(shí)邊界框胖笛,再將矩陣中第行和第列上的所有元素丟棄网持。此時(shí)矩陣中已有兩行兩列的元素被丟棄宜肉。依此類推,直到矩陣中所有列元素全部被丟棄翎碑。這個(gè)時(shí)候谬返,我們已為個(gè)錨框各分配了一個(gè)真實(shí)邊界框。
- 接下來日杈,我們只遍歷剩余的個(gè)錨框:給定其中的錨框遣铝,根據(jù)矩陣的第行找到與交并比最大的真實(shí)邊界框,且只有當(dāng)該交并比大于預(yù)先設(shè)定的閾值(threshold)時(shí)莉擒,才為錨框分配真實(shí)邊界框酿炸;而若小于閾值,則將該錨框標(biāo)記為背景涨冀。
- 舉例說明:
如上圖(左)所示填硕,假設(shè)矩陣中最大值為,我們將為錨框分配真實(shí)邊界框鹿鳖。然后扁眯,丟棄矩陣中第2行和第3列的所有元素,找出剩余陰影部分的最大元素翅帜,為錨框分配真實(shí)邊界框姻檀。接著如上圖(中)所示,丟棄矩陣中第7行和第1列的所有元素涝滴,找出剩余陰影部分的最大元素绣版,為錨框分配真實(shí)邊界框。最后如上圖(右)所示歼疮,丟棄矩陣中第5行和第4列的所有元素杂抽,找出剩余陰影部分的最大元素,為錨框分配真實(shí)邊界框韩脏。之后缩麸,我們只需遍歷除去的剩余錨框,并根據(jù)閾值判斷是否為剩余錨框分配真實(shí)邊界框骤素。
-
標(biāo)注錨框的類別和偏移量的方法:
如果一個(gè)錨框被分配了真實(shí)邊界框匙睹,將錨框的類別設(shè)為的類別,并根據(jù)和的中心坐標(biāo)的相對(duì)位置以及兩個(gè)框的相對(duì)大小為錨框標(biāo)注偏移量济竹。
如果一個(gè)錨框沒有被分配真實(shí)邊界框痕檬,我們只需將該錨框的類別設(shè)為背景。類別為背景的錨框通常被稱為負(fù)類錨框送浊,其余則被稱為正類錨框梦谜。
-
由于數(shù)據(jù)集中各個(gè)框的位置和大小各異,因此這些相對(duì)位置和相對(duì)大小通常需要一些特殊變換,才能使偏移量的分布更均勻從而更容易擬合唁桩。設(shè)錨框及其被分配的真實(shí)邊界框的中心坐標(biāo)分別為和闭树,和的寬分別為和,高分別為和荒澡,一個(gè)常用的技巧是將的偏移量標(biāo)注為
其中常數(shù)的默認(rèn)值為报辱。
代碼實(shí)現(xiàn)
def assign_anchor(bb, anchor, jaccard_threshold=0.5):
"""
# 按照「9.4.1. 生成多個(gè)錨框」圖9.3所講為每個(gè)anchor分配真實(shí)的bb, anchor表示成歸一化(xmin, ymin, xmax, ymax).
https://zh.d2l.ai/chapter_computer-vision/anchor.html
Args:
bb: 真實(shí)邊界框(bounding box), shape:(nb, 4)
anchor: 待分配的anchor, shape:(na, 4)
jaccard_threshold: 預(yù)先設(shè)定的閾值
Returns:
assigned_idx: shape: (na, ), 每個(gè)anchor分配的真實(shí)bb對(duì)應(yīng)的索引, 若未分配任何bb則為-1
"""
na = anchor.shape[0]
nb = bb.shape[0]
jaccard = compute_jaccard(anchor, bb).detach().cpu().numpy() # shape: (na, nb)
assigned_idx = np.ones(na) * -1 # 存放標(biāo)簽初始全為-1
# 先為每個(gè)bb分配一個(gè)anchor(不要求滿足jaccard_threshold)
jaccard_cp = jaccard.copy()
for j in range(nb):
i = np.argmax(jaccard_cp[:, j])
assigned_idx[i] = j
jaccard_cp[i, :] = float("-inf") # 賦值為負(fù)無窮, 相當(dāng)于去掉這一行
# 處理還未被分配的anchor, 要求滿足jaccard_threshold
for i in range(na):
if assigned_idx[i] == -1:
j = np.argmax(jaccard[i, :])
if jaccard[i, j] >= jaccard_threshold:
assigned_idx[i] = j
return torch.tensor(assigned_idx, dtype=torch.long)
def xy_to_cxcy(xy):
"""
將(x_min, y_min, x_max, y_max)形式的anchor轉(zhuǎn)換成(center_x, center_y, w, h)形式的.
https://github.com/sgrvinod/a-PyTorch-Tutorial-to-Object-Detection/blob/master/utils.py
Args:
xy: bounding boxes in boundary coordinates, a tensor of size (n_boxes, 4)
Returns:
bounding boxes in center-size coordinates, a tensor of size (n_boxes, 4)
"""
return torch.cat([(xy[:, 2:] + xy[:, :2]) / 2, # c_x, c_y
xy[:, 2:] - xy[:, :2]], 1) # w, h
def MultiBoxTarget(anchor, label):
"""
# 按照「9.4.1. 生成多個(gè)錨框」所講的實(shí)現(xiàn), anchor表示成歸一化(xmin, ymin, xmax, ymax).
https://zh.d2l.ai/chapter_computer-vision/anchor.html
Args:
anchor: torch tensor, 輸入的錨框, 一般是通過MultiBoxPrior生成, shape:(1,錨框總數(shù)单山,4)
label: 真實(shí)標(biāo)簽, shape為(bn, 每張圖片最多的真實(shí)錨框數(shù), 5)
第二維中碍现,如果給定圖片沒有這么多錨框, 可以先用-1填充空白, 最后一維中的元素為[類別標(biāo)簽, 四個(gè)坐標(biāo)值]
Returns:
列表, [bbox_offset, bbox_mask, cls_labels]
bbox_offset: 每個(gè)錨框的標(biāo)注偏移量,形狀為(bn米奸,錨框總數(shù)*4)
bbox_mask: 形狀同bbox_offset, 每個(gè)錨框的掩碼, 一一對(duì)應(yīng)上面的偏移量, 負(fù)類錨框(背景)對(duì)應(yīng)的掩碼均為0, 正類錨框的掩碼均為1
cls_labels: 每個(gè)錨框的標(biāo)注類別, 其中0表示為背景, 形狀為(bn昼接,錨框總數(shù))
"""
assert len(anchor.shape) == 3 and len(label.shape) == 3
bn = label.shape[0]
def MultiBoxTarget_one(anc, lab, eps=1e-6):
"""
MultiBoxTarget函數(shù)的輔助函數(shù), 處理batch中的一個(gè)
Args:
anc: shape of (錨框總數(shù), 4)
lab: shape of (真實(shí)錨框數(shù), 5), 5代表[類別標(biāo)簽, 四個(gè)坐標(biāo)值]
eps: 一個(gè)極小值, 防止log0
Returns:
offset: (錨框總數(shù)*4, )
bbox_mask: (錨框總數(shù)*4, ), 0代表背景, 1代表非背景
cls_labels: (錨框總數(shù), 4), 0代表背景
"""
an = anc.shape[0]
# 變量的意義
assigned_idx = assign_anchor(lab[:, 1:], anc) # (錨框總數(shù), )
print("a: ", assigned_idx.shape)
print(assigned_idx)
bbox_mask = ((assigned_idx >= 0).float().unsqueeze(-1)).repeat(1, 4) # (錨框總數(shù), 4)
print("b: " , bbox_mask.shape)
print(bbox_mask)
cls_labels = torch.zeros(an, dtype=torch.long) # 0表示背景
assigned_bb = torch.zeros((an, 4), dtype=torch.float32) # 所有anchor對(duì)應(yīng)的bb坐標(biāo)
for i in range(an):
bb_idx = assigned_idx[i]
if bb_idx >= 0: # 即非背景
cls_labels[i] = lab[bb_idx, 0].long().item() + 1 # 注意要加一
assigned_bb[i, :] = lab[bb_idx, 1:]
# 如何計(jì)算偏移量
center_anc = xy_to_cxcy(anc) # (center_x, center_y, w, h)
center_assigned_bb = xy_to_cxcy(assigned_bb)
offset_xy = 10.0 * (center_assigned_bb[:, :2] - center_anc[:, :2]) / center_anc[:, 2:]
offset_wh = 5.0 * torch.log(eps + center_assigned_bb[:, 2:] / center_anc[:, 2:])
offset = torch.cat([offset_xy, offset_wh], dim = 1) * bbox_mask # (錨框總數(shù), 4)
return offset.view(-1), bbox_mask.view(-1), cls_labels
# 組合輸出
batch_offset = []
batch_mask = []
batch_cls_labels = []
for b in range(bn):
offset, bbox_mask, cls_labels = MultiBoxTarget_one(anchor[0, :, :], label[b, :, :])
batch_offset.append(offset)
batch_mask.append(bbox_mask)
batch_cls_labels.append(cls_labels)
bbox_offset = torch.stack(batch_offset)
bbox_mask = torch.stack(batch_mask)
cls_labels = torch.stack(batch_cls_labels)
return [bbox_offset, bbox_mask, cls_labels]
輸出預(yù)測(cè)邊界框
- 在模型預(yù)測(cè)階段,我們先為圖像生成多個(gè)錨框悴晰,并為這些錨框一一預(yù)測(cè)類別和偏移量慢睡。隨后,我們根據(jù)錨框及其預(yù)測(cè)偏移量得到預(yù)測(cè)邊界框铡溪。
- 當(dāng)錨框數(shù)量較多時(shí)漂辐,同一個(gè)目標(biāo)上可能會(huì)輸出較多相似的預(yù)測(cè)邊界框。為了使結(jié)果更加簡(jiǎn)潔佃却,我們可以移除相似的預(yù)測(cè)邊界框者吁。常用的方法叫作非極大值抑制(non-maximum suppression,NMS):
- 對(duì)于一個(gè)預(yù)測(cè)邊界框饲帅,模型會(huì)計(jì)算各個(gè)類別的預(yù)測(cè)概率键菱。設(shè)其中最大的預(yù)測(cè)概率為炬太,該概率所對(duì)應(yīng)的類別即的預(yù)測(cè)類別锁蠕。我們也將稱為預(yù)測(cè)邊界框的置信度稽揭。在同一圖像上注祖,我們將預(yù)測(cè)類別非背景的預(yù)測(cè)邊界框按置信度從高到低排序卒蘸,得到列表囚玫。
- 從中選取置信度最高的預(yù)測(cè)邊界框作為基準(zhǔn)坑雅,將所有與的交并比大于某閾值的非基準(zhǔn)預(yù)測(cè)邊界框從中移除实檀。這里的閾值是預(yù)先設(shè)定的超參數(shù)惶洲。此時(shí),保留了置信度最高的預(yù)測(cè)邊界框并移除了與其相似的其他預(yù)測(cè)邊界框膳犹。
- 接下來恬吕,從中選取置信度第二高的預(yù)測(cè)邊界框作為基準(zhǔn),將所有與的交并比大于某閾值的非基準(zhǔn)預(yù)測(cè)邊界框從中移除须床。重復(fù)這一過程铐料,直到中所有的預(yù)測(cè)邊界框都曾作為基準(zhǔn)。此時(shí)中任意一對(duì)預(yù)測(cè)邊界框的交并比都小于閾值。最終钠惩,輸出列表中的所有預(yù)測(cè)邊界框柒凉。
代碼實(shí)現(xiàn)
from collections import namedtuple
Pred_BB_Info = namedtuple("Pred_BB_Info", ["index", "class_id", "confidence", "xyxy"])
def non_max_suppression(bb_info_list, nms_threshold = 0.5):
"""
非極大抑制處理預(yù)測(cè)的邊界框
Args:
bb_info_list: Pred_BB_Info的列表, 包含預(yù)測(cè)類別、置信度等信息
nms_threshold: 閾值
Returns:
output: Pred_BB_Info的列表, 只保留過濾后的邊界框信息
"""
output = []
# 先根據(jù)置信度從高到低排序
sorted_bb_info_list = sorted(bb_info_list, key = lambda x: x.confidence, reverse=True)
# 循環(huán)遍歷刪除冗余輸出
while len(sorted_bb_info_list) != 0:
best = sorted_bb_info_list.pop(0)
output.append(best)
if len(sorted_bb_info_list) == 0:
break
bb_xyxy = []
for bb in sorted_bb_info_list:
bb_xyxy.append(bb.xyxy)
iou = compute_jaccard(torch.tensor([best.xyxy]),
torch.tensor(bb_xyxy))[0] # shape: (len(sorted_bb_info_list), )
n = len(sorted_bb_info_list)
sorted_bb_info_list = [sorted_bb_info_list[i] for i in range(n) if iou[i] <= nms_threshold]
return output
def MultiBoxDetection(cls_prob, loc_pred, anchor, nms_threshold = 0.5):
"""
# 按照「9.4.1. 生成多個(gè)錨框」所講的實(shí)現(xiàn), anchor表示成歸一化(xmin, ymin, xmax, ymax).
https://zh.d2l.ai/chapter_computer-vision/anchor.html
Args:
cls_prob: 經(jīng)過softmax后得到的各個(gè)錨框的預(yù)測(cè)概率, shape:(bn, 預(yù)測(cè)總類別數(shù)+1, 錨框個(gè)數(shù))
loc_pred: 預(yù)測(cè)的各個(gè)錨框的偏移量, shape:(bn, 錨框個(gè)數(shù)*4)
anchor: MultiBoxPrior輸出的默認(rèn)錨框, shape: (1, 錨框個(gè)數(shù), 4)
nms_threshold: 非極大抑制中的閾值
Returns:
所有錨框的信息, shape: (bn, 錨框個(gè)數(shù), 6)
每個(gè)錨框信息由[class_id, confidence, xmin, ymin, xmax, ymax]表示
class_id=-1 表示背景或在非極大值抑制中被移除了
"""
assert len(cls_prob.shape) == 3 and len(loc_pred.shape) == 2 and len(anchor.shape) == 3
bn = cls_prob.shape[0]
def MultiBoxDetection_one(c_p, l_p, anc, nms_threshold = 0.5):
"""
MultiBoxDetection的輔助函數(shù), 處理batch中的一個(gè)
Args:
c_p: (預(yù)測(cè)總類別數(shù)+1, 錨框個(gè)數(shù))
l_p: (錨框個(gè)數(shù)*4, )
anc: (錨框個(gè)數(shù), 4)
nms_threshold: 非極大抑制中的閾值
Return:
output: (錨框個(gè)數(shù), 6)
"""
pred_bb_num = c_p.shape[1]
anc = (anc + l_p.view(pred_bb_num, 4)).detach().cpu().numpy() # 加上偏移量
confidence, class_id = torch.max(c_p, 0)
confidence = confidence.detach().cpu().numpy()
class_id = class_id.detach().cpu().numpy()
pred_bb_info = [Pred_BB_Info(
index = i,
class_id = class_id[i] - 1, # 正類label從0開始
confidence = confidence[i],
xyxy=[*anc[i]]) # xyxy是個(gè)列表
for i in range(pred_bb_num)]
# 正類的index
obj_bb_idx = [bb.index for bb in non_max_suppression(pred_bb_info, nms_threshold)]
output = []
for bb in pred_bb_info:
output.append([
(bb.class_id if bb.index in obj_bb_idx else -1.0),
bb.confidence,
*bb.xyxy
])
return torch.tensor(output) # shape: (錨框個(gè)數(shù), 6)
batch_output = []
for b in range(bn):
batch_output.append(MultiBoxDetection_one(cls_prob[b], loc_pred[b], anchor[0], nms_threshold))
return torch.stack(batch_output)
多尺度目標(biāo)檢測(cè)
- 如果以圖像每個(gè)像素為中心都生成錨框篓跛,很容易生成過多錨框而造成計(jì)算量過大膝捞。減少錨框個(gè)數(shù)的一種簡(jiǎn)單方法是在輸入圖像中均勻采樣一小部分像素,并以采樣的像素為中心生成錨框愧沟。此外蔬咬,在不同尺度下,我們可以生成不同數(shù)量和不同大小的錨框央渣。
- 較小目標(biāo)比較大目標(biāo)在圖像上出現(xiàn)位置的可能性更多计盒。因此,當(dāng)使用較小錨框來檢測(cè)較小目標(biāo)時(shí)芽丹,我們可以采樣較多的區(qū)域北启;而當(dāng)使用較大錨框來檢測(cè)較大目標(biāo)時(shí),我們可以采樣較少的區(qū)域拔第。