When I want to do something by matlab, I need the .mat files, e.g. mean.binaryproto.
But I don't know to do it? How to convert the .binarproto to .mat ?
Thank you very much!
Unfortunately there's no straightforward way to do this at this point. I have written a Matlab script to perform the same procedure as _compute_image_mean_ application. If you have used _convert_image_set_ with a _train.txt_ file which lists the files, you can use this code to generate the corresponding _mean.mat_.
%% This script calculates the mean image!
%% Configuration
config = struct();
config.base_address = '/path-to-the-dataset/';
config.target_file = 'train.txt';
config.target_size = [60 60];
config.result_file = 'train_mean_60.mat';
%% Doing the job!
fid = fopen([config.base_address config.target_file]);
fileLines = textscan(fid, '%[^ ] %d', 'delimiter', '\n', 'BufSize', 100000);
fclose(fid);
files = fileLines{1, 1};
file_count = numel(files);
fprintf('Found %d files\n', file_count);
images = cell(file_count, 1);
fprintf('Processing the files ...'); tic;
parfor f = 1:file_count,
fprintf('Doing %s\n', files{f});
im = imread([config.base_address files{f}]);
im = imresize(im, config.target_size, 'bilinear');
images{f} = im;
end
fprintf(' done in %.2fs\n', toc);
%% The rest of the job
fprintf('Calculating the mean\n');
images_reshape = cellfun(@(x) reshape(x, [1 config.target_size 3]), images, 'UniformOutput', false);
images_reshape = cell2mat(images_reshape);
image_mean_exp = mean(images_reshape);
image_mean = single(squeeze(image_mean_exp));
%% Saving the output
fprintf('Saving the file ...'); tic;
save([config.base_address config.result_file], 'image_mean');
fprintf(' done in %.2fs\n', toc);
%% clear up
clear image_mean images_reshape image_mean_exp images;
Note that I have used _parfor_ and I load and resize all the images before I calculate the mean value, so it's not memory efficient. Depending on your dataset, RAM, and CPU you may have to change the code.
Note that this does not give the exact value as the mean.proto because of the resize algorithm, but it's approximately the same and should work.
Thank you very much, I think this is a good idea~
Glad to have this problem already solved. FWIW, the best way is to manipulate protobufs easily in python: matlab does not have protobuf bindings.
Basically, load the protobuffer in python, and convert it to a numpy array:
https://github.com/BVLC/caffe/blob/master/python/caffe/io.py
You can then manipulate it fairly easily with tools that you like. Numpy can save arrays to matlab format as well.
You may also find the python protobuf developer guide useful:
https://developers.google.com/protocol-buffers/docs/pythontutorial
I think a better way to convert mean.binaryproto to .mat file is using read_mean() function, which can be found in caffe/matlab/+caffe/private/io.m
@BingZhangHu Thank you. But could you please provide a code snippet?
if (strfind(conf.image_means,'binaryproto'))
s=caffe.io.read_mean(conf.image_means);
s_mean(:,:,1)=mean2(s(:,:,1));
s_mean(:,:,2)=mean2(s(:,:,2));
s_mean(:,:,3)=mean2(s(:,:,3));
conf.image_means = s_mean;
Most helpful comment
I think a better way to convert mean.binaryproto to .mat file is using read_mean() function, which can be found in caffe/matlab/+caffe/private/io.m