Crop Layer tutorial
- Layer type:
Crop
- Doxygen Documentation
- Header:
./include/caffe/layers/crop_layer.hpp
- CPU implementation:
./src/caffe/layers/crop_layer.cpp
- CUDA GPU implementation:
./src/caffe/layers/crop_layer.cu
Parameters
- Parameters (
CropParameter crop_param
) - From
./src/caffe/proto/caffe.proto
):
Crop层介绍
在全卷积网络中,一般会在原始图像上加一个很大的 pad,比如 voc-fcn32s 的第一个卷积层设置 pad = 100:
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
}
}
关于为什么第一层卷积要加这么大一个 pad,参考文章:FCN学习:Semantic Segmentation
所以加入 pad 后,就导致图像比原始图像要大一些,fcn 为了得到一张与原图大小相同的分割结果,最后就需要用 crop 层将多余的部分裁剪掉。
Crop举例
在 voc-fcn32s 的 crop 层定义如下:
layer {
name: "score"
type: "Crop"
bottom: "upscore"
bottom: "data"
top: "score"
crop_param {
axis: 2
offset: 19
}
}
- 2 个输入(bottom):upscore 和 data
- 2 个参数(crop_param):axis 和 offset
- 1 个输出(top):score
Crop 层的目的是将特征图裁剪到与输入图像大小相同,所以一手拿着得到的特征图,一手拿着输入图像,我们对比着输入图像的尺寸,就可以对任意大小的特征图进行裁剪操作了。
- upscore:需要进行裁剪的特征图
- data:裁剪的参考输入,即输入的原始图像
裁剪的输出 top,就是在 upscore 的基础上,按照 data 的 size 裁剪而来,所以 top 的尺寸和 data 的尺寸是一样的。
裁剪操作还需要 2 个重要的参数:
- axis:决定从哪个轴开始裁剪
- offsets:给出 3 个维度的裁剪偏移量
例如,若 axis = 1,offset = (10,100,100),upscore 的 size 为 (20,20,512,512),data 的 size 为(20,10,256,256),那么:top = upscore[:,10:10+data.shape[1],100:100+data.shape[2],100:100+data.shape[3]]
当 offset 的三个维度都相等时,可以设 offset 为一个数,表示在三个维度上的偏移是相同的。
比如上面的 axis = 2,offset = 19,表示 upscore 的前两维(batch_size 和 channels)都不变,只对图像大小进行裁剪,且裁剪偏移量为 19。