SSD Network Architecture--keras version

这里的网络架构和论文中插图中的网络架构是相一致的。对了,忘了说了,这里使用的keras版本是1.2.2,等源码读完之后,我自己改一个2.0.6版本上传到github上面。可别直接粘贴复制,里面有些中文的解释,不一定可行的。#defint input shapeinput_shape = (300,300,3)#defint the number of classes
num_classes = 21
#Here the network is wrapped in to a dictory because it more easy to make some operations.
net = {}
# Block 1
input_tensor = Input(shape=input_shape)#defint the image hight and wight
img_size = (input_shape[1], input_shape[0])
net[‘input‘] = input_tensor
net[‘conv1_1‘] = Convolution2D(64, 3, 3,
                               activation=‘relu‘,
                               border_mode=‘same‘,
                               name=‘conv1_1‘)(net[‘input‘])
net[‘conv1_2‘] = Convolution2D(64, 3, 3,
                               activation=‘relu‘,
                               border_mode=‘same‘,
                               name=‘conv1_2‘)(net[‘conv1_1‘])
net[‘pool1‘] = MaxPooling2D((2, 2), strides=(2, 2), border_mode=‘same‘,
                            name=‘pool1‘)(net[‘conv1_2‘])
# Block 2
net[‘conv2_1‘] = Convolution2D(128, 3, 3,
                               activation=‘relu‘,
                               border_mode=‘same‘,
                               name=‘conv2_1‘)(net[‘pool1‘])
net[‘conv2_2‘] = Convolution2D(128, 3, 3,
                               activation=‘relu‘,
                               border_mode=‘same‘,
                               name=‘conv2_2‘)(net[‘conv2_1‘])
net[‘pool2‘] = MaxPooling2D((2, 2), strides=(2, 2), border_mode=‘same‘,
                            name=‘pool2‘)(net[‘conv2_2‘])
# Block 3
net[‘conv3_1‘] = Convolution2D(256, 3, 3,
                               activation=‘relu‘,
                               border_mode=‘same‘,
                               name=‘conv3_1‘)(net[‘pool2‘])
net[‘conv3_2‘] = Convolution2D(256, 3, 3,
                               activation=‘relu‘,
                               border_mode=‘same‘,
                               name=‘conv3_2‘)(net[‘conv3_1‘])
net[‘conv3_3‘] = Convolution2D(256, 3, 3,
                               activation=‘relu‘,
                               border_mode=‘same‘,
                               name=‘conv3_3‘)(net[‘conv3_2‘])
net[‘pool3‘] = MaxPooling2D((2, 2), strides=(2, 2), border_mode=‘same‘,
                            name=‘pool3‘)(net[‘conv3_3‘])
# Block 4
net[‘conv4_1‘] = Convolution2D(512, 3, 3,
                               activation=‘relu‘,
                               border_mode=‘same‘,
                               name=‘conv4_1‘)(net[‘pool3‘])
net[‘conv4_2‘] = Convolution2D(512, 3, 3,
                               activation=‘relu‘,
                               border_mode=‘same‘,
                               name=‘conv4_2‘)(net[‘conv4_1‘])#the first layer be operated
net[‘conv4_3‘] = Convolution2D(512, 3, 3,
                               activation=‘relu‘,
                               border_mode=‘same‘,
                               name=‘conv4_3‘)(net[‘conv4_2‘])
net[‘pool4‘] = MaxPooling2D((2, 2), strides=(2, 2), border_mode=‘same‘,
                            name=‘pool4‘)(net[‘conv4_3‘])
# Block 5
net[‘conv5_1‘] = Convolution2D(512, 3, 3,
                               activation=‘relu‘,
                               border_mode=‘same‘,
                               name=‘conv5_1‘)(net[‘pool4‘])
net[‘conv5_2‘] = Convolution2D(512, 3, 3,
                               activation=‘relu‘,
                               border_mode=‘same‘,
                               name=‘conv5_2‘)(net[‘conv5_1‘])
net[‘conv5_3‘] = Convolution2D(512, 3, 3,
                               activation=‘relu‘,
                               border_mode=‘same‘,
                               name=‘conv5_3‘)(net[‘conv5_2‘])
net[‘pool5‘] = MaxPooling2D((3, 3), strides=(1, 1), border_mode=‘same‘,
                            name=‘pool5‘)(net[‘conv5_3‘])#here is the FC6 in the orginal VGG16 Network,There move to Atrous Convolution for the reason i don‘t know.
