forked from apache/mxnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.m
53 lines (45 loc) · 1.35 KB
/
demo.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
%% Assumes model symbol and parameters already downloaded using .sh script
%% Load the model
clear model
format compact
model = mxnet.model;
model.load('data/Inception-BN', 126);
%% Load and resize the image
img = imresize(imread('data/cat.png'), [224 224]);
img = single(img) - 120;
%% Run prediction
pred = model.forward(img);
%% load the labels
labels = {};
fid = fopen('data/synset.txt', 'r');
assert(fid >= 0);
tline = fgetl(fid);
while ischar(tline)
labels{end+1} = tline;
tline = fgetl(fid);
end
fclose(fid);
%% Print top 5 predictions
fprintf('Top 5 predictions: \n');
[p, i] = sort(pred, 'descend');
for x = 1:5
fprintf(' %2.2f%% - %s\n', p(x)*100, labels{i(x)} );
end
%% Print the last 10 layers in the symbol
fprintf('\nLast 10 layers in the symbol: \n');
sym = model.parse_symbol();
layers = {};
for i = 1 : length(sym.nodes)
if ~strcmp(sym.nodes{i}.op, 'null')
layers{end+1} = sym.nodes{i}.name;
end
end
fprintf(' layer name: %s\n', layers{end-10:end})
%% Extract feature from internal layers
fprintf('\nExtract feature from internal layers using CPU forwarding: \n');
feas = model.forward(img, {'max_pool_5b_pool', 'global_pool', 'fc1'});
feas(:)
%% If GPU is available
fprintf('\nExtract feature from internal layers using GPU forwarding: \n');
feas = model.forward(img, 'gpu', 0, {'max_pool_5b_pool', 'global_pool', 'fc1'});
feas(:)