美文网首页
ssd object detection

ssd object detection

作者: alex_f532 | 来源:发表于2020-02-14 20:00 被阅读0次

SSD的英文全名是Single Shot MultiBox Detector,Single shot说明SSD算法属于one-stage方法,MultiBox说明SSD算法基于多框预测。

SSD是一种非常优秀的one-stage目标检测方法,one-stage算法就是目标检测和分类是同时完成的,其主要思路是利用CNN提取特征后,物体分类与框的回归同时进行,整个过程只需要一步,所以其优势是速度快。但是一个缺点是训练比较困难,这主要是因为正样本与负样本(背景)极其不均衡(一般训练的时候取负样本与正样本的比例维3:1),导致模型准确度稍低。

1.主干网络

SSD选用的主干网络是VGG,并且对VGG进行一定的修改,修改的地方为:

1.将fc6,fc7的全连接层改成全卷积层。

2.去点fc8层和dropout层。

3.增加conv6,conv7,conv8以及conv9.

本次实现一个5个类别的分类,所以简化了主干网络,此模型包含 7 个卷积层, 其中 4 个预测层, 预测层从第 4, 5, 6, 和 7 层做预测,keras代码如下:

conv1=Conv2D(32,(5,5),strides=(1,1),padding='same',kernel_initializer='he_normal',kernel_regularizer=l2(l2_reg),name='conv1')(x1)

    conv1=BatchNormalization(axis=3,momentum=0.99,name='bn1')(conv1)

    conv1=ELU(name='elu1')(conv1)

    pool1=MaxPooling2D(pool_size=(2,2),name='pool1')(conv1)

    conv2=Conv2D(48,(3,3),strides=(1,1),padding='same',kernel_initializer='he_normal',kernel_regularizer=l2(l2_reg),name='conv2')(pool1)

    conv2=BatchNormalization(axis=3,momentum=0.99,name='bn2')(conv2)

    conv2=ELU(name='elu2')(conv2)

    pool2=MaxPooling2D(pool_size=(2,2),name='pool2')(conv2)

    conv3=Conv2D(64,(3,3),strides=(1,1),padding='same',kernel_initializer='he_normal',kernel_regularizer=l2(l2_reg),name='conv3')(pool2)

    conv3=BatchNormalization(axis=3,momentum=0.99,name='bn3')(conv3)

    conv3=ELU(name='elu3')(conv3)

    pool3=MaxPooling2D(pool_size=(2,2),name='pool3')(conv3)

    conv4=Conv2D(64,(3,3),strides=(1,1),padding='same',kernel_initializer='he_normal',kernel_regularizer=l2(l2_reg),name='conv4')(pool3)

    conv4=BatchNormalization(axis=3,momentum=0.99,name='bn4')(conv4)

    conv4=ELU(name='elu4')(conv4)

    pool4=MaxPooling2D(pool_size=(2,2),name='pool4')(conv4)

    conv5=Conv2D(48,(3,3),strides=(1,1),padding='same',kernel_initializer='he_normal',kernel_regularizer=l2(l2_reg),name='conv5')(pool4)

    conv5=BatchNormalization(axis=3,momentum=0.99,name='bn5')(conv5)

    conv5=ELU(name='elu5')(conv5)

    pool5=MaxPooling2D(pool_size=(2,2),name='pool5')(conv5)

    conv6=Conv2D(48,(3,3),strides=(1,1),padding='same',kernel_initializer='he_normal',kernel_regularizer=l2(l2_reg),name='conv6')(pool5)

    conv6=BatchNormalization(axis=3,momentum=0.99,name='bn6')(conv4)

    conv6=ELU(name='elu6')(conv6)

    pool6=MaxPooling2D(pool_size=(2,2),name='pool6')(conv6)

    conv7=Conv2D(32,(3,3),strides=(1,1),padding='same',kernel_initializer='he_normal',kernel_regularizer=l2(l2_reg),name='conv7')(pool6)

    conv7=BatchNormalization(axis=3,momentum=0.99,name='bn7')(conv7)

    conv7=ELU(name='elu7')(conv7)

