1. 程式人生 > >手把手教你用matlab做深度學習(一)- --CNN

手把手教你用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)

下面給出我的訓練過程:

執行結果:

如果你想直觀的看訓練過程,只要增加一條即可:

% 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'這一條。

現在看看重新執行效果:

看,這個整個訓練過程就可以看出來了,是不是很直觀。從上面可以看出訓練的精度在80%左右,下一篇部落格將介紹怎麼提高訓練精度。