【圖像識別】基于Matlab的遷移學(xué)習(xí)的圖像分類案例
Matlab編程Deep learning的基礎(chǔ)知識
一、什么是遷移學(xué)習(xí)?

加載數(shù)據(jù)
unzip('MerchData.zip');imds = imageDatastore('MerchData', ...'IncludeSubfolders',true, ...'LabelSource','foldernames');
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7,'randomized');這個非常小的數(shù)據(jù)集現(xiàn)在包含 55 個訓(xùn)練圖像和 20 個驗證圖像。
numTrainImages = numel(imdsTrain.Labels);idx = randperm(numTrainImages,16);figurefor i = 1:16subplot(4,4,i)I = readimage(imdsTrain,idx(i));imshow(I)end

加載預(yù)訓(xùn)練網(wǎng)絡(luò)
net = alexnet;使用 analyzeNetwork 可以交互可視方式呈現(xiàn)網(wǎng)絡(luò)架構(gòu)以及有關(guān)網(wǎng)絡(luò)層的詳細信息。
analyzeNetwork(net)
第一層(圖像輸入層)需要大小為 227×227×3 的輸入圖像
其中 3 是顏色通道數(shù)
inputSize = 1×3227 227 3
三、網(wǎng)絡(luò)的訓(xùn)練
替換最終層
layersTransfer = net.Layers(1:end-3);numClasses = numel(categories(imdsTrain.Labels))numClasses = 5
layers = [layersTransferfullyConnectedLayer(numClasses,'WeightLearnRateFactor',20,'BiasLearnRateFactor',20)softmaxLayerclassificationLayer];
訓(xùn)練網(wǎng)絡(luò)
pixelRange = [-30 30];imageAugmenter = imageDataAugmenter( ...'RandXReflection',true, ...'RandXTranslation',pixelRange, ...'RandYTranslation',pixelRange);augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain, ...'DataAugmentation',imageAugmenter);

對驗證圖像進行分類
使用經(jīng)過微調(diào)的網(wǎng)絡(luò)對驗證圖像進行分類
[YPred,scores] = classify(netTransfer,augimdsValidation);顯示四個示例驗證圖像及預(yù)測的標(biāo)簽。
idx = randperm(numel(imdsValidation.Files),4);figurefor i = 1:4subplot(2,2,i)I = readimage(imdsValidation,idx(i));imshow(I)label = YPred(idx(i));title(string(label));end

計算針對驗證集的分類準(zhǔn)確度。準(zhǔn)確度是網(wǎng)絡(luò)預(yù)測正確的標(biāo)簽的比例
YValidation = imdsValidation.Labels;accuracy = mean(YPred == YValidation)
accuracy = 1
今天你學(xué)廢了嗎???
來源:渝西圖像練習(xí)生
End 
聲明:部分內(nèi)容來源于網(wǎng)絡(luò),僅供讀者學(xué)術(shù)交流之目的,文章版權(quán)歸原作者所有。如有不妥,請聯(lián)系刪除。
評論
圖片
表情
