Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7e3f4b7

Browse files
committedMar 27, 2021
Adding demo files
0 parents  commit 7e3f4b7

12 files changed

+666
-0
lines changed
 

‎CIFARTraining/cifar10CNN.m

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
%% Classifying the CIFAR-10 dataset using Convolutional Neural Networks
2+
% This example shows how to train a Convolutional Neural Network (CNN) from
3+
% scratch using the dataset CIFAR10.
4+
%
5+
% Data Credit: Krizhevsky, A., & Hinton, G. (2009). Learning multiple
6+
% layers of features from tiny images.
7+
8+
% Copyright 2016 The MathWorks, Inc.
9+
10+
%% Download the CIFAR-10 dataset
11+
if ~exist('cifar-10-batches-mat','dir')
12+
cifar10Dataset = 'cifar-10-matlab';
13+
disp('Downloading 174MB CIFAR-10 dataset...');
14+
websave([cifar10Dataset,'.tar.gz'],...
15+
['https://www.cs.toronto.edu/~kriz/',cifar10Dataset,'.tar.gz']);
16+
gunzip([cifar10Dataset,'.tar.gz'])
17+
delete([cifar10Dataset,'.tar.gz'])
18+
untar([cifar10Dataset,'.tar'])
19+
delete([cifar10Dataset,'.tar'])
20+
end
21+
22+
%% Prepare the CIFAR-10 dataset
23+
if ~exist('cifar10Train','dir')
24+
disp('Saving the Images in folders. This might take some time...');
25+
saveCIFAR10AsFolderOfImages('cifar-10-batches-mat', pwd, true);
26+
end
27+
28+
%% Load image CIFAR-10 Training dataset (50000 32x32 colour images in 10 classes)
29+
imsetTrain = imageSet('cifar10Train','recursive');
30+
31+
%% Display Sampling of Image Data
32+
numClasses = size(imsetTrain,2);
33+
imagesPerClass = 10;
34+
imagesInMontage = cell(imagesPerClass,numClasses);
35+
for i = 1:size(imagesInMontage,2)
36+
imagesInMontage(:,i) = ...
37+
imsetTrain(i).ImageLocation(randi(imsetTrain(i).Count, 1, ...
38+
imagesPerClass));
39+
end
40+
41+
montage({imagesInMontage{:}},'Size',[numClasses,imagesPerClass]);
42+
title('Sample of Training Data (Credit:Learning Multiple Layers of Features from Tiny Images, Alex Krizhevsky, 2009.)')
43+
44+
%% Prepare the data for Training
45+
% Read all images and store them in a 4D uint8 input array for training,
46+
% with its corresponding class
47+
48+
trainNames = {imsetTrain.Description};
49+
XTrain = zeros(32,32,3,sum([imsetTrain.Count]),'uint8');
50+
TTrain = categorical(discretize((1:sum([imsetTrain.Count]))',...
51+
[0,cumsum([imsetTrain.Count])],'categorical',trainNames));
52+
53+
j = 0;
54+
tic;
55+
for c = 1:length(imsetTrain)
56+
for i = 1:imsetTrain(c).Count
57+
XTrain(:,:,:,i+j) = read(imsetTrain(c),i);
58+
end
59+
j = j + imsetTrain(c).Count;
60+
end
61+
toc;
62+
63+
%% Define a CNN architecture
64+
conv1 = convolution2dLayer(5,32,'Padding',2,...
65+
'BiasLearnRateFactor',2);
66+
conv1.Weights = gpuArray(single(randn([5 5 3 32])*0.0001));
67+
fc1 = fullyConnectedLayer(64,'BiasLearnRateFactor',2);
68+
fc1.Weights = gpuArray(single(randn([64 576])*0.1));
69+
fc2 = fullyConnectedLayer(10,'BiasLearnRateFactor',2);
70+
fc2.Weights = gpuArray(single(randn([10 64])*0.1));
71+
72+
layers = [ ...
73+
imageInputLayer([32 32 3]);
74+
conv1;
75+
maxPooling2dLayer(3,'Stride',2);
76+
reluLayer();
77+
convolution2dLayer(5,32,'Padding',2,'BiasLearnRateFactor',2);
78+
reluLayer();
79+
averagePooling2dLayer(3,'Stride',2);
80+
convolution2dLayer(5,64,'Padding',2,'BiasLearnRateFactor',2);
81+
reluLayer();
82+
averagePooling2dLayer(3,'Stride',2);
83+
fc1;
84+
reluLayer();
85+
fc2;
86+
softmaxLayer()
87+
classificationLayer()];
88+
89+
% Define the training options.
90+
opts = trainingOptions('sgdm', ...
91+
'InitialLearnRate', 0.001, ...
92+
'LearnRateSchedule', 'piecewise', ...
93+
'LearnRateDropFactor', 0.1, ...
94+
'LearnRateDropPeriod', 8, ...
95+
'L2Regularization', 0.004, ...
96+
'MaxEpochs', 10, ...
97+
'MiniBatchSize', 100, ...
98+
'Verbose', true);
99+
100+
%% Training the CNN
101+
[net, info] = trainNetwork(XTrain, TTrain, layers, opts);
102+
103+
% Alternative way using imageDataStore
104+
% imdsTrain = imageDatastore(fullfile(pwd,'cifar10Train'),...
105+
% 'IncludeSubfolders',true,'LabelSource','foldernames');
106+
% [net, info] = trainNetwork(imdsTrain, layers, opts);
107+
108+
%% Visualise the first layer weights.
109+
figure;
110+
montage(mat2gray(gather(net.Layers(2).Weights)));
111+
title('First Layer Weights');
112+
113+
%% Load Test Data
114+
115+
imsetTest = imageSet('cifar10Test','recursive');
116+
117+
testNames = {imsetTest.Description};
118+
XTest = zeros(32,32,3,sum([imsetTest.Count]),'uint8');
119+
TTest = categorical(discretize((1:sum([imsetTest.Count]))',...
120+
[0,cumsum([imsetTest.Count])],'categorical',testNames));
121+
j = 0;
122+
tic;
123+
for c = 1:length(imsetTest)
124+
for i = 1:imsetTest(c).Count
125+
XTest(:,:,:,i+j) = read(imsetTest(c),i);
126+
end
127+
j = j + imsetTest(c).Count;
128+
end
129+
toc;
130+
131+
%% Run the network on the test set
132+
133+
YTest = classify(net, XTest);
134+
135+
% Alternative way using imageDataStore
136+
% imdsTest = imageDatastore(fullfile(pwd, 'cifar10Test'),...
137+
% 'IncludeSubfolders',true,'LabelSource','foldernames');
138+
% YTest = classify(net, imdsTest);
139+
140+
% Calculate the accuracy.
141+
accuracy = sum(YTest == TTest)/numel(TTest)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
% Copyright 2016 The MathWorks, Inc.
2+
3+
function [XTrain, TTrain, XTest, TTest] = loadCIFAR10AsAFourDimensionalArray()
4+
5+
% loadCIFAR10AsAFourDimensionalArray Load the CIFAR-10 data
6+
7+
[XTrain1, TTrain1] = iLoadBatchAsFourDimensionalArray('data_batch_1.mat');
8+
[XTrain2, TTrain2] = iLoadBatchAsFourDimensionalArray('data_batch_2.mat');
9+
[XTrain3, TTrain3] = iLoadBatchAsFourDimensionalArray('data_batch_3.mat');
10+
[XTrain4, TTrain4] = iLoadBatchAsFourDimensionalArray('data_batch_4.mat');
11+
[XTrain5, TTrain5] = iLoadBatchAsFourDimensionalArray('data_batch_5.mat');
12+
13+
XTrain = cat(4, XTrain1, XTrain2, XTrain3, XTrain4, XTrain5);
14+
TTrain = [TTrain1; TTrain2; TTrain3; TTrain4; TTrain5];
15+
16+
[XTest, TTest] = iLoadBatchAsFourDimensionalArray('test_batch.mat');
17+
18+
XTrain = double(XTrain);
19+
XTest = double(XTest);
20+
end
21+
22+
function [XBatch, TBatch] = iLoadBatchAsFourDimensionalArray(batchFileName)
23+
load(batchFileName);
24+
XBatch = data';
25+
XBatch = reshape(XBatch, 32,32,3,[]);
26+
XBatch = permute(XBatch, [2 1 3 4]);
27+
TBatch = iConvertLabelsToCategorical(labels);
28+
end
29+
30+
function categoricalLabels = iConvertLabelsToCategorical(integerLabels)
31+
load('batches.meta.mat');
32+
categoricalLabels = categorical(integerLabels, 0:9, label_names);
33+
end
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
% Copyright 2016 The MathWorks, Inc.
2+
3+
function saveCIFAR10AsFolderOfImages(inputPath, outputPath, varargin)
4+
% saveCIFAR10AsFolderOfImages Save the CIFAR-10 dataset as a folder of images
5+
% saveCIFAR10AsFolderOfImages(inputPath, outputPath) takes the CIFAR-10
6+
% dataset located at inputPath and saves it as a folder of images to the
7+
% directory outputPath. If inputPath or outputPath is an empty string, it
8+
% is assumed that the current folder should be used.
9+
%
10+
% saveCIFAR10AsFolderOfImages(..., labelDirectories) will save the
11+
% CIFAR-10 data so that instances with the same label will be saved to
12+
% sub-directories with the name of that label.
13+
14+
% Check input directories are valid
15+
if(~isempty(inputPath))
16+
assert(exist(inputPath,'dir') == 7);
17+
end
18+
if(~isempty(outputPath))
19+
assert(exist(outputPath,'dir') == 7);
20+
end
21+
22+
% Check if we want to save each set with the same labels to its own
23+
% directory.
24+
if(isempty(varargin))
25+
labelDirectories = false;
26+
else
27+
assert(nargin == 3);
28+
labelDirectories = varargin{1};
29+
end
30+
31+
% Set names for directories
32+
trainDirectoryName = 'cifar10Train';
33+
testDirectoryName = 'cifar10Test';
34+
35+
% Create directories for the output
36+
mkdir(fullfile(outputPath, trainDirectoryName));
37+
mkdir(fullfile(outputPath, testDirectoryName));
38+
39+
if(labelDirectories)
40+
labelNames = {'airplane','automobile','bird','cat','deer','dog','frog','horse','ship','truck'};
41+
iMakeTheseDirectories(fullfile(outputPath, trainDirectoryName), labelNames);
42+
iMakeTheseDirectories(fullfile(outputPath, testDirectoryName), labelNames);
43+
for i = 1:5
44+
iLoadBatchAndWriteAsImagesToLabelFolders(fullfile(inputPath,['data_batch_' num2str(i) '.mat']), fullfile(outputPath, trainDirectoryName), labelNames, (i-1)*10000);
45+
end
46+
iLoadBatchAndWriteAsImagesToLabelFolders(fullfile(inputPath,'test_batch.mat'), fullfile(outputPath, testDirectoryName), labelNames, 0);
47+
else
48+
for i = 1:5
49+
iLoadBatchAndWriteAsImages(fullfile(inputPath,['data_batch_' num2str(i) '.mat']), fullfile(outputPath, trainDirectoryName), (i-1)*10000);
50+
end
51+
iLoadBatchAndWriteAsImages(fullfile(inputPath,'test_batch.mat'), fullfile(outputPath, testDirectoryName), 0);
52+
end
53+
end
54+
55+
function iLoadBatchAndWriteAsImagesToLabelFolders(fullInputBatchPath, fullOutputDirectoryPath, labelNames, nameIndexOffset)
56+
load(fullInputBatchPath);
57+
data = data'; %#ok<NODEF>
58+
data = reshape(data, 32,32,3,[]);
59+
data = permute(data, [2 1 3 4]);
60+
for i = 1:size(data,4)
61+
imwrite(data(:,:,:,i), fullfile(fullOutputDirectoryPath, labelNames{labels(i)+1}, ['image' num2str(i + nameIndexOffset) '.png']));
62+
end
63+
end
64+
65+
function iLoadBatchAndWriteAsImages(fullInputBatchPath, fullOutputDirectoryPath, nameIndexOffset)
66+
load(fullInputBatchPath);
67+
data = data'; %#ok<NODEF>
68+
data = reshape(data, 32,32,3,[]);
69+
data = permute(data, [2 1 3 4]);
70+
for i = 1:size(data,4)
71+
imwrite(data(:,:,:,i), fullfile(fullOutputDirectoryPath, ['image' num2str(i + nameIndexOffset) '.png']));
72+
end
73+
end
74+
75+
function iMakeTheseDirectories(outputPath, directoryNames)
76+
for i = 1:numel(directoryNames)
77+
mkdir(fullfile(outputPath, directoryNames{i}));
78+
end
79+
end

