天天看點

手把手教你用matlab做深度學習(二)- --CNN

在上一篇部落格中,講解了怎麼用matlab搭建CNN網絡模型,并給出了訓練過程與結果。但是結果不是很滿意,大概訓練精度在80%左右,現在給出改進方案。

1.首先,我們可以把CNN濾波輸出數改大點,從原來的32改為numFilters = 128,完整的程式如下:

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

numImageCategories = 10;

categories(trainingLabels)

[height, width, numChannels, ~] = size(trainingImages);

imageSize = [height width numChannels];

inputLayer = imageInputLayer(imageSize)

% Convolutional layer parameters

filterSize = [5 5];

numFilters = 128;

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)

% 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)

]

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(256)

% 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

]

layers = [

    inputLayer

    middleLayers

    finalLayers

    ]

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

% 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');

% A trained network is loaded from disk to save time when running the

% example. Set this flag to true to train the network.

doTraining = true;

if doTraining    

    % Train a network.

    cifar10Net = trainNetwork(trainingImages, trainingLabels, layers, opts);

else

    % Load pre-trained detector for the example.

    load('rcnnStopSigns.mat','cifar10Net')       

end

% 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)

% Run the network on the test set.

YTest = classify(cifar10Net, testImages);

% Calculate the accuracy.

accuracy = sum(YTest == testLabels)/numel(testLabels)

訓練結果如下:

手把手教你用matlab做深度學習(二)- --CNN

從圖中可以看出結果非常好,訓練結果達到96%以上,然而測試結果基本沒有提高,

手把手教你用matlab做深度學習(二)- --CNN
手把手教你用matlab做深度學習(二)- --CNN

現在這種情況屬于High variance,這是由于過拟合造成的,現在要去解決這個問題。

繼續閱讀