-
Notifications
You must be signed in to change notification settings - Fork 18.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a caffe.io.write_mean function to the MATLAB interface #3058
Conversation
mxCHECK(ndims >= 2 && ndims <= 3, "mean_mat must have at 2 or 3 dimensions"); | ||
const mwSize *dims = mxGetDimensions(prhs[0]); | ||
int height = dims[0]; | ||
int width = dims[1]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
// Usage: caffe_('write_mean', mean_mat, mean_proto_file) | ||
static void write_mean(MEX_ARGS) { | ||
mxCHECK(nrhs == 2 && mxIsSingle(prhs[0]) && mxIsChar(prhs[1]), | ||
"Usage: caffe_('write_mean', mean_mat, mean_proto_file)"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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