‎LICENSE.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright (c) 2017, The MathWorks, Inc.
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are
6+
met:
7+
8+
* Redistributions of source code must retain the above copyright
9+
notice, this list of conditions and the following disclaimer.
10+
* Redistributions in binary form must reproduce the above copyright
11+
notice, this list of conditions and the following disclaimer in
12+
the documentation and/or other materials provided with the distribution.
13+
* In all cases, the software is, and all modifications and derivatives
14+
of the software shall be, licensed to you solely for use in conjunction
15+
with MathWorks products and service offerings.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27+
POSSIBILITY OF SUCH DAMAGE.

‎README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# DeepLearning-for-ComputerVision-with-MATLAB
2+
[![View Example files for "Deep Learning for Computer Vision with MATLAB" Webinar on File Exchange](https://www.mathworks.com/matlabcentral/images/matlab-file-exchange.svg)](https://www.mathworks.com/matlabcentral/fileexchange/58030-example-files-for-deep-learning-for-computer-vision-with-matlab-webinar)
3+
4+
These are the example files used in the webinar ["Aprendizaje Profundo para Visión Artificial con MATLAB" - Spanish](https://www.mathworks.com/videos/deep-learning-for-computer-vision-with-matlab-1540981496452.html) ("Deep Learning for Computer Vision with MATLAB").
5+
6+
7+
Deep Learning is an area of Machine Learning that uses multiple nonlinear processing layers to learn useful representations of features directly from data. This webinar shows the fundamentals of Deep Learning for Computer Vision and how to use Convolutional Neural Networks (popularly known as CNNs or ConvNets) to solve object classification/recognition problems.
8+
9+
The source code consists of 3 different examples:
10+
1. Running a trained CNN (/WebcamClassification)
11+
2. Training a CNN from scratch (/CIFARTraining)
12+
3. Fine-tuning a pre-trained CNN. Transfer learning (/TransferLearning)
13+
Examples 1) and 3) make use AlexNet [1]. In order to download the trained CNN [2], run the file downloadAndPrepareCNN.m (if using R2016a) or download AlexNet Network support package (if using R2016b or later).
14+
15+
References:
16+
[1] Krizhevsky, A., Sutskever, I., & Hinton, G. E. (2012). Imagenet classification with Deep Convolutional Neural Networks. Advances in Neural Information Processing Systems (pp. 1097-1105).
17+
[2] Vedaldi, A., & Lenc, K. (2015, October). MatConvNet: Convolutional Neural Networks for MATLAB. Proceedings of the 23rd ACM International Conference on Multimedia (pp. 689-692). ACM.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
% Copyright 2016 The MathWorks, Inc.
2+
3+
function DisplayImageMontage(cellArrayOfImages)
4+
% Displays a montage of images. Images are resized to handle different
5+
% image sizes.
6+
7+
thumbnails = [];
8+
for i = 1:numel(cellArrayOfImages)
9+
img = imread(cellArrayOfImages{i});
10+
thumbnails = cat(4, thumbnails, imresize(img, [200 200]));
11+
end
12+
13+
montage(thumbnails)
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
%% Fine Tuning A Deep Neural Network
2+
% This example shows how to fine tune a pre-trained deep convolutional
3+
% neural network (CNN) for a new recognition task.
4+
5+
% Copyright 2016 The MathWorks, Inc.
6+
7+
%% Load network
8+
cnnMatFile = fullfile(pwd, '..', 'networks', 'imagenet-cnn.mat');
9+
if ~exist(cnnMatFile,'file')
10+
disp('Run downloadAndPrepareCNN.m to download and prepare the CNN')
11+
return;
12+
end
13+
imagenet_cnn = load(cnnMatFile);
14+
net = imagenet_cnn.convnet;
15+
16+
%% Look at structure of pre-trained network
17+
% Notice the last layer performs 1000 object classification
18+
net.Layers
19+
20+
%% Perform net surgery
21+
% The pre-trained layers at the end of the network are designed to classify
22+
% 1000 objects. But we need to classify 2 different objects now. So the
23+
% first step in transfer learning is to replace the last 3 layers of the
24+
% pre-trained network with a set of layers that can classify 2 classes.
25+
26+
% Get the layers from the network. the layers define the network
27+
% architecture and contain the learned weights. Here we only need to keep
28+
% everything except the last 3 layers.
29+
layers = net.Layers(1:end-3);
30+
31+
% Add new fully connected layer for 2 categories.
32+
layers(end+1) = fullyConnectedLayer(2, 'Name', 'fc8_2')
33+
34+
% Add the softmax layer and the classification layer which make up the
35+
% remaining portion of the networks classification layers.
36+
layers(end+1) = softmaxLayer('Name','prob_2');
37+
layers(end+1) = classificationLayer('Name','classificationLayer_2')
38+
39+
% Modify image layer to add randcrop data augmentation. This increases the
40+
% diversity of training images. The size of the input images is set to the
41+
% original networks input size.
42+
layers(1) = imageInputLayer([227 227 3], 'DataAugmentation', 'randcrop');
43+
44+
45+
%% Setup learning rates for fine-tuning
46+
% For fine-tuning, we want to changed the network ever so slightly. How
47+
% much a network is changed during training is controlled by the learning
48+
% rates. Here we do not modify the learning rates of the original layers,
49+
% i.e. the ones before the last 3. The rates for these layers are already
50+
% pretty small so they don't need to be lowered further. You could even
51+
% freeze the weights of these early layers by setting the rates to zero.
52+
%
53+
% Instead we boost the learning rates of the new layers we added, so that
54+
% they change faster than the rest of the network. This way earlier layers
55+
% don't change that much and we quickly learn the weights of the newer
56+
% layer.
57+
58+
% fc 8 - bump up learning rate for last layers
59+
layers(end-2).WeightLearnRateFactor = 100;
60+
layers(end-2).WeightL2Factor = 1;
61+
layers(end-2).BiasLearnRateFactor = 20;
62+
layers(end-2).BiasL2Factor = 0;
63+
64+
%% Load Image Data
65+
% Now we get the training data. This was collected as traffic drove into
66+
% the MathWorks head office in Natick.
67+
%
68+
% Create an imageDataStore to read images
69+
location = 'VehicleData';
70+
imds = imageDatastore(location,'IncludeSubfolders',1,'LabelSource','foldernames');
71+
tbl = countEachLabel(imds);
72+
73+
% Display a sampling of training images
74+
75+
%% Display Sampling of Image Data
76+
% Notice that the cars and SUV look very similar
77+
imagesInMontage = cell(10,2);
78+
carFiles = imds.Files(imds.Labels == 'Car');
79+
suvFiles = imds.Files(imds.Labels == 'SUV');
80+
imagesInMontage(:,1) = carFiles(1:length(carFiles)/10:length(carFiles));
81+
imagesInMontage(:,2) = suvFiles(1:length(suvFiles)/10:length(suvFiles));
82+
figure;
83+
subplot(1,2,1);
84+
DisplayImageMontage({imagesInMontage{:,1}});title('Cars');
85+
subplot(1,2,2);
86+
DisplayImageMontage({imagesInMontage{:,2}});title('SUV');
87+
88+
%% Equalize number of images of each class in training set
89+
minSetCount = min(tbl{:,2}); % determine the smallest amount of images in a category
90+
% Use splitEachLabel method to trim the set.
91+
imds = splitEachLabel(imds, minSetCount);
92+
93+
% Notice that each set now has exactly the same number of images.
94+
countEachLabel(imds)
95+
[trainingDS, testDS] = splitEachLabel(imds, 0.7,'randomize');
96+
% Convert labels to categoricals
97+
trainingDS.Labels = categorical(trainingDS.Labels);
98+
trainingDS.ReadFcn = @readFunctionTrain;
99+
100+
%% Setup test data for validation
101+
testDS.Labels = categorical(testDS.Labels);
102+
testDS.ReadFcn = @readFunctionValidation;
103+
104+
%% Fine-tune the Network
105+
106+
miniBatchSize = 32; % lower this if your GPU runs out of memory.
107+
numImages = numel(trainingDS.Files);
108+
109+
% Run training for 5000 iterations. Convert 20000 iterations into the
110+
% number of epochs this will be.
111+
numIterationsPerEpoch = numImages/miniBatchSize;
112+
%maxEpochs = round(20000/numIterationsPerEpoch);
113+
maxEpochs = 100;
114+
lr = 0.001;
115+
opts = trainingOptions('sgdm', ...
116+
'InitialLearnRate', lr,...
117+
'LearnRateSchedule', 'none',...
118+
'L2Regularization', 0.0005, ...
119+
'MaxEpochs', maxEpochs, ...
120+
'MiniBatchSize', miniBatchSize);
121+
122+
net = trainNetwork(trainingDS, layers, opts);
123+
% This could take over an hour to run.
124+
125+
%% Test 2-class classifier on validation set
126+
% Now run the network on the test data set to see how well it does:
127+
128+
labels = classify(net, testDS, 'MiniBatchSize', 32);
129+
130+
confMat = confusionmat(testDS.Labels, labels);
131+
confMat = bsxfun(@rdivide,confMat,sum(confMat,2));
132+
133+
mean(diag(confMat))
134+
135+
%% Run on Video Stream
136+
videoPlayer = vision.DeployableVideoPlayer;
137+
138+
% Learn the background ( this takes a few seconds )
139+
foregroundDetector = vision.ForegroundDetector('NumGaussians', 3, ...
140+
'NumTrainingFrames', 50,'MinimumBackgroundRatio',0.5);
141+
roi = [139.25 239.75 985.5 400];
142+
videoReader = vision.VideoFileReader('MorningTraffic2.mp4');
143+
for i = 1:150
144+
frame = step(videoReader); % read the next video frame
145+
frame = imcrop(frame,roi);
146+
frameSmall = imresize(frame,0.25);
147+
foreground = step(foregroundDetector, frameSmall);
148+
end
149+
blobAnalysis = vision.BlobAnalysis('BoundingBoxOutputPort', true, ...
150+
'AreaOutputPort', false, 'CentroidOutputPort', false, ...
151+
'MinimumBlobArea', 300);
152+
153+
154+
%% Detect Cars using Background Subtraction then classify
155+
se = strel('square', 3); % morphological filter for noise removal
156+
while ~isDone(videoReader)
157+
158+
frame = step(videoReader); % read the next video frame
159+
frame = imcrop(frame,roi); % crop to region of interest
160+
frameSmall = imresize(frame,0.25);
161+
foreground = step(foregroundDetector, frameSmall);
162+
163+
% Use morphological opening to remove noise in the foreground
164+
filteredForeground = imopen(foreground, se);
165+
166+
% Detect the connected components with the specified minimum area, and
167+
% compute their bounding boxes
168+
bbox = step(blobAnalysis, filteredForeground);
169+
170+
numCars = size(bbox,1);
171+
bbox = 4.*bbox;
172+
result = frame;
173+
if numCars > 0
174+
for j=1:numCars
175+
176+
% Crop a slightly larger region around car
177+
im = imcrop(frame,[bbox(j,1)-10 bbox(j,2)-10 bbox(j,3)+30 bbox(j,4)+30]);
178+
179+
im = single(im2uint8(im));
180+
im = imresize(im, [227 227]) ;
181+
label = char(classify(net,im)); % classify with deep learning
182+
result = insertObjectAnnotation(result,'Rectangle',bbox(j,:),label,'FontSize',32);
183+
end
184+
185+
end
186+
step(videoPlayer,result);
187+
% Draw bounding boxes around the detected cars
188+
189+
end
190+
191+
release(videoReader); % close the video file

‎TransferLearning/readFunctionTrain.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
% Copyright 2016 The MathWorks, Inc.
2+
3+
function I = readFunctionTrain(filename)
4+
% Resize the flowers images to the size required by the network.
5+
I = imread(filename);
6+
7+
I = imresize(I, [227 227]);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
% Copyright 2016 The MathWorks, Inc.
2+
3+
function I = readFunctionValidation(filename)
4+
% Resize the flowers images to the size required by the network.
5+
I = imread(filename);
6+
7+
I = imresize(I, [227 227]);
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
% Copyright 2016-2017 The MathWorks, Inc.
2+
3+
function cnn_webcam_demo(numTopClasses)
4+
5+
if nargin == 0
6+
numTopClasses = 5;
7+
end
8+
9+
if verLessThan('nnet','9.1') % Neural Network Toolbox from R2016a or earlied
10+
cnnMatFile = fullfile(pwd, '..', 'networks', 'imagenet-cnn.mat');
11+
12+
if ~exist(cnnMatFile,'file')
13+
disp('Run ''downloadAndPrepareCNN.m'' to download and prepare the CNN')
14+
return;
15+
else
16+
imagenet_cnn = load(cnnMatFile);
17+
convnet = imagenet_cnn.convnet;
18+
end
19+
20+
% Convert ImageNet synset IDs to meaningful word descriptions
21+
synsetsFile = fullfile(fileparts(mfilename('fullpath')),'..',...
22+
'networks','synsetMap');
23+
24+
% Image net class labels are synset IDs.
25+
synsetMap = load(synsetsFile);
26+
synsetMap = synsetMap.synsetMap;
27+
synsetMapKeys = keys(synsetMap);
28+
categories = values(synsetMap, synsetMapKeys);
29+
else
30+
try
31+
convnet = alexnet;
32+
catch
33+
error('Download the CNN model: AlexNet from the Add-On Explorer.');
34+
end
35+
36+
% Proper category names already available in last layer. No need to
37+
% use syset IDs
38+
categories = convnet.Layers(end).ClassNames;
39+
end
40+
41+
wcam = webcam;
42+
player = vision.DeployableVideoPlayer('Name', ...
43+
'Object Detection using Convolutional Neural Networks');
44+
45+
% Configure position of labels and scores in Video Player
46+
imgResolution = str2double(strsplit(wcam.Resolution,'x'));
47+
classBarWidth = 30;
48+
spaceBetweenBars = 15;
49+
xPosition = imgResolution(1) + 50;
50+
yPosition = round((imgResolution(2) - classBarWidth * ...
51+
numTopClasses - spaceBetweenBars * (numTopClasses - 1))/2);
52+
53+
cont = true;
54+
while cont
55+
img = snapshot(wcam);
56+
57+
I = imresize(img, convnet.Layers(1).InputSize(1:2));
58+
[~, scores] = classify(convnet, I);
59+
[scores, idx] = sort(scores, 'descend');
60+
61+
labelsTopCategories = categories(idx(1:numTopClasses));
62+
topScores = scores(1:numTopClasses)' * 100;
63+
64+
img = insertTopClasses(img, labelsTopCategories, topScores);
65+
step(player,img)
66+
67+
cont = isOpen(player);
68+
end
69+
70+
function img = insertTopClasses(img, labels, scores)
71+
72+
labelsXposition = xPosition * ones(numTopClasses,1);
73+
labelsYposition = yPosition * ones(numTopClasses,1) + ...
74+
(classBarWidth + spaceBetweenBars) * (0:numTopClasses-1)';
75+
76+
img = padarray(img, [0 300], 0, 'post');
77+
78+
% Top class scores
79+
img = insertShape(img, 'FilledRectangle', [labelsXposition, ...
80+
labelsYposition, 2*round(scores), ...
81+
classBarWidth * ones(numTopClasses,1)], 'Color', 'green');
82+
83+
scoreLabels = cellfun(@(c1,c2) sprintf('%s (%2.2f %%)', c1, c2),...
84+
labels(:), num2cell(scores), 'UniformOutput', false);
85+
86+
% Top class labels
87+
img = insertText(img, [labelsXposition, labelsYposition], ...
88+
scoreLabels, 'BoxOpacity', 0, 'TextColor', 'white', ...
89+
'FontSize', 14);
90+
91+
% Highlighting top class
92+
img = insertText(img, [imgResolution(1)/2 30], labels(1), ...
93+
'BoxColor', 'green', 'FontSize', 20, ...
94+
'AnchorPoint', 'CenterTop');
95+
end
96+
97+
end

‎WebcamClassification/synsets2words.m

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
% Copyright 2016 The MathWorks, Inc.
2+
3+
function synsets2words(convnet)
4+
% This function converts ImageNet synset IDs to meaningful word
5+
% descriptions
6+
7+
synsetsFile = fullfile(fileparts(mfilename('fullpath')), '..', 'networks', 'synsetMap.mat');
8+
url = 'http://www.image-net.org/api/text/wordnet.synset.getwords?wnid=%s';
9+
10+
% image net class names are synset IDs. Get the description instead
11+
synsets = convnet.Layers(end).ClassNames;
12+
synsetMap = containers.Map;
13+
synsetWords = cell(1,numel(synsets));
14+
15+
fprintf(1,'Converting ImageNet synset IDs to meaningful word descriptions\n');
16+
fprintf(1,['Processing %0',num2str(length(num2str(numel(synsets)))),'i of %i.\n'],0,numel(synsets));
17+
18+
for i = 1:numel(synsets)
19+
fprintf(1,['\b\b\b\b\b\b\b',repmat('\b',1,2*numel(num2str(numel(synsets)))),...
20+
' %0',num2str(length(num2str(numel(synsets)))),'i of %i.\n'], i, numel(synsets));
21+
synsetWords{i} = urlread(sprintf(url,synsets{i}));
22+
synsetWords{i} = strtok(synsetWords{i},sprintf('\n'));
23+
synsetMap(synsets{i}) = synsetWords{i};
24+
end
25+
26+
save(synsetsFile, 'synsetMap');

‎downloadAndPrepareCNN.m

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
% Copyright 2016-2017 The MathWorks, Inc.
2+
3+
function downloadAndPrepareCNN()
4+
% Download AlexNet CNN in MatConvNet format and converts to SerialNetwork
5+
6+
addpath(fullfile(pwd, 'WebcamClassification'));
7+
mkdir(fullfile(pwd,'networks'));
8+
cnnMatFile = fullfile(pwd, 'networks', 'imagenet-cnn.mat');
9+
synsetsFile = fullfile(pwd, 'networks', 'synsetMap.mat');
10+
11+
if ~exist(cnnMatFile,'file')
12+
13+
cnnSourceMatFile = fullfile(pwd, 'networks', 'imagenet-caffe-alex.mat');
14+
cnnURL = 'http://www.vlfeat.org/matconvnet/models/beta16/imagenet-caffe-alex.mat';
15+
16+
if ~exist(cnnSourceMatFile,'file') % download only once
17+
disp('Downloading 233MB AlexNet pre-trained CNN model. This may take several minutes...');
18+
websave(cnnSourceMatFile, cnnURL);
19+
end
20+
convnet = helperImportMatConvNet(cnnSourceMatFile);
21+
22+
save(cnnMatFile, 'convnet');
23+
delete(cnnSourceMatFile)
24+
end
25+
26+
if ~exist(synsetsFile,'file')
27+
synsets2words(convnet);
28+
end

0 commit comments

Comments
 (0)
Please sign in to comment.