MATLAB學習help之——Create Simple Deep Learning Network for Classification from Neural Network Toolbox

這是MATLAB關于Deep Learning 的一個入門的簡單的例程

Step1
加載并查看數據

digitDatasetPath = fullfile(matlabroot,'toolbox','nnet','nndemos', ...
        'nndatasets','DigitDataset');
digitData = imageDatastore(digitDatasetPath, ...
        'IncludeSubfolders',true,'LabelSource','foldernames');

然后隨機顯示其中的一部分如下

figure;
perm = randperm(10000,20);
for i = 1:20
    subplot(4,5,i);
    imshow(digitData.Files{perm(i)});
end
圖片.png

Step2.
查看每個標簽的數量

CountLabel = digitData.countEachLabel;

查看每個圖片的尺寸大小

img = readimage(digitData,1);
size(img)

ans =

28    28

把數據集分為 訓練數據集 和 測試數據集

trainingNumFiles = 750;
rng(1) % For reproducibility
[trainDigitData,testDigitData] = splitEachLabel(digitData, ...
                trainingNumFiles,'randomize');

Step3.
定義神經層,定義卷積網絡的結構

layers = [imageInputLayer([28 28 1])
          convolution2dLayer(5,20)
          reluLayer
          maxPooling2dLayer(2,'Stride',2)
          fullyConnectedLayer(10)
          softmaxLayer
          classificationLayer()];

Image Input Layer :圖像輸入層,就是圖像的尺寸,長通道數,因為是黑白圖像,通道數為1,如果為彩色的話,就是3
Convolutional Layer :卷積層,第一個參數是濾波器的尺寸,代表5*5,第二個參數是濾波器的個數
ReLU Layer:激活函數的個數
Max-Pooling Layer:最大池化層
Fully Connected Layer :全連接層,和待輸出的標簽個數一致
Softmax Layer:激活函數或者分類函數
Classification Layer:分類函數

Step4.
指定訓練參數

options = trainingOptions('sgdm','MaxEpochs',15, ...
    'InitialLearnRate',0.0001);

訓練數據

convnet = trainNetwork(trainDigitData,layers,options);

訓練結果如下


圖片.png

Step5.
分類測試數據集

YTest = classify(convnet,testDigitData);
TTest = testDigitData.Labels;

計算準確率

accuracy = sum(YTest == TTest)/numel(TTest);
圖片.png

總結:

  1. 學習了幾個通用函數的使用方式
    fullfile,用來構造文件路徑
    randperm,取隨機整數
    rng,初始化隨機數種子

  2. 關于深度學習用到的幾個函數
    imageDatastore,組建圖像處理數據集
    splitEachLabel,標簽分類

CNN卷積的一些設置函數:
imageInputLayer
convolution2dLayer
reluLayer
maxPooling2dLayer
fullyConnectedLayer
softmaxLayer
classificationLayer

trainingOptions
trainNetwork,訓練神經網絡
classify,對訓練好的神經網絡應用用于分類

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容