Skip to content

Commit

Permalink
Merge pull request BVLC#3058 from zoharby/master
Browse files Browse the repository at this point in the history
Add a caffe.io.write_mean function to the MATLAB interface
  • Loading branch information
ronghanghu committed Oct 5, 2015
2 parents 6eae122 + 552a84a commit 92dc4e6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
8 changes: 8 additions & 0 deletions matlab/+caffe/io.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,13 @@
CHECK_FILE_EXIST(mean_proto_file);
mean_data = caffe_('read_mean', mean_proto_file);
end
function write_mean(mean_data, mean_proto_file)
% write_mean(mean_data, mean_proto_file)
% write image mean data to binaryproto file
% mean_data should be W x H x C with BGR channels
CHECK(ischar(mean_proto_file), 'mean_proto_file must be a string');
CHECK(isa(mean_data, 'single'), 'mean_data must be a SINGLE matrix');
caffe_('write_mean', mean_data, mean_proto_file);
end
end
end
24 changes: 24 additions & 0 deletions matlab/+caffe/private/caffe_.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,29 @@ static void read_mean(MEX_ARGS) {
mxFree(mean_proto_file);
}

// Usage: caffe_('write_mean', mean_data, mean_proto_file)
static void write_mean(MEX_ARGS) {
mxCHECK(nrhs == 2 && mxIsSingle(prhs[0]) && mxIsChar(prhs[1]),
"Usage: caffe_('write_mean', mean_data, mean_proto_file)");
char* mean_proto_file = mxArrayToString(prhs[1]);
int ndims = mxGetNumberOfDimensions(prhs[0]);
mxCHECK(ndims >= 2 && ndims <= 3, "mean_data must have at 2 or 3 dimensions");
const mwSize *dims = mxGetDimensions(prhs[0]);
int width = dims[0];
int height = dims[1];
int channels;
if (ndims == 3)
channels = dims[2];
else
channels = 1;
Blob<float> data_mean(1, channels, height, width);
mx_mat_to_blob(prhs[0], &data_mean, DATA);
BlobProto blob_proto;
data_mean.ToProto(&blob_proto, false);
WriteProtoToBinaryFile(blob_proto, mean_proto_file);
mxFree(mean_proto_file);
}

/** -----------------------------------------------------------------
** Available commands.
**/
Expand Down Expand Up @@ -515,6 +538,7 @@ static handler_registry handlers[] = {
{ "get_init_key", get_init_key },
{ "reset", reset },
{ "read_mean", read_mean },
{ "write_mean", write_mean },
// The end.
{ "END", NULL },
};
Expand Down

0 comments on commit 92dc4e6

Please sign in to comment.