SSD源码和论文学习笔记

源码学习

Dtype

caffe 的Dtype设置,Net, Solver都是模板,最后的设置其实是通过caffe.cpp实现的,都是用float

lmdb生成

主要是对数据进行检查并生成lmdb文件。

用户接口:create_data.sh

调用关系:create_data.sh -> create_annoset.py(命令行参数解析和文件路径检查) -> convert_annoset.cpp(创建lmdb或其它db) -> io.cpp(读取图像和标签)

AnnoDataLayer

BatchSampler

由于sample的参数是与物体bbox的最小jaccard overlap,所以与标注内容有关;

BatchSampler或者Sampler属于transfomation的一种,是一种特殊的crop操作

线程载入数据流程

LayerSetup->StartInternalThread->InternelThreadEntry->load_batch()

对数据进行在线增广,核心在于Expand和sample,需要调用TransformAnnotation,进而调用resize, project, mirror等bbox操作;

在线/离线增广

在线增广,通过prefetch充分利用网络训练时空闲的CPU,相比于离线增广,节省磁盘空间和增广时间,可以随时修改增广参数,非常灵活,通过概率设置,增广的量会更多。

离线增广的优点主要是可以利用一些现有的增广程序,不必加入到caffe的程序中

caffe及ssd都是支持在线增广的

BBoxUtil

理解ProjectBBox,LocateBBox是其逆过程(参见纸上笔记)

