标签:
%% Download and predict using a pretrained ImageNet model
% Setup MatConvNet
run(fullfile(‘matconvnet-1.0-beta15‘,‘matlab‘,‘vl_setupnn.m‘));
% Download ImageNet model from MatConvNet pretrained networks repository
urlwrite(‘http://www.vlfeat.org/matconvnet/models/imagenet-vgg-f.mat‘, ‘imagenet-vgg-f.mat‘);
cnnModel.net = load(‘imagenet-vgg-f.mat‘);
% Load and display an example image
imshow(‘dog_example.png‘);
img = imread(‘dog_example.png‘);
% Predict label using ImageNet trained vgg-f CNN model
label = cnnPredict(cnnModel,img);
title(label,‘FontSize‘,20)
%% Load images from folder
% Use imageSet to load images stored in pet_images folder
imset = imageSet(‘pet_images‘,‘recursive‘);
% Preallocate arrays with fixed size for prediction
imageSize = cnnModel.net.normalization.imageSize;
trainingImages = zeros([imageSize sum([imset(:).Count])],‘single‘);
% Load and resize images for prediction
for ii = 1:numel(imset)
for jj = 1:imset(ii).Count
trainingImages(:,:,:,jj) = imresize(single(read(imset(ii),jj)),imageSize(1:2));
end
end
% Get the image labels
trainingLabels = getImageLabels(imset);
summary(trainingLabels) % Display class label distribution
%% Extract features using pretrained CNN
% Depending on how much memory you have on your GPU you may use a larger
% batch size. I have 400 images, so I choose 200 as my batch size
cnnModel.info.opts.batchSize = 200;
% Make prediction on a CPU
[~, cnnFeatures, timeCPU] = cnnPredict(cnnModel,trainingImages,‘UseGPU‘,false);
% Make prediction on a GPU
[~, cnnFeatures, timeGPU] = cnnPredict(cnnModel,trainingImages,‘UseGPU‘,true);
% Compare the performance increase
bar([sum(timeCPU),sum(timeGPU)],0.5)
title(sprintf(‘Approximate speedup: %2.00f x ‘,sum(timeCPU)/sum(timeGPU)))
set(gca,‘XTickLabel‘,{‘CPU‘,‘GPU‘},‘FontSize‘,18)
ylabel(‘Time(sec)‘), grid on, grid minor
gpuArray
和gather
函数可以使你将MATLAB 工作空间中的数据转换到GPU中%% Train a classifier using extracted features
% Here I train a linear support vector machine (SVM) classifier.
svmmdl = fitcsvm(cnnFeatures,trainingLabels);
% Perform crossvalidation and check accuracy
cvmdl = crossval(svmmdl,‘KFold‘,10);
fprintf(‘kFold CV accuracy: %2.2f\n‘,1-cvmdl.kfoldLoss)
%% Tying the workflow together
vr = VideoReader(fullfile(‘PetVideos‘,‘videoExample.mov‘));
vw = VideoWriter(‘test.avi‘,‘Motion JPEG AVI‘);
opticFlow = opticalFlowFarneback;
open(vw);
while hasFrame(vr)
% Count frames
frameNumber = frameNumber + 1;
% Step 1. Read Frame
videoFrame = readFrame(vr);
% Step 2. Detect ROI
vFrame = imresize(videoFrame,0.25); % Get video frame
frameGray = rgb2gray(vFrame); % Convert to gray for detection
bboxes = findPet(frameGray,opticFlow); % Find bounding boxes
if ~isempty(bboxes)
img = zeros([imageSize size(bboxes,1)]);
for ii = 1:size(bboxes,1)
img(:,:,:,ii) = imresize(imcrop(videoFrame,bboxes(ii,:)),imageSize(1:2));
end
% Step 3. Recognize object
% (a) Extract features using a CNN
[~, scores] = cnnPredict(cnnModel,img,‘UseGPU‘,true,‘display‘,false);
% (b) Predict using the trained SVM Classifier
label = predict(svmmdl,scores);
% Step 4. Annotate object
videoFrame = insertObjectAnnotation(videoFrame,‘Rectangle‘,bboxes,cellstr(label),‘FontSize‘,40);
end
% Step 5. Write video to file
writeVideo(vw,videoFrame);
fprintf(‘Frames processed: %d of %d\n‘,frameNumber,ceil(vr.FrameRate*vr.Duration));
end
close(vw);
Deep Learning for Computer Vision with MATLAB and cuDNN(译文)
标签:
原文地址:http://blog.csdn.net/jnulzl/article/details/51096357