本文主要是介绍[深度学习从入门到女装]FCN,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
论文:Fully Convolutional Networks for Semantic Segmentation
本文简单介绍一下FCN模型,并对caffe源码进行阅读
对于convolution:
output = (input + 2 * padding - ksize) / stride + 1;
对于deconvolution:
output = (input - 1) * stride + ksize - 2 * padding;
FCN的整体网络框架是根据VGG16进行修改的,把最后的三层全连接层取消,换上两层conv层,然后最后一层改成上采样也就是deconv,具体的VGG结构请参考VGG
网络结果分为三种FCN-32s、FCN-16s、FCN-8s
FCN-32s:就是整个网络一条路下来,根据5次pool得到的conv7层的特征图(与原图相比缩小32倍),随后进行32倍的反卷积达到和输入图相同的尺寸,然后根据GT(Groud Truth)进行损失训练
FCN-16s:就是把7次pool得到的conv7层的特征图(与原图相比缩小32倍)进行2倍的反卷积得到比原图缩小16倍的特征图,然后把pool4层得到的特征图(与原图相比缩小16倍),将这两个特征图进行fuse操作,作者是使用sum也就是对应位置特征值相加进行fuse操作,随后将得到的融合特征图进行16倍的反卷积得到和输入图相同尺寸
FCN-8s:就是把7次pool得到的conv7层的特征图(与原图相比缩小32倍)进行4倍的反卷积得到比原图缩小8倍的特征图,然后把pool4层得到的特征图(与原图相比缩小16倍)进行2倍的反卷积得到比原图缩小8倍的特征图,再拿pool3层得到的特征图(与原图相比缩小8倍),把这三个特征图也进行sum fuse操作,然后再将这个融合特征图进行8倍反卷积得到与输入图相同尺寸
同样的还可以得到FCN-4s、FCN-2s。但是作者经过测试,再继续做下去完全没有必要,所以对于目前来说,FCN-8s是计算量和准确率相平衡的一种
下边来看看FCN-8s实现的网络caffe代码
fcn8s代码:
layer {
name: "input"
type: "Input"
top: "data"
input_param {
# These dimensions are purely for sake of example;
# see infer.py for how to reshape the net to the given input size.
shape { dim: 1 dim: 3 dim: 500 dim: 500 }
}
}
输入为500*500*3
layer {
name: "conv1_1"
type: "Convolution"
bottom: "data"
top: "conv1_1"
param {
lr_mult: 1
decay_mult: 1
}
param {
lr_mult: 2
decay_mult: 0
}
convolution_param {
num_output: 64
pad: 100
kernel_size: 3
stride: 1
}
}
layer {
name: "relu1_1"
type: "ReLU"
bottom: "conv1_1"
top: "conv1_1"
}
第一个卷积层conv1_1的pad为100 pad后为700*700*3
使用64个3*3*3进行卷积操作后输出为698*698*64
layer {
name: "conv1_2"
type: "Convolution"
bottom: "conv1_1"
top: "conv1_2"
param {
lr_mult: 1
decay_mult: 1
}
param {
lr_mult: 2
decay_mult: 0
}
convolution_param {
num_output: 64
pad: 1
kernel_size: 3
stride: 1
}
}
layer {
name: "relu1_2"
type: "ReLU"
bottom: "conv1_2"
top: "conv1_2"
}
第二个卷积层conv1_2的pad为1 pad后为700*700*64
使用64个3*3*64进行卷积操作后输出为698*698*64
layer {
name: "pool1"
type: "Pooling"
bottom: "conv1_2"
top: "pool1"
pooling_param {
pool: MAX
kernel_size: 2
stride: 2
}
}
使用stride为2的MAXpool进行pooling后输出为349*349*64
conv2_1:num_output: 128 pad: 1 kernel_size: 3 stride: 1
输出为349*349*128
conv2_2:num_output: 128 pad: 1 kernel_size: 3 stride: 1
输出为349*349*128
pool2:MAXpooling stride2 size2 输出为175*175*128(pool使用向上取整)
conv3_1、conv3_2、conv3_3:num_output: 256 pad: 1 kernel_size: 3 stride: 1
输出为175*175*256
pool3:MAXpooling stride2 size2 输出为88*88*256(pool使用向上取整)
conv4_1、conv4_2、conv4_3:num_output: 512 pad: 1 kernel_size: 3 stride: 1
输出为88*88*512
pool4:MAXpooling stride2 size2 输出为44*44*512(pool使用向上取整)
conv5_1、conv5_2、conv5_3:num_output: 512 pad: 1 kernel_size: 3 stride: 1
输出为44*44*512
pool5:MAXpooling stride2 size2 输出为22*22*512(pool使用向上取整)
layer {
name: "fc6"
type: "Convolution"
bottom: "pool5"
top: "fc6"
param {
lr_mult: 1
decay_mult: 1
}
param {
lr_mult: 2
decay_mult: 0
}
convolution_param {
num_output: 4096
pad: 0
kernel_size: 7
stride: 1
}
}
layer {
name: "relu6"
type: "ReLU"
bottom: "fc6"
top: "fc6"
}
(这个作者真的懒,改的VGG16的框架,连全连接层的名字都没给改成卷积层。。。)
输入为 22*22*512 输出为16*16*4096
layer {
name: "fc7"
type: "Convolution"
bottom: "fc6"
top: "fc7"
param {
lr_mult: 1
decay_mult: 1
}
param {
lr_mult: 2
decay_mult: 0
}
convolution_param {
num_output: 4096
pad: 0
kernel_size: 1
stride: 1
}
}
layer {
name: "relu7"
type: "ReLU"
bottom: "fc7"
top: "fc7"
}
输入为16*16*4096 输出为16*16*4096
layer {
name: "score_fr"
type: "Convolution"
bottom: "fc7"
top: "score_fr"
param {
lr_mult: 1
decay_mult: 1
}
param {
lr_mult: 2
decay_mult: 0
}
convolution_param {
num_output: 21
pad: 0
kernel_size: 1
}
}
score_fr:输入为16*16*4096 输出为16*16*21
layer {
name: "upscore2"
type: "Deconvolution"
bottom: "score_fr"
top: "upscore2"
param {
lr_mult: 0
}
convolution_param {
num_output: 21
bias_term: false
kernel_size: 4
stride: 2
}
}
upscore2:输入为16*16*21 输入为34*34*21
layer {
name: "score_pool4"
type: "Convolution"
bottom: "pool4"
top: "score_pool4"
param {
lr_mult: 1
decay_mult: 1
}
param {
lr_mult: 2
decay_mult: 0
}
convolution_param {
num_output: 21
pad: 0
kernel_size: 1
}
}
score_pool4:输入为44*44*512 输出为44*44*21
layer {
name: "score_pool4c"
type: "Crop"
bottom: "score_pool4"
bottom: "upscore2"
top: "score_pool4c"
crop_param {
axis: 2
offset: 5
}
}
score_pool4c:这一层为对socre_pool4进行裁剪 caffe中crop作用详见Caffe中crop_layer层的理解和使用
输入为44*44*21 输出为34*34*21
layer {
name: "fuse_pool4"
type: "Eltwise"
bottom: "upscore2"
bottom: "score_pool4c"
top: "fuse_pool4"
eltwise_param {
operation: SUM
}
}
fuse_pool4:这一层是为了将upscore2与score_pool4c进行合并,进行不同层次特征融合 输入为34*34*21
layer {
name: "upscore_pool4"
type: "Deconvolution"
bottom: "fuse_pool4"
top: "upscore_pool4"
param {
lr_mult: 0
}
convolution_param {
num_output: 21
bias_term: false
kernel_size: 4
stride: 2
}
}
upscore_pool4:输入为34*34*21 输出为70*70*21
layer {
name: "score_pool3"
type: "Convolution"
bottom: "pool3"
top: "score_pool3"
param {
lr_mult: 1
decay_mult: 1
}
param {
lr_mult: 2
decay_mult: 0
}
convolution_param {
num_output: 21
pad: 0
kernel_size: 1
}
}
score_pool3:输入为88*88*256 输出为88*88*21
layer {
name: "score_pool3c"
type: "Crop"
bottom: "score_pool3"
bottom: "upscore_pool4"
top: "score_pool3c"
crop_param {
axis: 2
offset: 9
}
}
score_pool3c:这层将score_pool3进行裁剪为和upscore_pool4相同尺寸
输入为88*88*21 输出为70*70*21
layer {
name: "fuse_pool3"
type: "Eltwise"
bottom: "upscore_pool4"
bottom: "score_pool3c"
top: "fuse_pool3"
eltwise_param {
operation: SUM
}
}
fuse_pool3:将upscore_pool4和score_pool3c特征图融合相加 输出为70*70*21
layer {
name: "upscore8"
type: "Deconvolution"
bottom: "fuse_pool3"
top: "upscore8"
param {
lr_mult: 0
}
convolution_param {
num_output: 21
bias_term: false
kernel_size: 16
stride: 8
}
}
upscore8:输入为70*70*21 输出为568*568*21
layer {
name: "score"
type: "Crop"
bottom: "upscore8"
bottom: "data"
top: "score"
crop_param {
axis: 2
offset: 31
}
}
score:对最终分割图进行裁剪 输出为506*506*21
通过源码我们可以看到,先实现过程中,并不是像上边图片上画的输入和输出尺寸相同,存在一定偏差,这也是因为,作者在第一次卷积的时候进行一次100的padding,是为了防止图像过小,缩小32次后,特征无法反向传播回去,然后最后根据多次cropping,将不同尺寸的特征图调整完相同的尺寸,最终得到506*506的输入图
这篇关于[深度学习从入门到女装]FCN的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!