SSD网络解析之bbox_util](https://blog.csdn.net/qq_21368481/article/details/82227989)

python接口使用caffe.proto参数

参考gen.py的作者部分,包括获取非Parameter的参数

VisualizeBBox

在网络中det_out层加上可视化参数会报错

涉及魔数4,还未修改

当前我的可视化是自己写的函数

CodeType

PriorBoxParameter_CodeType 是priorbox的描述(编码)方式,但编码方式其实直接对应了后面的回归模型的处理方式(体现在Encode()和Decode()中)

PirorBoxLayer

Layer comment :

/**
* @brief Generates prior boxes for a feature map with specified parameters.
*
* @param bottom input Blob vector (length 2)
*
* -# @f$ (N,\, C ,\, H^s ,\, W^s) @f$
* the input feature map @f$ F^s@f$
*
* -# @f$ (N,\, C ,\, H_0 ,\, W_0) @f$
* the data layer @f$ x_0 @f$, used for calculate step(if not provided).
*
* @param top output Blob vector (length 1)
* -# @f$ (1,\, 2 ,\, M_f \times 4) @f$ where @f$ M_f @f$ is the number of all
* priors in the feature map. @see Reshape().
*
* By default, a box of aspect ratio 1 and min_size and a box of aspect
* ratio 1 and sqrt(min_size * max_size) are created.
*/
Reshape(){
    ...
    // Since all images in a batch has same height and width, we only need to
    // generate one set of priors which can be shared across all images.
    top_shape[0] = 1;
    // 2 channels. First channel stores the value of each prior coordinate.
    // Second channel stores the variance of each prior coordinate. # ???? 
    top_shape[1] = 2;
    top_shape[2] = feature_width * feature_height * num_priors_ * 4;
    ...
}

MultiboxLayer

注意:代码中通过计算encoded_box来得到target

/**
 * @brief Perform MultiBox operations. Including the following:
 *
 *  - decode the predictions.
 *  - perform matching between priors/predictions and ground truth.
 *  - perform negative example mining.
 *  - use matched boxes and confidences to compute loss with internal
 *  localization loss layer and confidence loss layer.
 */

/* Data preparations. */
  // Retrieve all ground truth.
  // Retrieve all prior bboxes. It is same within a batch since we assume all images in a batch are of same dimension.
  // Retrieve all predictions.
  // Find matches between source bboxes and ground truth bboxes.
/* Mining. */
	// Sample hard negative (and positive) examples based on mining type.
/* Optionally Visualize data. */
/* Compute location loss. */
/* Compute confidence loss. */
/* Compute multi-box loss */

DetOutLayer

实际的代码实现会更加复杂一下,以下只是表示其流程步骤

/**
 * @brief Generate the detection output based on location and confidence
 * predictions by doing non maximum suppression.
 *
 * Intended for use with MultiBox detection method.
 *
 * NOTE: does not implement Backwards operation.
 */

SSD DetectionOutputLayer
Input: conf, loc, prior
Output: detections
Steps:
  numOfKeptDets = 0;
  allClassBoxesBatch[batchSize]
  // For each image of the batch.
    // Decode loc predictions from priors to boxes.
    { // Post process.
      // For each class, start from 1 to ignore background class
        // For each prior.
          // Filter confPredictions by confidenceThreshold to get classBoxes(optionally share location across classes)(this step will may filter out many predictions)
        // Sort classBoxes with confidence.
        // Apply MNS to classBoxes.
        // Add classBoxes after MNS to allClassBoxes.
      // Sort allClassBoxes with confidence.
      // Keep k results with topest confidence of allClassBoxes per image.
      // Update numOfKeptDets.
    } // Post process.
    // Add allClassBoxes after MNS to allClassBoxesBatch.

  // Reshape output data to numOfKeptDets x detVectorLen.
  // Write allClassBoxesBatch to output data.

DetEvalLayer

DetEval层输出TP/FP score; Solver中计算AP

/**
 * @brief Generate the detection evaluation based on DetectionOutputLayer and
 * ground truth bounding box labels.
 *
 * Intended for use with MultiBox detection method.
 *
 * NOTE: does not implement Backwards operation.
 */
	  
	// Retrieve all detection results.
  // Retrieve all ground truth (including difficult ones).
  // Initialize top_data.
  // Insert detection evaluate status.
    // For each image.
      // Get current image detections
      // If no ground truth for current image, All detections become false_pos.
      // If have ground truth for current image.
      	// For each label type of current image detections.
          // Get detecions of current label.
            // If no ground truth for current label, all detections become false_pos.
            // If have ground truth for current label.
              // Get gt bboxes of current label.
              // Scale ground truth if needed.
              // Sort detections in descend order based on scores.
              // For each detection.
                // Compute max overlap with each ground truth bbox and record the matched gt.
                // If max overlap is greater than threshold and the matched gt is not visited, set this detection as tp, else set fp.

原理和建模

论文:原文+博客解读(https://blog.csdn.net/u010167269/article/details/52563573)

网络结构

查看netscope

AnnotatedDataLayer

output:

data

label: ( 1 ,   1 ,   N g ,   8 ) (1,\, 1,\, N_g,\, 8) (1,1,Ng,8)

N g N_g Ng is the number of gt

the 8 is for

[item_id, group_label, instance_id, xmin, ymin, xmax, ymax, diff]

note it is different from NormalizeBBox

Modeling

Training

input:

image, ground truth(gt)

output:

predicition(pred)

pred 和 gt都是一个带有类别的矩形框

model:

image->conv net feature map-> cls conv + loc conv->conf + bbox transform (+ prior box) ->conf + bbox ->post processed-> pred

bbox transform can be also called the encoded bbox, see encode/decode

prior box is the initial value of pred

loss

loss = L(pred, gt)

Inferrence

**input: **image

output: predicitions

PriorBox generation

single scale prior box generation

input: feature map , data

output: P s P^s Ps, prior of feture map with scale s.

multi-scale prior box generation
{ P ( 1 ,   2 ,   M f × 4 ) s } s ∈ S c a l e s → a x i s = 2 C o n c a t P ( 1 ,   2 ,   M a × 4 ) \{P^s_{(1,\, 2 ,\, M_f \times 4)}\}_{s\in Scales} \xrightarrow[axis=2]{Concat} P_{(1,\, 2 ,\, M_a \times 4)} {P(1,2,Mf×4)s}sScalesConcat axis=2P(1,2,Ma×4)

MultiBox prediction

input

feature map: F F F

output

localization prediction: l l l

confidence prediction: c c c

single scale localization prediction

the number of prior of each feature map cell: M c M_c Mc

number of priors in the feature map : M f = H × W × M c M_f = H \times W \times M_c Mf=H×W×Mc

number of localization prediction in the feature map: M f ’ = M f × N l c M_f’=M_f \times N_{lc} Mf=Mf×Nlc

the number of location classes: N l c N_{lc} Nlc

N l c = K + 1 N_{lc} = K + 1 Nlc=K+1 if not share location across classes, in other word, use class-specific bbox, otherwise, N l c = 1 N_{lc} = 1 Nlc=1

so, C l = M c × N l c × 4 C_l = M_c \times N_{lc} \times 4 Cl=Mc×Nlc×4
F ( N ,   C f ,   H ,   W ) → C o n v l 1 ( N ,   C l ,   H ,   W ) → o r d e r = 0 , 2 , 3 , 1 P e r m u t e l 2 ( N ,   H ,   W ,   C l ) → a x i s = 1 F l a t t e n l 3 ( N ,   H × W × C l ) = l 3 ( N ,   M f × N l c × 4 ) = l 3 ( N ,   M f ′ × 4 ) F_{(N,\,C_f,\,H,\,W)}\xrightarrow{Conv} l1_{(N,\,C_l,\,H,\,W)} \xrightarrow[order=0,2,3,1]{Permute} l2_{(N,\,H,\,W,\,C_l)} \xrightarrow[axis=1]{Flatten} l3_{(N,\,H\times W\times C_l)} = l3_{(N,\,M_f \times N_{lc} \times 4)} = l3_{(N,\,M_f' \times 4)} F(N,Cf,H,W)Conv l1(N,Cl,H,W)Permute order=0,2,3,1l2(N,H,W,Cl)Flatten axis=1l3(N,H×W×Cl)=l3(N,Mf×Nlc×4)=l3(N,Mf×4)
single scale confidence prediction

let C c = M c × ( K + 1 ) C_c = M_c \times (K+1) Cc=Mc×(K+1),
F ( N ,   C f ,   H ,   W ) → C o n v c 1 ( N ,   C c ,   H ,   W ) → o r d e r = 0 , 2 , 3 , 1 P e r m u t e c 2 ( N ,   H ,   W ,   C c ) → a x i s = 1 F l a t t e n c 3 ( N ,   H × W × C c ) = c 3 ( N ,   M f × ( K + 1 ) ) F_{(N,\,C_f,\,H,\,W)}\xrightarrow{Conv} c1_{(N,\,C_c,\,H,\,W)} \xrightarrow[order=0,2,3,1]{Permute} c2_{(N,\,H,\,W,\,C_c)} \xrightarrow[axis=1]{Flatten} c3_{(N,\,H\times W\times C_c)} = c3_{(N,\,M_f \times (K+1))} F(N,Cf,H,W)Conv c1(N,Cc,H,W)Permute order=0,2,3,1c2(N,H,W,Cc)Flatten axis=1c3(N,H×W×Cc)=c3(N,Mf×(K+1))
multi scale localization prediction

s s s is the scale indicator.

number of all priors: M a = ∑ s ∈ S c a l e s M f s = ∑ s ∈ S c a l e s ( H s × W s × M c s ) M_a = \sum_{s\in Scales} M^s_f = \sum_{s\in Scales} (H^s \times W^s \times M^s_c) Ma=sScalesMfs=sScalesHs×Ws×Mcs)

number of all localization prediction: M a ’ = M a × N l c M_a’ = M_a \times N_{lc} Ma=Ma×Nlc
{ l 3 ( N ,   H s ,   W s ,   C l s ) s } s ∈ S c a l e s → a x i s = 1 C o n c a t l ( N ,   M a × N l c × 4 ) = l ( N ,   M a ′ × 4 ) \{l3^s_{(N,\,H^s,\,W^s,\,C^s_l)}\}_{s\in Scales} \xrightarrow[axis=1]{Concat} l_{(N,\,M_a \times N_{lc} \times 4)} = l_{(N,\,M_a' \times 4)} {l3(N,Hs,Ws,Cls)s}sScalesConcat axis=1l(N,Ma×Nlc×4)=l(N,Ma×4)
multi scale confidence prediction
{ c 3 ( N ,   H s ,   W s ,   C c s ) s } s ∈ S c a l e s → a x i s = 1 C o n c a t c ( N ,   M a × ( K + 1 ) ) \{c3^s_{(N,\,H^s,\,W^s,\,C^s_c)}\}_{s\in Scales} \xrightarrow[axis=1]{Concat} c_{(N,\,M_a \times (K+1))} {c3(N,Hs,Ws,Ccs)s}sScalesConcat axis=1c(N,Ma×(K+1))

encode/decode

encode

input: ( P , G ) (P,G) (P,G) or (piror box, decoded_box)

output: l l l or encoded_box

decode

input: ( P , l ) (P, l) (P,l) or (prior box, encoded_box)

output: G G G or decoded_box

Training

Matching strategy

why and what?

与一般的机器学习问题(一般的分类和回归问题)不同的是,对于一个input(image), 有多个pred和gt,那么在计算损失时,就存在一个某个pred与某个gt匹配的过程,对匹配上的pred和gt之间才能计算损失

who?

directly method: match between pred and gt

可能会出现虽然预测的pred与gt很接近,但prior和gt相差很远,从而pred与prior所在的卷积网络特征没有多少联系,造成训练的混乱,最终预测效果很差。

adopted method: match between prior and gt

其实是在训练过程中给pred和gt之间加了一个约束,即pred的初值prior是比较接近gt的,从而更容易获得准确的pred;这样学到的模型,在推理时,会倾向于只对prior做较小的变换得到pred, 利用的(prior所在的)卷积网络的特征与pred联系更紧密,从而不易造成过大的误差

how?

reffer to paper and source code.

jaccard overlap

应该就是IoU, 这里简称为jo

Bipartite matching

参考Bipartite graph/network的定义,本质上就是一一对应的意思

Bipartite graph/network翻译过来就是:二分图。

维基百科中对二分图的介绍为:二分图是一类图(G,E),其中G是顶点的集合,E为边的集合,并且G可以分成两个不相交的集合U和V,E中的任意一条边的一个顶点属于集合U,另一顶点属于集合V。一个简单的形象表示如下图:

[外链图片转存失败(img-IaAgZsZz-1568687869591)(paper_source_code_study.assets/20140221143913937-1552273578069.png)]

所以Bipartite matching就是gt与prior一一对应匹配,gt与jo最大的prior匹配.

per-predicition matching

在bipartite matching基础上做的matching, 不过论文和源码有些出入。

论文允许prior匹配任何 j o > T jo>T jo>T的gt:

(note by me: After bipartite matching, ) we then match default boxes to any ground truth with jaccard overlap higher than an threshold(0.5). This simplifies the learning problem, allowing the network to predict high scores for multiple overlapping default boxes rather than requiring it to pick only the one with maximum overlap.

而源码中,在bipartite matching的基础上,prior(不包括bipartite已经匹配的prior)只能匹配 j o > T jo>T jo>T的gt中jo最大的gt.

result?

positive: prior matched to gt

negative: prior does not match to gt

Hard negative mining

why?

too many negative priors, significant imbalance b.t. pos and neg

how?

c m a x i c^i_{max} cmaxi is the max prediction confidence over all category of i-th prior.
c m a x i = max ⁡ k ∈ { 0 , 1 , … , K } c k i c^i_{max}=\max \limits_{k\in \{0,1,\dots ,K\}} c^i_k cmaxi=k{0,1,,K}maxcki
for all i ∈ N e g i\in Neg iNeg, sort c m a x i c^i_{max} cmaxi,

pick the top ones so that the ratio between the negatives and positives is at most 3:1.

We found that this leads to faster optimization and a more stable training.

Training objective

MultiBoxLoss input & output

localization prediction: l l l , see [MultiBox prediction](#MultiBox prediction)

confidence prediction: c c c

piror: P P P , see [PriorBox generation](#PriorBox generation)

gt: G G G, see AnnotatedDataLayer

localization is in the format of bbox trainsformation

output: multi-box loss L L L
s h a p e ( l ) = ( N ,   M a ′ × N r ) s h a p e ( c ) = ( N ,   M a × ( K + 1 ) ) s h a p e ( P ) = ( 1 ,   2 ,   M a × N r ) s h a p e ( G ) = ( 1 ,   1 ,   N g ,   N l ) \begin{aligned} &shape(l) = (N,\,M_a' \times N_r) \\ &shape(c) = (N,\,M_a \times (K+1)) \\ &shape(P) = (1,\, 2 ,\, M_a \times N_r) \\ &shape(G) = (1,\, 1,\, N_g,\, N_l) \\ \end{aligned} shape(l)=(N,Ma×Nr)shape(c)=(N,Ma×(K+1))shape(P)=(1,2,Ma×Nr)shape(G)=(1,1,Ng,Nl)
N r N_r Nr is the number of variables to be regressed of a single box.

parameter inference:
M a = c [ 1 ] / ( K + 1 ) N r = P [ 2 ] / M a M a ′ = l [ 1 ] / N r N l c = M a ′ / M a \begin{aligned} M_a &= c[1]/(K+1) \\ N_r &= P[2]/M_a \\ M_a' &= l[1]/N_r \\ N_{lc} &= M_a'/M_a \end{aligned} MaNrMaNlc=c[1]/(K+1)=P[2]/Ma=l[1]/Nr=Ma/Ma
Multi-box loss
L = 1 N P o s + N N e g L c l s ( I , c ) + α 1 N P o s L l o c ( I , t , l ) ) L = \frac{1}{N_{Pos}+N_{Neg}}L_{cls}(I,c) + \alpha \frac{1}{N_{Pos}} L_{loc}(I,t,l)) L=NPos+NNeg1Lcls(I,c)+αNPos1Lloc(I,t,l)
α \alpha α is the weight term

P o s Pos Pos 为default box中正例(匹配的)的集合, N e g Neg Neg 为default box中负例的集合

I i j ∈ { 1 , 0 } I_{ij}\in \{1,0\} Iij{1,0} 为第i个default box 与第j个ground truth box是否匹配的指示器

Localization loss:
L l o c ( I , t , l ) = ∑ i ∈ P o s N P o s ∑ j ∑ ∗ ∈ { x , y , w , h } I i j s m o o t h L 1 ( t i j ∗ − l i ∗ ) L_{loc}(I,t,l) = \sum_{i\in Pos}^{N_{Pos}} \sum_j \sum_{*\in \{x,y,w,h\}} I_{ij}smooth_{L1}(t^*_{ij}-l^*_i) Lloc(I,t,l)=iPosNPosj{x,y,w,h}IijsmoothL1(tijli)
δ = t i j ∗ − d i ∗ \delta = t^* _{ij}-d^*_i δ=tijdi 是误差,与RCNN中的bbox regression的误差的区别主要是这里的default box与ground truth box 需要进行匹配

t i j ∗ t^*_{ij} tij: localization target, l i ∗ l^*_i li: localization(bbox trainsformation) prediction.

t = t ( G , P ) t=t(G,P) t=t(G,P)
t i j x = ( G x j − P x i ) / P w i t i j y = ( G y j − P y i ) / P h i t i j w = l o g ( G w j / P w i ) t i j h = l o g ( G h j / P h i ) \begin{aligned} t^x_{ij}&=(G_x^j-P_x^i)/P_w^i \\ t^y_{ij}&=(G_y^j-P_y^i)/P_h^i \\ t^w_{ij}&=log(G_w^j/P_w^i) \\ t^h_{ij}&=log(G_h^j/P_h^i) \\ \end{aligned} tijxtijytijwtijh=(GxjPxi)/Pwi=(GyjPyi)/Phi=log(Gwj/Pwi)=log(Ghj/Phi)
l l l is input, and is in the format of bbox transformation, the definition is l = l ( G ^ , P ) l=l(\hat G,P ) l=l(G^,P), the final localization(bbox) predition can be get by G ^ = l − 1 ( l , P ) \hat G=l^{-1}(l,P) G^=l1(l,P)
l i x = ( G ^ x i − P x i ) / P w i l i y = ( G ^ y i − P y i ) / P h i l i w = l o g ( G ^ w i / P w i ) l i h = l o g ( G ^ h i / P h i ) \begin{aligned} l^x_{i}&=(\hat{G}_x^i-P_x^i)/P_w^i \\ l^y_{i}&=(\hat{G}_y^i-P_y^i)/P_h^i \\ l^w_{i}&=log(\hat{G}_w^i/P_w^i) \\ l^h_{i}&=log(\hat{G}_h^i/P_h^i) \\ \end{aligned} lixliyliwlih=(G^xiPxi)/Pwi=(G^yiPyi)/Phi=log(G^wi/Pwi)=log(G^hi/Phi)
smooth L1 loss:
s m o o t h L 1 ( x ) = { 0.5 x 2 if  ∣ x ∣ &lt; 1 , ∣ x ∣ − 0.5 otherwise . smooth_{L1}(x) = \begin{cases} 0.5x^2 &amp; \text{if } \left| x \right| &lt;1,\\ \left| x \right| - 0.5 &amp; \text{otherwise}.\\ \end{cases} smoothL1(x)={0.5x2x0.5if x<1,otherwise.
相对于L2loss,对outlier更加不敏感,更易训练。

Score function:

s k i s_k^i ski is the predicted score of category k from the i-th input(here input is default box), k ∈ { 0 , 1 , … , K } k\in\{0,1,\dots ,K\} k{0,1,,K}, k = 0 k=0 k=0 is for background.
s k i = f ( x i , w k ) s_k^i = f(x^i,w_k) ski=f(xi,wk)
Softmax function:

c k i c_k^i cki is the confidence predicttion of category k from the i-th input.
c k i = P ( Y = k ∣ X = x i ) = e s k i ∑ k ′ e s k ′ i c_k^i = P(Y=k|X=x^i) = \frac{e^{s_k^i}}{\sum_{k&#x27;}e^{s_{k&#x27;}^i}} cki=P(Y=kX=xi)=keskieski
General Softmax loss:
L i = − l n c y i i = − l n P ( Y = y i ∣ X = x i ) L^i = -lnc^i_{y^i} = -lnP(Y=y^i|X=x^i) Li=lncyii=lnP(Y=yiX=xi)
Confidence loss:

The first difference from classic softmax loss is the match of default box and gt box.

The second difference is the special class(or category) of background. If the default box does not match any gt box, it is negative and count c 0 i c^i_0 c0i into loss.
L c o n f ( I , c ) = − ∑ i ∈ P o s N P o s ∑ j I i j l n c j i − ∑ i ∈ N e g N N e g l n c 0 i = − ∑ i ∈ P o s N P o s l n c G c i i − ∑ i ∈ N e g N N e g l n c 0 i \begin{aligned} L_{conf}(I,c) &amp;= -\sum_{i\in Pos}^{N_{Pos}} \sum_j I_{ij}lnc^i_{j} - \sum_{i\in Neg}^{N_{Neg}} lnc^i_0 \\ &amp;= -\sum_{i\in Pos}^{N_{Pos}} lnc^i_{G^i_c} - \sum_{i\in Neg}^{N_{Neg}} lnc^i_0 \end{aligned} Lconf(I,c)=iPosNPosjIijlncjiiNegNNeglnc0i=iPosNPoslncGciiiNegNNeglnc0i
G c i G^i_c Gci: the category of the ground truth box which is matched to i-th default box.

N e g Neg Neg : the collection of default boxes that are not matched to gt box

其它

对小物体的检测,用data aug可优化(程序中已经做了论文上的小物体检测的相应处理),或者大尺寸图片;data aug的作用很大。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值