手把手教你用matlab做深度学习(一)- --CNN

 

1.使用深度学习做目标检测

上一篇博客已经讲解了怎么用matlab导入数据。

[trainingImages,trainingLabels,testImages,testLabels] = helperCIFAR10Data.load('cifar10Data');

使用这个指令就可以导入CIFAR-10 data的数据。

使用下面指令查看样本和图片大小:

size(trainingImages)

CIFAR-10 数据集有10类,使用指令列出:

numImageCategories = 10;
categories(trainingLabels)

1.接下来我们来建立CNN模型,这里建立输入层:

% Create the image input layer for 32x32x3 CIFAR-10 images
[height, width, numChannels, ~] = size(trainingImages);

imageSize = [height width numChannels];
inputLayer = imageInputLayer(imageSize)

2.建立网络中间层

% Convolutional layer parameters filter size
filterSize = [5 5];
numFilters = 32;

middleLayers = [
    
% The first convolutional layer has a bank of 32 5x5x3 filters. A
% symmetric padding of 2 pixels is added to ensure that image borders
% are included in the processing. This is important to avoid
% information at the borders being washed away too early in the
% network.
convolution2dLayer(filterSize, numFilters, 'Padding', 2)  %(n+2p-f)/s+1

% Note that the third dimension of the filter can be omitted because it
% is automatically deduced based on the connectivity of the network. In
% this case because this layer follows the image layer, the third
% dimension must be 3 to match the number of channels in the input
% image.

% Next add the ReLU layer:
reluLayer()

% Follow it with a max pooling layer that has a 3x3 spatial pooling area
% and a stride of 2 pixels. This down-samples the data dimensions from
% 32x32 to 15x15.
maxPooling2dLayer(3, 'Stride', 2)

% Repeat the 3 core layers to complete the middle of the network.
convolution2dLayer(filterSize, numFilters, 'Padding', 2)
reluLayer()
maxPooling2dLayer(3, 'Stride',2)

convolution2dLayer(filterSize, 2 * numFilters, 'Padding', 2)
reluLayer()
maxPooling2dLayer(3, 'Stride',2)


]

3.最后定义输出层

finalLayers = [
    
% Add a fully connected layer with 64 output neurons. The output size of
% this layer will be an array with a length of 64.
fullyConnectedLayer(64)

% Add an ReLU non-linearity.
reluLayer

% Add the last fully connected layer. At this point, the network must
% produce 10 signals that can be used to measure whether the input image
% belongs to one category or another. This measurement is made using the
% subsequent loss layers.
fullyConnectedLayer(numImageCategories)

% Add the softmax loss layer and classification layer. The final layers use
% the output of the fully connected layer to compute the categorical
% probability distribution over the image classes. During the training
% process, all the network weights are tuned to minimize the loss over this
% categorical distribution.
softmaxLayer
classificationLayer
]

 

4.合并

 

layers = [
    inputLayer
    middleLayers
    finalLayers
    ]

5.定义输入层权值,

layers(2).Weights = 0.0001 * randn([filterSize numChannels numFilters]);

6.这里参数解释,sgdm就是stochastic gradient descent with momentum(动量的随机梯度下降法),Momentum是动量参数为0.9,InitialLearnRate初始学习速率0.001,L2Regularization=0.004是L2正则化系数,LearnRateDropFactor=0.1、LearnRateDropPeriod=8是每8个epoces使得学习速率乘以一个0.1的比例因子,MaxEpochs= 40最大训练为40个epoces,MiniBatchSize=128为Batch为128,Verbose =true就是把信息打印到命令窗口

Note that the training algorithm uses a mini-batch size of 128 images. If using a GPU for training, this size may need to be lowered due to memory constraints on the GPU.

% Set the network training options
opts = trainingOptions('sgdm', ...
    'Momentum', 0.9, ...
    'InitialLearnRate', 0.001, ...
    'LearnRateSchedule', 'piecewise', ...
    'LearnRateDropFactor', 0.1, ...
    'LearnRateDropPeriod', 8, ...
    'L2Regularization', 0.004, ...
    'MaxEpochs', 40, ...
    'MiniBatchSize', 128, ...
    'Verbose', true);

 

7.这里,doTraining设置为false了,就是直接导入已经训练好的模型,你也可以把doTraining改为True,自己改模型训练,下一篇博客应该教大家怎么改模型,怎么训练

% A trained network is loaded from disk to save time when running the
% example. Set this flag to true to train the network.
doTraining = false;

if doTraining    
    % Train a network.
    cifar10Net = trainNetwork(trainingImages, trainingLabels, layers, opts);
else
    % Load pre-trained detector for the example.
    load('rcnnStopSigns.mat','cifar10Net')       
end

 

8.这里,你可以看到权值

% Extract the first convolutional layer weights
w = cifar10Net.Layers(2).Weights;

% rescale the weights to the range [0, 1] for better visualization
w = rescale(w);

figure
montage(w)

 

9.到这里,你已经成功了,可以看到accuracy

To completely validate the training results, use the CIFAR-10 test data to measure the classification accuracy of the network. A low accuracy score indicates additional training or additional training data is required. The goal of this example is not necessarily to achieve 100% accuracy on the test set, but to sufficiently train a network for use in training an object detector.

% Run the network on the test set.
YTest = classify(cifar10Net, testImages);

% Calculate the accuracy.
accuracy = sum(YTest == testLabels)/numel(testLabels)

下面给出我的训练过程:

手把手教你用matlab做深度学习(一)- --CNN_第1张图片

运行结果:

手把手教你用matlab做深度学习(一)- --CNN_第2张图片

如果你想直观的看训练过程,只要增加一条即可:

% Set the network training options
opts = trainingOptions('sgdm', ...
    'Momentum', 0.9, ...
    'InitialLearnRate', 0.001, ...
    'LearnRateSchedule', 'piecewise', ...
    'LearnRateDropFactor', 0.1, ...
    'LearnRateDropPeriod', 8, ...
    'L2Regularization', 0.004, ...
    'MaxEpochs', 40, ...
    'MiniBatchSize', 128, ...
    'Verbose', true,...
 'Plots','training-progress');

这里,你发现了与上面的不同吗?对,增加了'Plots','training-progress'这一条。

现在看看重新运行效果:

手把手教你用matlab做深度学习(一)- --CNN_第3张图片

看,这个整个训练过程就可以看出来了,是不是很直观。从上面可以看出训练的精度在80%左右,下一篇博客将介绍怎么提高训练精度。

你可能感兴趣的:(matlab,deeplearning)