#输出classes形状(batch,height,width,n_boxes*n_classes)

    classes4=Conv2D(n_boxes[0]*n_classes,(3,3),strides=(1,1),padding='same',kernel_initializer='he_normal',kernel_regularizer=l2(l2_reg),name='classes4')(conv4)

    classes5=Conv2D(n_boxes[1]*n_classes,(3,3),strides=(1,1),padding='same',kernel_initializer='he_normal',kernel_regularizer=l2(l2_reg),name='classes5')(conv5)

    classes6=Conv2D(n_boxes[2]*n_classes,(3,3),strides=(1,1),padding='same',kernel_initializer='he_normal',kernel_regularizer=l2(l2_reg),name='classes6')(conv6)

    classes7=Conv2D(n_boxes[3]*n_classes,(3,3),strides=(1,1),padding='same',kernel_initializer='he_normal',kernel_regularizer=l2(l2_reg),name='classes7')(conv7)

    #输出box形状(batch,height,width,n_boxes*4)

    boxes4=Conv2D(n_boxes[0]*4,(3,3),strides=(1,1),padding='same',kernel_initializer='he_normal',kernel_regularizer=l2(l2_reg))(conv4)

    boxes5=Conv2D(n_boxes[1]*4,(3,3),strides=(1,1),padding='same',kernel_initializer='he_normal',kernel_regularizer=l2(l2_reg))(conv5)

    boxes6=Conv2D(n_boxes[2]*4,(3,3),strides=(1,1),padding='same',kernel_initializer='he_normal',kernel_regularizer=l2(l2_reg))(conv6)

    boxes7=Conv2D(n_boxes[3]*4,(3,3),strides=(1,1),padding='same',kernel_initializer='he_normal',kernel_regularizer=l2(l2_reg))(conv7)

    #产生anchor box

    #输出anchor的形状 (batch,height,width,n_boxes,8)

    anchors4=AnchorBoxes(img_height,img_width,this_scale=scales[0],next_scale=scales[1],aspect_ratios=aspect_ratios[0],

                        two_boxes_for_ar1=two_boxes_for_ar1,this_steps=steps[0],this_offsets=offsets[0],

                        clip_boxes=clip_boxes,variances=variances,coords=coords,normalize_coords=normalize_coords,name='anchors4')(boxes4)

    anchors5=AnchorBoxes(img_height,img_width,this_scale=scales[1],next_scale=scales[2],aspect_ratios=aspect_ratios[1],

                        two_boxes_for_ar1=two_boxes_for_ar1,this_steps=steps[1],this_offsets=offsets[1],

                        clip_boxes=clip_boxes,variances=variances,coords=coords,normalize_coords=normalize_coords,name='anchors5')(boxes5)

    anchors6=AnchorBoxes(img_height,img_width,this_scale=scales[2],next_scale=scales[3],aspect_ratios=aspect_ratios[2],

                        two_boxes_for_ar1=two_boxes_for_ar1,this_steps=steps[2],this_offsets=offsets[2],

                        clip_boxes=clip_boxes,variances=variances,coords=coords,normalize_coords=normalize_coords,name='anchors6')(boxes6)

    anchors7=AnchorBoxes(img_height,img_width,this_scale=scales[3],next_scale=scales[4],aspect_ratios=aspect_ratios[3],

                        two_boxes_for_ar1=two_boxes_for_ar1,this_steps=steps[3],this_offsets=offsets[3],

                        clip_boxes=clip_boxes,variances=variances,coords=coords,normalize_coords=normalize_coords,name='anchors7')(boxes7)

    classes4_reshaped=Reshape((-1,n_classes),name='classes4_reshape')(classes4)

    classes5_reshaped=Reshape((-1,n_classes),name='classes5_reshape')(classes5)

    classes6_reshaped=Reshape((-1,n_classes),name='classes6_reshape')(classes6)

    classes7_reshaped=Reshape((-1,n_classes),name='classes7_reshape')(classes7)

    boxes4_reshaped=Reshape((-1,4),name='boxes4_reshape')(boxes4)

    boxes5_reshaped=Reshape((-1,4),name='boxes5_reshape')(boxes5)

    boxes6_reshaped=Reshape((-1,4),name='boxes6_reshape')(boxes6)

    boxes7_reshaped=Reshape((-1,4),name='boxes7_reshape')(boxes7)

    anchors4_reshaped=Reshape((-1,8),name='anchors4_reshape')(anchors4)

    anchors5_reshaped=Reshape((-1,8),name='anchors5_reshape')(anchors5)

    anchors6_reshaped=Reshape((-1,8),name='anchors6_reshape')(anchors6)

    anchors7_reshaped=Reshape((-1,8),name='anchors7_reshape')(anchors7)

    #classes_concat 形状 (batch,n_boxes_total,n_classes)

    classes_concat=Concatenate(axis=1,name='classes_concat')([classes4_reshaped,

                                                              classes5_reshaped,

                                                              classes6_reshaped,

                                                              classes7_reshaped])

    #boxes_concat 形状 (batch,n_boxes_total,4)

    boxes_concat=Concatenate(axis=1,name='boxes_concat')([boxes4_reshaped,

                                                          boxes5_reshaped,

                                                          boxes6_reshaped,

                                                          boxes7_reshaped])

    #anchors_concat 形状 (batch,n_boxes_total,8)

    anchors_concat=Concatenate(axis=1,name='anchors_concat')([anchors4_reshaped,

                                                              anchors5_reshaped,

                                                              anchors6_reshaped,

                                                              anchors7_reshaped])

    classes_softmax=Activation('softmax',name='classes_softmax')(classes_concat)

    predictions=Concatenate(axis=2,name='predictions')([classes_softmax,boxes_concat,anchors_concat])

    model = Model(inputs=x, outputs=decoded_predictions)

相关文章

网友评论

      本文标题:ssd object detection

      本文链接:https://www.haomeiwen.com/subject/ckijfhtx.html