-
Notifications
You must be signed in to change notification settings - Fork 2
/
sample_image.m
63 lines (47 loc) · 1.11 KB
/
sample_image.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
function [labels, scores] = sample_image(image, network, txt_labels)
%% CONFIGURATION
% network
[model, weights] = caffe_network(network);
% default labels
if ~exist('txt_labels', 'var') || isempty(txt_labels)
txt_labels = 'resources/labels.txt';
end
%% SETUP
% configure caffe
caffe.set_mode_cpu();
%caffe.set_device(0);
% create net and load weights
net = caffe.Net(model, weights, 'test');
% cropped image size
net_input_shape = net.blobs('data').shape;
cropped_dim = net_input_shape(1);
%% EXECUTION
% load image
im = imread(image);
% prepare input
input = {prepare_image(im, cropped_dim)};
% run network
output = net.forward(input);
% scores
scores = mean(output{1}, 2);
% load labels
labels = {};
fh = fopen(txt_labels);
while 1
line = fgetl(fh);
if ~ischar(line), break, end
labels{end+1} = line; %#ok<AGROW>
end
fclose(fh);
% print if no output arguments
if 0 == nargout
% sorted scores
[sorted_scores, idx] = sort(scores, 'descend');
% print top five labels
for i = 1:5
fprintf('%f\t%s\n', sorted_scores(i), labels{idx(i)});
end
end
%% CLEAN UP
caffe.reset_all();
end