Introductory example of Matlab deep learning: traffic light recognition based on AlexNet (with complete code)

AlexNet appeared in ImageNet's image classification competition in 2012 and won the championship that year. Since then, convolutional neural networks have received strong attention from people. AlexNet is the beginning of the upsurge in deep convolutional neural network research, and it is also a sign of the transition of research hotspots from traditional visual methods to convolutional neural networks.

The AlexNet model contains a total of 8 layers, including 5 convolutional layers and 3 fully connected layers. Compared with LeNet, the feature of AlexNet is that for each convolutional layer, RELU and local response normalization are included.

The address of the dataset folder must be set to be the same as that of the m-file of the neural network. The dataset folder is named TrafficLightSamples and contains subfolders as follows. There are 500 pictures in each category:

Some pictures are as follows:

Some functions of building the network will not be described in detail, you can check the previous article (LeNet).

Code implementation and detailed explanation:

1. Load image samples:

% 功能:对AlexNet用样本数据进行训练,实现对输入图像的识别
clear
close all
clc
% 加载图像数据
imds = imageDatastore('TrafficLightSamples', ...
       'IncludeSubfolders',true, ...
       'LabelSource','foldernames');

'IncludeSubfolders', true: include all files and subfolders in each folder;

'LabelSource','foldernames': Assign labels based on folder names and store them in the Labels property.

2. Divide the samples into training set and test set, and count the number of categories:

% 划分验证集和训练集
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7,'randomized');

% 确定训练数据中需要分类的种类
numClasses = numel(categories(imdsTrain.Labels));

imdsTrain is the training sample data, imdsValidation is the verification sample data, and 0.7 is the ratio of the training sample.

3. Build AlexNet and perform visual analysis:

% AlexNet网络
alexnet = [
    imageInputLayer([60,20,3])
    convolution2dLayer([11,11],48,'Padding','same','Stride',4)
    batchNormalizationLayer
    reluLayer
    maxPooling2dLayer(3,'Padding','same','Stride',2)
    convolution2dLayer([5,5],128,'Padding',2,'Stride',1)
    batchNormalizationLayer
    reluLayer
    maxPooling2dLayer(3,'Padding','same','Stride',2)
    convolution2dLayer([3 3],192,'Padding',1,'Stride',1)
    batchNormalizationLayer
    reluLayer
    convolution2dLayer([3 3],192,'Padding',1,'Stride',1)
    batchNormalizationLayer
    reluLayer
    convolution2dLayer([3 3],128,'Stride',1,'Padding',1)
    batchNormalizationLayer
    reluLayer
    maxPooling2dLayer(3,'Padding','same','Stride',2)
    fullyConnectedLayer(4096)
    reluLayer
    dropoutLayer(0.5)
    fullyConnectedLayer(numClasses)  % 将新的全连接层的输出设置为训练数据中的种类
    softmaxLayer                     % 添加新的Softmax层
    classificationLayer ];           % 添加新的分类层


% 对构建的网络进行可视化分析
lgraph = layerGraph(alexnet);
analyzeNetwork(lgraph)

batchNormalizationLayer: local response normalization;

reluLayer: RELU function activation;

dropoutLayer(0.5): Nodes are dropped, so that each neuron has a 50% probability of being removed to prevent overfitting;

analyzeNetwork: Visual analysis of the network, its operation results are as follows:

4. Adjust the image size of the training set and input stage to be the same as the AlexNet input layer:

%% 调整数据集

% 网络输入层的大小和通道数
inputSize = [60,20,3];

% 将批量训练图像的大小调整为与输入层的大小相同
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain);
% 将批量验证图像的大小调整为与输入层的大小相同
augimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation);

5. Configure training options and train the network:

%% 对网络进行训练

% 对训练参数进行设置
options = trainingOptions('sgdm', ...
    'MiniBatchSize',15, ...
    'MaxEpochs',3, ...
    'InitialLearnRate',0.0003, ...
    'Shuffle','every-epoch', ...
    'ValidationData',augimdsValidation, ...
    'ValidationFrequency',3, ...
    'Verbose',true, ...
    'Plots','training-progress');

% 用训练图像对网络进行训练
net = trainNetwork(augimdsTrain,alexnet,options);

The training options are as follows:

'sgdm': the training method is sgdm;

'MiniBatchSize',15: The number of small batch samples is 15;

'MaxEpochs',3: The maximum number of rounds is 3;

'InitialLearnRate',0.0003: the initial learning rate is 0.0003;

'Shuffle','every-epoch': shuffle the data before each round of training;

'ValidationData', augimdsValidation: the data used during training is augimdsValidation;

 'ValidationFrequency', 3: The validation frequency is 3 times/round;

  'Verbose', true: set open command window output;

 'Plots','training-progress': set to open the training progress graph, the code running results are as follows:

 The final verification accuracy reached an extremely high 99.22%.

6. Use the trained image to classify the new input image and calculate the accuracy:

%% 验证并显示结果

% 对训练好的网络采用验证数据集进行验证
[YPred,scores] = classify(net,augimdsValidation);

%% 计算分类准确率
YValidation = imdsValidation.Labels;
accuracy = mean(YPred == YValidation)

%% 创建并显示混淆矩阵
figure
confusionchart(YValidation,YPred)

confusionchart can generate a confusion matrix so that we can see the results of AlexNet verification more intuitively. The results of this code are as follows:

 The training accuracy reached a very high 99.22%, indicating that the network has achieved excellent training results and accurately classified the input images.

The complete code is as follows:

% 功能:对AlexNet用样本数据进行训练,实现对输入图像的识别
clear
close all
clc
% 加载图像数据
imds = imageDatastore('TrafficLightSamples', ...
       'IncludeSubfolders',true, ...
       'LabelSource','foldernames');

% 划分验证集和训练集
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7,'randomized');

% 确定训练数据中需要分类的种类
numClasses = numel(categories(imdsTrain.Labels));

% AlexNet网络
alexnet = [
    imageInputLayer([60,20,3])
    convolution2dLayer([11,11],48,'Padding','same','Stride',4)
    batchNormalizationLayer
    reluLayer
    maxPooling2dLayer(3,'Padding','same','Stride',2)
    convolution2dLayer([5,5],128,'Padding',2,'Stride',1)
    batchNormalizationLayer
    reluLayer
    maxPooling2dLayer(3,'Padding','same','Stride',2)
    convolution2dLayer([3 3],192,'Padding',1,'Stride',1)
    batchNormalizationLayer
    reluLayer
    convolution2dLayer([3 3],192,'Padding',1,'Stride',1)
    batchNormalizationLayer
    reluLayer
    convolution2dLayer([3 3],128,'Stride',1,'Padding',1)
    batchNormalizationLayer
    reluLayer
    maxPooling2dLayer(3,'Padding','same','Stride',2)
    fullyConnectedLayer(4096)
    reluLayer
    dropoutLayer(0.5)
    fullyConnectedLayer(numClasses)  % 将新的全连接层的输出设置为训练数据中的种类
    softmaxLayer                     % 添加新的Softmax层
    classificationLayer ];           % 添加新的分类层


% 对构建的网络进行可视化分析
lgraph = layerGraph(alexnet);
analyzeNetwork(lgraph)
%% 调整数据集

% 网络输入层的大小和通道数
inputSize = [60,20,3];

% 将批量训练图像的大小调整为与输入层的大小相同
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain);
% 将批量验证图像的大小调整为与输入层的大小相同
augimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation);

%% 对网络进行训练

% 对训练参数进行设置
options = trainingOptions('sgdm', ...
    'MiniBatchSize',15, ...
    'MaxEpochs',3, ...
    'InitialLearnRate',0.0003, ...
    'Shuffle','every-epoch', ...
    'ValidationData',augimdsValidation, ...
    'ValidationFrequency',3, ...
    'Verbose',true, ...
    'Plots','training-progress');

% 用训练图像对网络进行训练
net = trainNetwork(augimdsTrain,alexnet,options);

%% 验证并显示结果

% 对训练好的网络采用验证数据集进行验证
[YPred,scores] = classify(net,augimdsValidation);

%% 计算分类准确率
YValidation = imdsValidation.Labels;
accuracy = mean(YPred == YValidation)

%% 创建并显示混淆矩阵
figure
confusionchart(YValidation,YPred)

Guess you like

Origin blog.csdn.net/qq_51942551/article/details/128028245