文章出處
文章列表
matcaffe是caffe的matlab接口。caffe本身是C++寫的,其blob的維度順序是[N,C,H,W],N表示batchSize,C表示channel數目,H表示feature map的Height, W則是width
而在matcaffe中,blob的順序則是[W,H,C,N]。這是默認的順序!所以在faster-rcnn的matlab代碼中,當加載了proposal_test.prototxt
后,發現其網絡輸入是:
input: "data"
input_dim: 1
input_dim: 3
input_dim: 224
input_dim: 224
一開始還奇怪,為啥在matlab中設定斷點后輸出網絡的blob的結構是這樣的:
K>> rpn_net.blobs('data').shape
ans =
224 224 3 1
為啥不是[1,3,224,224]這個在prototxt中指定的順序呢?雖然官方caffe的tutorial里也說了,matcaffe的blob順序就是[W,H,C,N]
翻看matcaffe的代碼:caffe_.cpp
,找到370行左右:
// Usage: caffe_('blob_get_shape', hBlob)
static void blob_get_shape(MEX_ARGS) {
mxCHECK(nrhs == 1 && mxIsStruct(prhs[0]),
"Usage: caffe_('blob_get_shape', hBlob)");
Blob<float>* blob = handle_to_ptr<Blob<float> >(prhs[0]);
const int num_axes = blob->num_axes();
mxArray* mx_shape = mxCreateDoubleMatrix(1, num_axes, mxREAL);
double* shape_mem_mtr = mxGetPr(mx_shape);
for (int blob_axis = 0, mat_axis = num_axes - 1; blob_axis < num_axes;
++blob_axis, --mat_axis) {
shape_mem_mtr[mat_axis] = static_cast<double>(blob->shape(blob_axis));
}
plhs[0] = mx_shape;
}
其中最關鍵的是這個:
for (int blob_axis = 0, mat_axis = num_axes - 1; blob_axis < num_axes;
++blob_axis, --mat_axis) {
shape_mem_mtr[mat_axis] = static_cast<double>(blob->shape(blob_axis));
}
這里面mat_axis
這個索引是倒序的,從3到0;而blob_axis
這個索引是正序增加的,從0到3。因此最終的結果是:matcaffe中的blob維度順序和caffe中順序完全相反,是[W,H,C,N]
文章列表
全站熱搜