参考文献:https://blog.youkuaiyun.com/ytusdc/article/details/86610897
1.Reshape
- Layer type: Reshape
- 头文件位置: ./include/caffe/layers/reshape_layer.hpp
- CPU 执行源文件位置:./src/caffe/layers/reshape_layer.cpp
reshape层的功能:Flatten层用来将输入“压平”,即把多维的输入一维化,常用在从卷积层到全连接层的过渡。Flatten不影响batch的大小;
把一个输入的大小为n * c * h * w变成一个简单的向量,其大小为 n * (c*h*w)。可以用reshape代替~,相当于第一维不变,后面的自动计算。【Reshape层的param参数为:{ shape { dim: 0 dim: -1 } } ,那么输出和flatten输出是完全一样的。】
message ReshapeParameter {
optional BlobShape shape = 1;
optional int32 axis = 2 [default = 0];
optional int32 num_axes = 3 [default = -1];
}
使用方法:
"基础使用:"
layer {
type: "Reshape"
bottom: "input"
top: "output"
reshape_param { shape{ . . . } }
}
"关键部分:shape{}中的dim参数分别用于指定输入blob各维(n*c*w*h)的值[这里是3D也可以用于2D]"
"eg1:input:(32*3*28*28)-->output:(32*2*3*(3*28*28/2/3=392))=(32*2*3*392)"
shape{
dim:0 "维度不变,直接copy"
dim:2 "将原来的维度变为2 "
dim:3 "将原来的维度变为3 "
dim:-1 "由系统自动计算维度, "
}
"等价于:"
shape{
dim:0 "维度不变,直接copy"
dim:2 "将原来的维度变为2 "
dim:3 "将原来的维度变为3 "
}
axis:-1 "指定由系统自动计算值的维度,这里就是dim4:h是由系统自动计算"
"num_axes和axis:除了[axis,axis+num_axes]下标的输出维度,其他维度是通过系统自由计算得到的;
dim指定的是输出下标[axis,axis+num]的维度;
一般axis默认为0-->[0,num_axes]"
"eg1等价于"
shape{
dim:0 "维度不变,直接copy"
dim:2 "将原来的维度变为2 "
dim:3 "将原来的维度变为3 "
}
num_axes=2
"特殊地想要:input:(3*28*28)-->output:(1*3*28*28)"
shape{
dim:1
dim:0
dim:0
dim:0
}
==>
shape{
dim:1
dim:0
dim:0
}
num_axes:2
==>
"这里表示[0,0]为指定,[1,3]为系统自由推导计算:第一规则是直接复制"
shape{
dim:1
}
num_axes:0
2.Flatten
- Layer type: Flatten
- 头文件位置: ./include/caffe/layers/flatten_layer.hpp
- CPU 执行源文件位置:./src/caffe/layers/flatten_layer.cpp
Flatten层的功能:Flatten层用来将输入“压平”,即把多维的输入一维化,常用在从卷积层到全连接层的过渡。Flatten不影响batch的大小;
/// Message that stores parameters used by FlattenLayer
message FlattenParameter {
// The first axis to flatten: all preceding axes are retained in the output.
// May be negative to index from the end (e.g., -1 for the last axis).
optional int32 axis = 1 [default = 1];"从哪个轴开始平铺(该轴前面的都保留原状)"
// The last axis to flatten: all following axes are retained in the output.
// May be negative to index from the end (e.g., the default -1 for the last
// axis).
optional int32 end_axis = 2 [default = -1];"哪个轴结束平铺(该轴后面的都保留原状)"
}
把一个输入的大小为n * c * h * w变成一个简单的向量,其大小为 n * (c*h*w)。可以用reshape代替~,相当于第一维不变,后面的自动计算。【Reshape层的param参数为:{ shape { dim: 0 dim: -1 } } ,那么输出和flatten输出是完全一样的。】
使用方法:[以SSD为例]
layer {
name: "data_flat"
type: "Flatten"
bottom: "input"
top: "output"
flatten_param {
axis: 1
}
}
3.concat
- Layer type: Concat
- 头文件位置: ./include/caffe/layers/concat_layer.hpp
- CPU 执行源文件位置:./src/caffe/layers/concat_layer.cpp
- CUDA GPU 执行源文件位置: ./src/caffe/layers/concat_layer.cu
Concat层的功能:Concat层实现输入数据的拼接。Concat层是一个实用程序层,它将多个输入blob连接到一个输出blob(按照给定的axis,注意除了规定的axis以外,被concat的输入bolb的其他维度的size必须一致)。
参数(ConcatParameter concat_param)
定义位置: ./src/caffe/proto/caffe.proto
message ConcatParameter {
// The axis along which to concatenate -- may be negative to index from the
// end (e.g., -1 for the last axis). Other axes must have the
// same dimension for all the bottom blobs.
// By default, ConcatLayer concatenates blobs along the "channels" axis (1).
// 指定拼接的维度,默认为1即以channel通道进行拼接;支持负索引,即-1表示最后一个维度
optional int32 axis = 2 [default = 1];
// DEPRECATED: alias for "axis" -- does not support negative indexing.
// 以后会被弃用,作用同axis一样,但不能指定为负数
optional uint32 concat_dim = 1 [default = 1];
}
caffe中数据通常为4个维度,即 num×channels×height×width,因此默认值1表示channels通道进行拼接。而0表示num这个维度,即数量上的叠加。
除了拼接维度外的其它维度都必须相等。比如上面,输入图像均为 24×24×3,用于分类的有150张图片,用于boundingbox回归的有50张,用于关键点回归的也有50张,则最后拼接的结果就是(150+50+50)×3×24×24
使用方法:
layer {
name: "data_concat"
type: "Concat"
bottom: "data_classfier"
bottom: "data_boundingbox"
bottom: "data_facialpoints"
top: "data"
concat_param {
axis: 0
}
}
4.conadd
5.eltwise
- Layer type: Eltwise
- 头文件位置: ./include/caffe/layers/eltwise_layer.hpp
- CPU 执行源文件位置:./src/caffe/layers/eltwise_layer.cpp
- CUDA GPU 执行源文件位置: ./src/caffe/layers/eltwise_layer.cu
Eltwise层的功能:有三个-->product(点乘), sum(相加减) 和 max(取大值),其中sum是默认操作。
PROD表示将A、B按元素相乘,SUM表示将A、B按元素求和,MAX表示将A、B按元素求最大值。
使用方法:
layer {
name: "data_eltwise"
type: "Eltwise"
bottom: "data_A"
bottom: "data_B"
top: "data"
eltwise_param {
operation: SUM
coeff: 1 "A*coeff_A"
coeff: -1 "B*coeff_B"
"A*coeff_A+B*coeff_B=A-B"
}
}
6.slice
SSD特辑:
1.VideoData
2.DetectionOutput