# FC6
net[‘fc6‘] = AtrousConvolution2D(1024, 3, 3, atrous_rate=(6, 6),
                                 activation=‘relu‘, border_mode=‘same‘,
                                 name=‘fc6‘)(net[‘pool5‘])#the second layer to be operated
# FC7
net[‘fc7‘] = Convolution2D(1024, 1, 1, activation=‘relu‘,
                           border_mode=‘same‘, name=‘fc7‘)(net[‘fc6‘])
# x = Dropout(0.5, name=‘drop7‘)(x)
# Block 6
net[‘conv6_1‘] = Convolution2D(256, 1, 1, activation=‘relu‘,
                               border_mode=‘same‘,
                               name=‘conv6_1‘)(net[‘fc7‘])#the third layer to be opreated
net[‘conv6_2‘] = Convolution2D(512, 3, 3, subsample=(2, 2),
                               activation=‘relu‘, border_mode=‘same‘,
                               name=‘conv6_2‘)(net[‘conv6_1‘])
# Block 7
net[‘conv7_1‘] = Convolution2D(128, 1, 1, activation=‘relu‘,
                               border_mode=‘same‘,
                               name=‘conv7_1‘)(net[‘conv6_2‘])
net[‘conv7_2‘] = ZeroPadding2D()(net[‘conv7_1‘])#the forth layer to be operated
net[‘conv7_2‘] = Convolution2D(256, 3, 3, subsample=(2, 2),
                               activation=‘relu‘, border_mode=‘valid‘,
                               name=‘conv7_2‘)(net[‘conv7_2‘])
# Block 8
net[‘conv8_1‘] = Convolution2D(128, 1, 1, activation=‘relu‘,
                               border_mode=‘same‘,
                               name=‘conv8_1‘)(net[‘conv7_2‘])#the fifth layer to be operated
net[‘conv8_2‘] = Convolution2D(256, 3, 3, subsample=(2, 2),
                               activation=‘relu‘, border_mode=‘same‘,
                               name=‘conv8_2‘)(net[‘conv8_1‘])# the last layer to be operated
# Last Pool
net[‘pool6‘] = GlobalAveragePooling2D(name=‘pool6‘)(net[‘conv8_2‘])

# Prediction from conv4_3# net[‘conv4_3‘]._shape = (?, 38, 38, 512)# 算了还是说中文吧,这个层是用来对输入数据进行正则化的层,有参数需要学习,输出的数据形式和输入输入形式是一致的。
net[‘conv4_3_norm‘] = Normalize(20, name=‘conv4_3_norm‘)(net[‘conv4_3‘])
num_priors = 3
#here is *4 because the box need 4 number to define,here is only predice the box coordinate
x = Convolution2D(num_priors * 4, 3, 3, border_mode=‘same‘,
                  name=‘conv4_3_norm_mbox_loc‘)(net[‘conv4_3_norm‘])
net[‘conv4_3_norm_mbox_loc‘] = x
flatten = Flatten(name=‘conv4_3_norm_mbox_loc_flat‘)
net[‘conv4_3_norm_mbox_loc_flat‘] = flatten(net[‘conv4_3_norm_mbox_loc‘])#the box coordinate is finished now it will perdice the classes
name = ‘conv4_3_norm_mbox_conf‘
if num_classes != 21:
    name += ‘_{}‘.format(num_classes)# here is start predict the classes
x = Convolution2D(num_priors * num_classes, 3, 3, border_mode=‘same‘,
                  name=name)(net[‘conv4_3_norm‘])
net[‘conv4_3_norm_mbox_conf‘] = x
flatten = Flatten(name=‘conv4_3_norm_mbox_conf_flat‘)
net[‘conv4_3_norm_mbox_conf_flat‘] = flatten(net[‘conv4_3_norm_mbox_conf‘])#这里是用来对conv4_3层的feature map生成论文中所说的default box,对没错,就是直接使用Feature map来进行default box的生成#当然这里要指定一些参数,这些参数是需要好好斟酌的。
priorbox = PriorBox(img_size, 30.0, aspect_ratios=[2],
                    variances=[0.1, 0.1, 0.2, 0.2],
                    name=‘conv4_3_norm_mbox_priorbox‘)
