Add a caffe.io.write_mean function to the MATLAB interface#3058
Add a caffe.io.write_mean function to the MATLAB interface#3058ronghanghu merged 1 commit intoBVLC:masterfrom
Conversation
matlab/+caffe/private/caffe_.cpp
Outdated
There was a problem hiding this comment.
I think you've messed up with dimensions. It should be int width = dims[0]; int height = dims[1];
|
Actually, MATLAB has a Column-major order, so the order of the dimensions in memory is: Images in MATLAB are definitely stored as HxWxC, so the following is correct: Did you have any specific reason why you used (width, height, channels, num) for the MATLAB memory layout? |
Yes, you are right. There has been a lot of confusion on Caffe's dimensions, so let me try to clarify here. If you load an image using Matlab's However, this characteristic of Matlab's image toolbox (height being the fastest dimension) is inconsistent with many other image tools, such as OpenCV. In OpenCV (both C/C++ and Python), the default memory layout will be (H x W x C, row-major) in C-contiguous array, that is, channel being the fastest dimension, followed by width, and height being the slowest dimension. Since C/C++ is row-major and Matlab is column major, OpenCV's memory layout (H x W x C, row-major) is exactly the opposite from Matlab (H x W x C, column-major). When designing Caffe, to be consistent with C/C++ and numpy's default, and to make it easier to do convolution with existing BLAS libraries, we adopted (N x C x H x W, row-major) memory layout. This is neither Matlab convention nor OpenCV convention. Also, OpenCV uses BGR channel order by default while Matlab uses RGB channel order, and we follow OpenCV to use BGR in Caffe. So if you load an image using Matlab's imread, you will need to:
In your code above, to make the mean protobinary consistent with the rest of Caffe and loadable by Caffe's data layers, you will need to set And remember convert dimensions to (W x H x C, column-major), channel order to BGR before running your |
|
Right, my mistake, thanks for the clear & detailed explanation. Fixed & Pushed, I think it's all good now. |
|
@zoharby Please squash into one single commit, and I'll merge. |
matlab/+caffe/private/caffe_.cpp
Outdated
There was a problem hiding this comment.
Better to be consistent with matlab code in nomination here and use 'mean_data' instead of 'mean_mat'.
|
Fixed the naming you suggested and squashed (took a few attempts but finally figured it out, learning as I ago along...). |
Useful for exporting models from MATLAB (e.g. MatConvNet) to Caffe
|
Everything looks good now :) @zoharby Thank you for this PR! |
Add a caffe.io.write_mean function to the MATLAB interface
Useful for exporting models from MATLAB (e.g. MatConvNet) to Caffe