錨框


生成多個錨框(contrib.nd.MultiBoxPrior)

每個像素點構造多個錨框,假設設定一組大小s1,...,sn和一組寬高比r1,...,rm。然后每個像素組合,就有hwnm個錨框,一般情況下,只要s1,和r1的組合。即:

共wh(n+m-1)個錨框

 

交並比(IoU)

 

面試中有提問過。。。

 

標注訓練集的錨框

為了訓練檢測模型,為每一個錨框標注兩類信息(類別,與真實邊界框的偏移量offset)

目標檢測過程:

  • 生成多個錨框
  • 計算每個錨框的預測類別和偏移量
  • 調整錨框位置
  • 篩選需要輸出的預測邊界框

假設:

生成的錨框為A1,A2,...,Ana,真實邊界框為B1,B2,...,Bnb。(na>=nb)

矩陣Xna*nb 是對應錨框與真實邊界框的IoU,然后不停篩maxX即可,多余的na-nb,橫着查max,看是否達到閥值:

生成錨框的類別就弄好了,偏移量的計算:

其中:

A : 寬高 wa , ha , 中心 (xa , ya)

B : 寬高 wb , hb , 中心 (xb , yb)

若 A 沒有分配真實框 B,則 A 為背景(負類錨框)

 

%matplotlib inline
import sys
import gluonbook as gb
from mxnet import image,nd,gluon,contrib
import numpy as np
np.set_printoptions(2)

img = image.imread('./catdog.jpg').asnumpy()
h, w = img.shape[0:2]
h, w

X = nd.random.uniform(shape=(1,3,h,w))
Y = contrib.nd.MultiBoxPrior(X,sizes=[0.75,0.5,0.25],ratios=[1,2,0.5])
Y.shape

boxes = Y.reshape((h,w,5,4))
boxes[25,250,:,:]

# 畫出一個像素的所有錨框
def show_bboxes(axes, bboxes, labels=None, colors=None):
    def _make_list(obj, default_values=None):
        if obj is None:
            obj = default_values
        elif not isinstance(obj, (list, tuple)):
            obj = [obj]
        return obj

    labels = _make_list(labels)
    colors = _make_list(colors, ['b', 'g', 'r', 'm', 'c'])
    for i, bbox in enumerate(bboxes):
        color = colors[i % len(colors)]
        rect = gb.bbox_to_rect(bbox.asnumpy(), color)
        axes.add_patch(rect)
        if labels and len(labels) > i:
            text_color = 'k' if color == 'w' else 'w'
            axes.text(rect.xy[0], rect.xy[1], labels[i],
                      va='center', ha='center', fontsize=9, color=text_color,
                      bbox=dict(facecolor=color, lw=0))

gb.set_figsize()
bbox_scale = nd.array((w,h,w,h))
fig = gb.plt.imshow(img)
show_bboxes(fig.axes,boxes[250,250,:,:]*bbox_scale,['s=0.75, r=1', 's=0.5, r=1', 's=0.25, r=1', 's=0.75, r=2',
             's=0.75, r=0.5'])

labels = contrib.nd.MultiBoxTarget(anchors.expand_dims(axis=0),
                                   ground_truth.expand_dims(axis=0),
                                   nd.zeros((1, 3, 5)))
# 標注類別
labels[2]

# mask變量,用來過濾掉背景的偏移量
labels[1]

# 偏移量
labels[0]

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM