-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathml_demo.m
89 lines (82 loc) · 2.52 KB
/
ml_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
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
%% Deep Label Distribution Learning for Multi-label Classification
% add the MatConvNet toolbox to MATLAB path
% run(fullfile(fileparts(mfilename('fullpath')),...
% './External/matconvnet-1.0-beta18', 'matlab', 'vl_setupnn.m')) ;
addpath('./External/edges');
addpath(genpath('./External/Piotr-CV'));
addpath(genpath('./External/Ncut_9'));
addpath('./utils')
gpu_id = 12;
% load pre-trained edge detection model
model=load('./External/edges/models/forest/modelBsds');
model=model.model;
model.opts.multiscale=0;
model.opts.sharpen=2;
model.opts.nThreads=4;
edge_model = model;
% load PFDLDL model
modelPath = sprintf('./DLDLModels/pfdldl_%s_%s_%s.mat', 'vgg16','max','voc07');
fprintf('load model from %s\n', modelPath);
tic;
load(modelPath, 'net', 'info');
toc;
for l = 1:numel(net.layers)
if strcmp(net.layers{l}.name, 'hcp')
break;
end
end
net.layers(l:end) = [];
if ~isempty(gpu_id)
gpuDevice(gpu_id);
net = vl_simplenn_move(net, 'gpu') ;
end
vl_simplenn_display(net);
rgbmean = net.meta.normalization.averageImage;
tic;
% load image
img_name = '2007_001311.jpg';
img_path = fullfile('./images/voc11', img_name);
im = imread(img_path);
im_patch = genImgProposal(im, edge_model, rgbmean);
patch_size = size(im_patch, 4);
batch_size = 250;
out_score = zeros(20, patch_size);
for t = 1:batch_size:patch_size
batch = t:min(t+batch_size-1, patch_size);
input_data = im_patch(:,:,:,batch);
if ~isempty(gpu_id)
input_data = gpuArray(input_data) ;
end
res = dldl_simplenn(net, input_data, [], [], ...
'accumulate', 0, ...
'mode', 'test', ...
'conserveMemory', true, ...
'backPropDepth', 0, ...
'sync', false, ...
'cudnn', true) ;
out_score(:,batch) = squeeze(gather(res(end).x));
end
max_score = max(out_score, [], 2);
pro_score = bsxfun(@times, exp(max_score), 1./sum(exp(max_score)));
%% visialization
classname = net.meta.classes.name;
[prob, id] = sort(pro_score, 'descend');
figure(1)
imshow(im)
strs = sprintf('Top1: %s, %.2f Top2: %s, %.2f Top3: %s, %.2f',...
classname{id(1)}, prob(1),classname{id(2)}, prob(2),classname{id(3)}, prob(3));
title(strs, 'color', 'r');
figure(2)
bar(pro_score)
colormap([255 165 0]./255)
grid on
xlim([1 20])
set(gca,'XTick',1:1:20);
set(gca,'XTickLabel',[]);
set(gca,'ycolor','black','FontSize',10)
set(gca,'xcolor','black','FontSize',10)
for i=1:20
text(i,-0.005,classname(i),'color','blue','HorizontalAlignment','right','rotation',45,'FontSize',12);
end
ylabel('Probability','color','blue','FontSize',12)
grid on