1708.
Kaiming He, Focal Loss for Dense Object Detection.
为解决1-stage训练时正负例不平衡
现象,对CE损失函数添加权重因子:
balanced cross entropy:从样本分布角度添加
focal loss:从样本分类难易程度出发添加(使loss聚焦于难分样本)
1506.
Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks.
1506.01497
[paper]
其中,
i i i:某个minibatch中,第 i i i个anchor
p i p_i pi:二分类的预测值 p i ∗ p_i^* pi∗:gt { 1 I o U ( g t , a n c h o r ) > 0.7 0 I o U ( g t , a n c h o r ) < 0.3 \begin{cases}1 &IoU(gt,anchor)>0.7 \\ 0 &IoU(gt,anchor)<0.3\end{cases} {10IoU(gt,anchor)>0.7IoU(gt,anchor)<0.3
t i t_i ti:回归位置的预测值 t i ∗ t_i^* ti∗:gt,计算如下
:
[zhihu]
AnchorTargetCreator
:生成正负anchor,计算 r p n _ l o s s rpn\_loss rpn_loss.
ProposalCreator
:生成RoIs
(region of interests)作为训练样本传给RoIHead.
NMS
,得到2000个RoIs.RoI Pooling
:将不同尺寸的特征图区域(128个sample_rois)pooling成固定的尺寸 7 ∗ 7 7*7 7∗7.
FC 21
:用来分类,预测RoIs属于哪个类别(20个类 + + + 背景)
FC 84
:用来回归(21个类 × × × 4个位置参数)
ProposalTargetCreator
:选择128个正、负sample_rois用以训练.计算roi_loss
.Problem:在Test阶段,NMS是按类别分别进行NMS吗?
(是的)
NMS
;RoIHead_Test时,再做一遍回归
调整;RoIHead_Test时,再做一遍L o s s = L o s s r p n ( c l s 2 , r e g ) + L o s s r o i ( c l s 21 , r e g ) Loss=Loss_{rpn}(cls_2,reg)+Loss_{roi}(cls_{21},reg) Loss=Lossrpn(cls2,reg)+Lossroi(cls21,reg)
输入:(boxes,nms_threshold) >> boxes
目的:去掉 模型检测出的 多余框
步骤:
(1)将boxes按得分降序;
(2.1)若boxes非空,取首项为best,并记录
best;否则结束
(2.2)计算best与剩余boxes的IOUs,找出IOUs中所有小于阈值的索引为inds;
(2.3)boxes = boxes[inds+1]
,重复(2.1);
import numpy as np
def nms(dets, thresh):
"""Pure Python NMS baseline."""
x1 = dets[:, 0]
y1 = dets[:, 1]
x2 = dets[:, 2]
y2 = dets[:, 3]
scores = dets[:, 4]
areas = (x2 - x1 + 1) * (y2 - y1 + 1)
order = scores.argsort()[::-1] # 得分降序,取最高
keep = []
while order.size > 0:
i = order[0]
keep.append(i)
xx1 = np.maximum(x1[i], x1[order[1:]]) #并集Union
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] # 只保留iou低的索引列表!!!若空则order空
order = order[inds + 1]
return keep
######
boxes=np.array([[100,100,210,210,0.72],
[250,250,420,420,0.8],
[220,220,320,330,0.92],
[100,100,210,210,0.72],
[230,240,325,330,0.61],
[220,230,315,340,0.9]])
nms(boxes,0.5)