3. Sieci neuronowe głębokie

3.4. Przykład użycia sieci ALEXNET w trybie transfer learning

Przykład zastosowania dotyczy rozpoznania 20 klas obrazów twarzy. Każda klasa jest reprezentowana przez 25 fotografii w różnym ujęciu i oświetleniu, przy czym klasy umieszczone są w oddzielnych folderach, a liczba tych folderów jest równa liczbie klas. Na tej podstawie program automatycznie przypisuje dane wejściowe do klasy zgodnej z numerem folderu.

% Przykład zastosowania ALEXNET w klasyfikacji obrazów%

wyn=[]; wynVal=[];
Pred_y=[]; Pred_yVal=[];
N_run=1 % liczba powtórzeń procesu
images = imageDatastore( 'C:\chmura\ALEX_obrazy20',... % Baza danych uczących
'IncludeSubfolders',true,...
'ReadFcn', @customreader, ...
'LabelSource','foldernames');
[uczImages,testImages] = splitEachLabel(images,0.8,'randomized');
for i=1:No_run
[trainingImages,validationImages] = splitEachLabel(uczImages,0.70+0.1*randn(1),'randomized');
numTrainImages = numel(trainingImages.Labels);
net = alexnet;
layersTransfer = net.Layers(1:end-6);
inputSize = net.Layers(1).InputSize;
% imageAugmenter = imageDataAugmenter( ...
% 'RandRotation',[-20,20], ...
% 'RandXTranslation',[-3 3], ...
% 'RandYTranslation',[-3 3])
% augimdsTrain = augmentedImageDatastore(inputSize(1:2),trainingImages,'DataAugmentation',imageAugmenter);
augimdsTrain = augmentedImageDatastore(inputSize(1:2),trainingImages);
augimdsTest = augmentedImageDatastore(inputSize(1:2),testImages);
layer = 'fc7';
featuresTrain = activations(net,augimdsTrain,layer,'OutputAs','rows');
featuresTest = activations(net,augimdsTest,layer,'OutputAs','rows');

numClasses = numel(categories(trainingImages.Labels));
layers = [
layersTransfer
fullyConnectedLayer(1000+35*i,'WeightLearnRateFactor',20,'BiasLearnRateFactor',20)
reluLayer()
dropoutLayer(0.5+0.033*randn(1))
fullyConnectedLayer(numClasses,'WeightLearnRateFactor',20,'BiasLearnRateFactor',20)
softmaxLayer
classificationLayer];
miniBatchSize = 10; % bylo 10
numIterationsPerEpoch = floor(numel(trainingImages.Labels)/miniBatchSize);
options = trainingOptions('sgdm',...
'MiniBatchSize',miniBatchSize,...
'MaxEpochs',10,...
'ExecutionEnvironment','gpu',...
'InitialLearnRate',1.3e-4,...
'Verbose',false,...
'ValidationPatience',10,...
'Plots','training-progress',...
'ValidationData',validationImages,...
'ValidationFrequency',numIterationsPerEpoch);
netTransfer = trainNetwork(trainingImages,layers,options);
predictedLabels = classify(netTransfer,testImages);
testLabels = testImages.Labels;
accuracy = (mean(predictedLabels == testLabels))*100
Pred_y=[Pred_y predictedLabels];
wyn=[wyn accuracy] %wyniki poszczególnych powtórzeń procesu uczenia i testowania
end

Program umożliwia wielokrotne uruchomienie uczenia ustawione wartością parametru No_run. W wyniku określona jest względna dokładność rozpoznania klas (wartość accuracy). Przy wielu próbach (No_run > 1) kolejne wyniki umieszczone są w tabeli wyn. Z tej tabeli można obliczyć średnią dokładność klasyfikatora w postaci mean(wyn).