net[‘conv4_3_norm_mbox_priorbox‘] = priorbox(net[‘conv4_3_norm‘])
#好了,到这里第一个层的操作就完成了,下面其他层的操作都是相类似的啦。
# Prediction from fc7
num_priors = 6
net[‘fc7_mbox_loc‘] = Convolution2D(num_priors * 4, 3, 3,
                                    border_mode=‘same‘,
                                    name=‘fc7_mbox_loc‘)(net[‘fc7‘])
flatten = Flatten(name=‘fc7_mbox_loc_flat‘)
net[‘fc7_mbox_loc_flat‘] = flatten(net[‘fc7_mbox_loc‘])
name = ‘fc7_mbox_conf‘
if num_classes != 21:
    name += ‘_{}‘.format(num_classes)
net[‘fc7_mbox_conf‘] = Convolution2D(num_priors * num_classes, 3, 3,
                                     border_mode=‘same‘,
                                     name=name)(net[‘fc7‘])
flatten = Flatten(name=‘fc7_mbox_conf_flat‘)
net[‘fc7_mbox_conf_flat‘] = flatten(net[‘fc7_mbox_conf‘])
priorbox = PriorBox(img_size, 60.0, max_size=114.0, aspect_ratios=[2, 3],
                    variances=[0.1, 0.1, 0.2, 0.2],
                    name=‘fc7_mbox_priorbox‘)
net[‘fc7_mbox_priorbox‘] = priorbox(net[‘fc7‘])
# Prediction from conv6_2
num_priors = 6
x = Convolution2D(num_priors * 4, 3, 3, border_mode=‘same‘,
                  name=‘conv6_2_mbox_loc‘)(net[‘conv6_2‘])
net[‘conv6_2_mbox_loc‘] = x
flatten = Flatten(name=‘conv6_2_mbox_loc_flat‘)
net[‘conv6_2_mbox_loc_flat‘] = flatten(net[‘conv6_2_mbox_loc‘])
name = ‘conv6_2_mbox_conf‘
if num_classes != 21:
    name += ‘_{}‘.format(num_classes)
x = Convolution2D(num_priors * num_classes, 3, 3, border_mode=‘same‘,
                  name=name)(net[‘conv6_2‘])
net[‘conv6_2_mbox_conf‘] = x
flatten = Flatten(name=‘conv6_2_mbox_conf_flat‘)
net[‘conv6_2_mbox_conf_flat‘] = flatten(net[‘conv6_2_mbox_conf‘])
priorbox = PriorBox(img_size, 114.0, max_size=168.0, aspect_ratios=[2, 3],
                    variances=[0.1, 0.1, 0.2, 0.2],
                    name=‘conv6_2_mbox_priorbox‘)
net[‘conv6_2_mbox_priorbox‘] = priorbox(net[‘conv6_2‘])
# Prediction from conv7_2
num_priors = 6
x = Convolution2D(num_priors * 4, 3, 3, border_mode=‘same‘,
                  name=‘conv7_2_mbox_loc‘)(net[‘conv7_2‘])
net[‘conv7_2_mbox_loc‘] = x
flatten = Flatten(name=‘conv7_2_mbox_loc_flat‘)
net[‘conv7_2_mbox_loc_flat‘] = flatten(net[‘conv7_2_mbox_loc‘])
name = ‘conv7_2_mbox_conf‘
if num_classes != 21:
    name += ‘_{}‘.format(num_classes)
x = Convolution2D(num_priors * num_classes, 3, 3, border_mode=‘same‘,
                  name=name)(net[‘conv7_2‘])
net[‘conv7_2_mbox_conf‘] = x
flatten = Flatten(name=‘conv7_2_mbox_conf_flat‘)
net[‘conv7_2_mbox_conf_flat‘] = flatten(net[‘conv7_2_mbox_conf‘])
priorbox = PriorBox(img_size, 168.0, max_size=222.0, aspect_ratios=[2, 3],
                    variances=[0.1, 0.1, 0.2, 0.2],
                    name=‘conv7_2_mbox_priorbox‘)
net[‘conv7_2_mbox_priorbox‘] = priorbox(net[‘conv7_2‘])
# Prediction from conv8_2
num_priors = 6
x = Convolution2D(num_priors * 4, 3, 3, border_mode=‘same‘,
                  name=‘conv8_2_mbox_loc‘)(net[‘conv8_2‘])
net[‘conv8_2_mbox_loc‘] = x
flatten = Flatten(name=‘conv8_2_mbox_loc_flat‘)
net[‘conv8_2_mbox_loc_flat‘] = flatten(net[‘conv8_2_mbox_loc‘])
name = ‘conv8_2_mbox_conf‘
if num_classes != 21:
    name += ‘_{}‘.format(num_classes)
