2018年08月11日 17:36:14 yuanCruise 閱讀數:793更多
所屬專欄: Caffe源碼學習
版權聲明:本文爲博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/qiu931110/article/details/81588956
- Layer type: Reshape
- 頭文件位置:./include/caffe/layers/reshape_layer.hpp
- CPU 執行源文件位置: ./src/caffe/layers/reshape_layer.cpp
- Reshape層的功能:根據給定參數改變輸入blob的維度,僅僅改變數據的維度,但內容不變。
參數解讀
layer {
name: "reshape"
type: "Reshape"
bottom: "input"
top: "output"
reshape_param {
shape {
dim: 0 # copy the dimension from below
dim: 2
dim: 3
dim: -1 # infer it from the other dimensions
}
}
}
如上圖中的dim,具體還以如下:
#有一個可選的參數組shape, 用於指定blob數據的各維的值(blob是一個四維的數據:n*c*w*h)。
#dim:0 表示維度不變,即輸入和輸出是相同的維度。
#dim:2 或 dim:3 將原來的維度變成2或3
#dim:-1 表示由系統自動計算維度。數據的總量不變,系統會根據blob數據的其它三維來自動計算當前維的維度值 。
#假設原數據爲:32*3*28*28, 表示32張3通道的28*28的彩色圖片
# shape {
# dim: 0 32-32
# dim: 0 3-3
# dim: 14 28-14
# dim: -1 #讓其推斷出此數值
# }
#輸出數據爲:32*3*14*56
參數定義
參數(ReshapeParameter reshape_param)
定義位置 ./src/caffe/proto/caffe.proto:
可選參數組:shape
message ReshapeParameter {
// Specify the output dimensions. If some of the dimensions are set to 0,
// the corresponding dimension from the bottom layer is used (unchanged).
// Exactly one dimension may be set to -1, in which case its value is
// inferred from the count of the bottom blob and the remaining dimensions.
// For example, suppose we want to reshape a 2D blob "input" with shape 2 x 8:
//
// layer {
// type: "Reshape" bottom: "input" top: "output"
// reshape_param { ... }
// }
//
// If "input" is 2D with shape 2 x 8, then the following reshape_param
// specifications are all equivalent, producing a 3D blob "output" with shape
// 2 x 2 x 4:
//
// reshape_param { shape { dim: 2 dim: 2 dim: 4 } }
// reshape_param { shape { dim: 0 dim: 2 dim: 4 } }
// reshape_param { shape { dim: 0 dim: 2 dim: -1 } }
// reshape_param { shape { dim: 0 dim:-1 dim: 4 } }
//
optional BlobShape shape = 1;
// axis and num_axes control the portion of the bottom blob's shape that are
// replaced by (included in) the reshape. By default (axis == 0 and
// num_axes == -1), the entire bottom blob shape is included in the reshape,
// and hence the shape field must specify the entire output shape.
//
// axis may be non-zero to retain some portion of the beginning of the input
// shape (and may be negative to index from the end; e.g., -1 to begin the
// reshape after the last axis, including nothing in the reshape,
// -2 to include only the last axis, etc.).
//
// For example, suppose "input" is a 2D blob with shape 2 x 8.
// Then the following ReshapeLayer specifications are all equivalent,
// producing a blob "output" with shape 2 x 2 x 4:
//
// reshape_param { shape { dim: 2 dim: 2 dim: 4 } }
// reshape_param { shape { dim: 2 dim: 4 } axis: 1 }
// reshape_param { shape { dim: 2 dim: 4 } axis: -3 }
//
// num_axes specifies the extent of the reshape.
// If num_axes >= 0 (and axis >= 0), the reshape will be performed only on
// input axes in the range [axis, axis+num_axes].
// num_axes may also be -1, the default, to include all remaining axes
// (starting from axis).
//
// For example, suppose "input" is a 2D blob with shape 2 x 8.
// Then the following ReshapeLayer specifications are equivalent,
// producing a blob "output" with shape 1 x 2 x 8.
//
// reshape_param { shape { dim: 1 dim: 2 dim: 8 } }
// reshape_param { shape { dim: 1 dim: 2 } num_axes: 1 }
// reshape_param { shape { dim: 1 } num_axes: 0 }
//
// On the other hand, these would produce output blob shape 2 x 1 x 8:
//
// reshape_param { shape { dim: 2 dim: 1 dim: 8 } }
// reshape_param { shape { dim: 1 } axis: 1 num_axes: 0 }
//
optional int32 axis = 2 [default = 0];
optional int32 num_axes = 3 [default = -1];
}
番外篇
Reshape layer只改變輸入數據的維度,但內容不變,也沒有數據複製的過程,與Flatten layer類似。
輸出維度由reshape_param 指定,正整數直接指定維度大小,下面兩個特殊的值:
0 => 表示copy the respective dimension of the bottom layer,複製輸入相應維度的值。
-1 => 表示infer this from the other dimensions,根據其他維度自動推測維度大小。reshape_param中至多只能有一個-1。
再舉一個例子:如果指定reshape_param參數爲:{ shape { dim: 0 dim: -1 } } ,那麼輸出和Flattening layer的輸出是完全一樣的。
Flatten層的操作詳見:【caffe】Layer解讀之:Flatten