forked from vlfeat/matconvnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcnn_imagenet_minimal.m
34 lines (27 loc) · 1.1 KB
/
cnn_imagenet_minimal.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
function cnn_imagenet_minimal()
% CNN_IMAGENET_MINIMAL Minimalistic demonstration of how to run an ImageNet CNN model
% Setup MatConvNet.
run(fullfile(fileparts(mfilename('fullpath')), ...
'..', '..', 'matlab', 'vl_setupnn.m')) ;
% Download a pre-trained CNN from the web.
if ~exist('imagenet-vgg-f.mat', 'file')
fprintf('Downloading the VGG-F model ... this may take a while\n') ;
urlwrite('http://www.vlfeat.org/matconvnet/models/imagenet-vgg-f.mat', ...
'imagenet-vgg-f.mat') ;
end
% Load the model and upgrade it to MatConvNet current version.
net = load('imagenet-vgg-f.mat') ;
net = vl_simplenn_tidy(net) ;
% Obtain and preprocess an image.
im = imread('peppers.png') ;
im_ = single(im) ; % note: 255 range
im_ = imresize(im_, net.meta.normalization.imageSize(1:2)) ;
im_ = im_ - net.meta.normalization.averageImage ;
% Run the CNN.
res = vl_simplenn(net, im_) ;
% Show the classification result.
scores = squeeze(gather(res(end).x)) ;
[bestScore, best] = max(scores) ;
figure(1) ; clf ; imagesc(im) ;
title(sprintf('%s (%d), score %.3f',...
net.meta.classes.description{best}, best, bestScore)) ;