forked from kevinlin311tw/Caffe-DeepBinaryCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare_batch.m
More file actions
executable file
·44 lines (40 loc) · 1.32 KB
/
prepare_batch.m
File metadata and controls
executable file
·44 lines (40 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
% ------------------------------------------------------------------------
function images = prepare_batch(image_files,mean_data,batch_size)
% ------------------------------------------------------------------------
if nargin < 2
% load mean file
d = load('/home/iis/Research/packages/caffe-master/matlab/+caffe/imagenet/ilsvrc_2012_mean.mat');
mean_data = d.mean_data;
end
num_images = length(image_files);
if nargin < 3
batch_size = num_images;
end
IMAGE_DIM = 256;
CROPPED_DIM = 227;
indices = [0 IMAGE_DIM-CROPPED_DIM] + 1;
center = floor(indices(2) / 2)+1;
num_images = length(image_files);
images = zeros(CROPPED_DIM,CROPPED_DIM,3,batch_size,'single');
parfor i=1:num_images
% read file
fprintf('%c Preparing %s\n',13,image_files{i});
try
im = imread(image_files{i});
% resize to fixed input size
im = single(im);
im = imresize(im, [IMAGE_DIM IMAGE_DIM], 'bilinear');
% Transform GRAY to RGB
if size(im,3) == 1
im = cat(3,im,im,im);
end
% permute from RGB to BGR (IMAGE_MEAN is already BGR)
im = im(:,:,[3 2 1]) - mean_data;
% im = im(:,:,[3 2 1]);
% Crop the center of the image
images(:,:,:,i) = permute(im(center:center+CROPPED_DIM-1,...
center:center+CROPPED_DIM-1,:),[2 1 3]);
catch
warning('Problems with file',image_files{i});
end
end