-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextractCaffeNetFeaturesValidation.m
89 lines (67 loc) · 2.63 KB
/
extractCaffeNetFeaturesValidation.m
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
facesLoaded = exist( 'facesVal', 'var' );
if facesLoaded == 0
load( 'validation.mat' );
end
caffe.set_mode_gpu();
gpu_id = 0; % we will use the first gpu in this demo
caffe.set_device(gpu_id);
chalearn = 1;
% Initialize the network using dex-imdb-wiki model.
if chalearn
model_dir = '/home/iarganda/workspace/caffe/models/dex_chalearn/';
else
model_dir = '/home/iarganda/workspace/caffe/models/dex_imdb_wiki/';
end
net_model = [model_dir 'age.prototxt'];
if chalearn
net_weights = [model_dir 'dex_chalearn_iccv2015.caffemodel'];
else
net_weights = [model_dir 'dex_imdb_wiki.caffemodel'];
end
phase = 'test'; % run with phase test (so that dropout isn't applied)
if ~exist(net_weights, 'file')
error('Please download IDMB_WIKI from its site before you run this demo');
end
% Initialize a network
net = caffe.Net(net_model, net_weights, phase);
% caffe/matlab/+caffe/imagenet/ilsvrc_2012_mean.mat contains mean_data that
% is already in W x H x C with BGR channels
d = load('/home/iarganda/workspace/caffe/matlab/+caffe/imagenet/ilsvrc_2012_mean.mat');
mean_data = d.mean_data;
CROPPED_DIM = 224;
mean_data_resized = imresize(mean_data,[CROPPED_DIM CROPPED_DIM], 'bilinear');
numFiles = length( facesVal );
tic;
for K = 1 : numFiles
filename = [ facesVal{K}.path facesVal{K}.filename ];
im = imread( filename );
if size( im, 3) == 1
im = cat( 3, im, im, im );
end
% Convert an image returned by Matlab's imread to im_data in caffe's data
% format: W x H x C with BGR channels
im_data = im(:, :, [3, 2, 1]); % permute channels from RGB to BGR
im_data = permute(im_data, [2, 1, 3]); % flip width and height
im_data = single(im_data); % convert from uint8 to single
im_data = imresize(im_data, [CROPPED_DIM CROPPED_DIM], 'bilinear'); % resize im_data
im_data = im_data - mean_data_resized; % subtract mean_data (already in W x H x C, BGR)
crops_data=im_data;
input_data = { crops_data };
% The net forward function. It takes in a cell array of N-D arrays
% (where N == 4 here) containing data of input blob(s) and outputs a cell
% array containing data from output blob(s)
scores = net.forward(input_data);
fc7 = net.blobs('fc7').get_data();
fc6 = net.blobs('fc6').get_data();
if chalearn
facesVal{ K }.dex_chalearn_features_fc7 = fc7;
facesVal{ K }.dex_chalearn_features_fc6 = fc6;
else
facesVal{ K }.imdb_wiki_features_fc7 = fc7;
facesVal{ K }.imdb_wiki_features_fc6 = fc6;
end
end
toc;
% call caffe.reset_all() to reset caffe
caffe.reset_all();
save( 'validation.mat', 'facesVal', '-v7.3' );