x = Convolution2D(num_priors * num_classes, 3, 3, border_mode=‘same‘,
                  name=name)(net[‘conv8_2‘])
net[‘conv8_2_mbox_conf‘] = x
flatten = Flatten(name=‘conv8_2_mbox_conf_flat‘)
net[‘conv8_2_mbox_conf_flat‘] = flatten(net[‘conv8_2_mbox_conf‘])
priorbox = PriorBox(img_size, 222.0, max_size=276.0, aspect_ratios=[2, 3],
                    variances=[0.1, 0.1, 0.2, 0.2],
                    name=‘conv8_2_mbox_priorbox‘)
net[‘conv8_2_mbox_priorbox‘] = priorbox(net[‘conv8_2‘])
# Prediction from pool6
num_priors = 6
x = Dense(num_priors * 4, name=‘pool6_mbox_loc_flat‘)(net[‘pool6‘])
net[‘pool6_mbox_loc_flat‘] = x
name = ‘pool6_mbox_conf_flat‘
if num_classes != 21:
    name += ‘_{}‘.format(num_classes)
x = Dense(num_priors * num_classes, name=name)(net[‘pool6‘])
net[‘pool6_mbox_conf_flat‘] = x
priorbox = PriorBox(img_size, 276.0, max_size=330.0, aspect_ratios=[2, 3],
                    variances=[0.1, 0.1, 0.2, 0.2],
                    name=‘pool6_mbox_priorbox‘)#由于这里的维数不对,因此要修改Feature map层对应的维数信息
if K.image_dim_ordering() == ‘tf‘:
    target_shape = (1, 1, 256)
else:
    target_shape = (256, 1, 1)
net[‘pool6_reshaped‘] = Reshape(target_shape,
                                name=‘pool6_reshaped‘)(net[‘pool6‘])
net[‘pool6_mbox_priorbox‘] = priorbox(net[‘pool6_reshaped‘])#好啦,到这里位置,所有的信息都已经生成了,下一步就是根据这些信息来进行训练或者是预测了。
# Gather all predictions
net[‘mbox_loc‘] = merge([net[‘conv4_3_norm_mbox_loc_flat‘],
                         net[‘fc7_mbox_loc_flat‘],
                         net[‘conv6_2_mbox_loc_flat‘],
                         net[‘conv7_2_mbox_loc_flat‘],
                         net[‘conv8_2_mbox_loc_flat‘],
                         net[‘pool6_mbox_loc_flat‘]],
                        mode=‘concat‘, concat_axis=1, name=‘mbox_loc‘)
net[‘mbox_conf‘] = merge([net[‘conv4_3_norm_mbox_conf_flat‘],
                          net[‘fc7_mbox_conf_flat‘],
                          net[‘conv6_2_mbox_conf_flat‘],
                          net[‘conv7_2_mbox_conf_flat‘],
                          net[‘conv8_2_mbox_conf_flat‘],
                          net[‘pool6_mbox_conf_flat‘]],
                         mode=‘concat‘, concat_axis=1, name=‘mbox_conf‘)
net[‘mbox_priorbox‘] = merge([net[‘conv4_3_norm_mbox_priorbox‘],
                              net[‘fc7_mbox_priorbox‘],
                              net[‘conv6_2_mbox_priorbox‘],
                              net[‘conv7_2_mbox_priorbox‘],
                              net[‘conv8_2_mbox_priorbox‘],
                              net[‘pool6_mbox_priorbox‘]],
                             mode=‘concat‘, concat_axis=1,
                             name=‘mbox_priorbox‘)
if hasattr(net[‘mbox_loc‘], ‘_keras_shape‘):
    num_boxes = net[‘mbox_loc‘]._keras_shape[-1] // 4
elif hasattr(net[‘mbox_loc‘], ‘int_shape‘):
    num_boxes = K.int_shape(net[‘mbox_loc‘])[-1] // 4
net[‘mbox_loc‘] = Reshape((num_boxes, 4),
                          name=‘mbox_loc_final‘)(net[‘mbox_loc‘])
net[‘mbox_conf‘] = Reshape((num_boxes, num_classes),
                           name=‘mbox_conf_logits‘)(net[‘mbox_conf‘])
net[‘mbox_conf‘] = Activation(‘softmax‘,
                              name=‘mbox_conf_final‘)(net[‘mbox_conf‘])
net[‘predictions‘] = merge([net[‘mbox_loc‘],
                           net[‘mbox_conf‘],
                           net[‘mbox_priorbox‘]],
                           mode=‘concat‘, concat_axis=2,
                           name=‘predictions‘)
model = Model(net[‘input‘], net[‘predictions‘])
时间: 2024-10-10 02:09:35

SSD Network Architecture--keras version的相关文章

SSD Network Architecture Special Lyaers--keras version

"""Some special pupropse layers for SSD.""" import keras.backend as K from keras.engine.topology import InputSpec from keras.engine.topology import Layer import numpy as np import tensorflow as tf class Normalize(Layer): &quo

【Network Architecture】Densely Connected Convolutional Networks 论文解析

0. Paper link 1. Overview ??文章开篇提到了如果在靠近输入与输出的层之间存在短连接(shorter connections),可以训练更深.更准确.更有效的卷积网络,DenseNet利用了这个性质,每层都与之前所有的层进行连接,即之前所有层的feature map都作为这一层的输入.DenseNet有减少梯度消失,增强特征传递,鼓励特征重利用同时极大的减少了参数的数量.在很多任务上达到了state-of-the-art. ??另外DenseNet并不是像ResNet那样

【Network Architecture】Feature Pyramid Networks for Object Detection(FPN)论文解析(转)

目录 0. 前言 1. 博客一 2.. 博客二 0. 前言 ??这篇论文提出了一种新的特征融合方式来解决多尺度问题, 感觉挺有创新性的, 如果需要与其他网络进行拼接,还是需要再回到原文看一下细节.这里转了两篇比较好的博客作为备忘. 1. 博客一 这篇论文是CVPR2017年的文章,采用特征金字塔做目标检测,有许多亮点,特来分享. 论文:feature pyramid networks for object detection 论文链接:https://arxiv.org/abs/1612.031

【Network architecture】Rethinking the Inception Architecture for Computer Vision(inception-v3)论文解析

0. paper link inception-v3 1. Overview ??这篇文章很多"经验"性的东西,因此会写的比较细,把文章里的一些话摘取出来,多学习一下,希望对以后自己设计网络有帮助. 2. Four General Design Principles ??这里文章介绍了四种设计网络设计原则,这是作者利用各种卷积网络结构,通过大量的实验推测的. 避免特征表示瓶颈,尤其是在网络的前面.要避免严重压缩导致(pooling,卷积等操作)的瓶颈.特征表示尺寸应该温和的减少,从输入

【Network Architecture】SegNet论文解析(转)

文章来源: https://blog.csdn.net/fate_fjh/article/details/53467948 Introduction 自己制作国内高速公路label,使用SegNet训练高速公路模型,测试效果 参考:http://mi.eng.cam.ac.uk/projects/segnet/tutorial.html SegNet是Cambridge提出旨在解决自动驾驶或者智能机器人的图像语义分割深度网络,开放源码,基于caffe框架.SegNet基于FCN,修改VGG-16

(转) Learning Deep Learning with Keras

Learning Deep Learning with Keras Piotr Migda? - blog Projects Articles Publications Resume About Photos Learning Deep Learning with Keras 30 Apr 2017 ? Piotr Migda? ? [machine-learning] [deep-learning] [overview] I teach deep learning both for a liv

Keras vs. PyTorch in Transfer Learning

We perform image classification, one of the computer vision tasks deep learning shines at. As training from scratch is unfeasible in most cases (as it is very data hungry), we perform transfer learning using ResNet-50 pre-trained on ImageNet. We get

Is VXLAN the Answer to the Network Virtualization Question?

Network virtualization is a growing topic of interest and for some good reasons as networks scale to meet the challenges of cloud computing they are running up against VLAN scaling limitations. There have been several network overlay technologies rel

Keras版Faster-RCNN代码学习(IOU,RPN)1

最近开始使用Keras来做深度学习,发现模型搭建相较于MXnet, Caffe等确实比较方便,适合于新手练手,于是找来了目标检测经典的模型Faster-RCNN的keras代码来练练手,代码的主题部分转自知乎专栏Learning Machine,作者张潇捷,链接如下: keras版faster-rcnn算法详解(1.RPN计算) keras版faster-rcnn算法详解 (2.roi计算及其他) 我再对代码中loss的计算,config的设置等细节进行学习 Keras版Faster-RCNN代