-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalexNet_transferLearning.m
More file actions
65 lines (51 loc) · 1.73 KB
/
alexNet_transferLearning.m
File metadata and controls
65 lines (51 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
%% Mel Spectrograms Folder Location
clear
clc
dataPath = '/MelSpecs';
% Create an image datastore
imds = imageDatastore(dataPath, 'IncludeSubfolders', true, 'LabelSource', 'foldernames');
%% Label Counting
labelCount = countEachLabel(imds);
%% Get the size of the images
img = readimage(imds, 1);
size(img);
%% Training and Validation Sets
[imdsTrain, imdsValidation] = splitEachLabel(imds, 0.7, 'randomized');
%% Load Pretrained Network
net = alexnet;
%analyzeNetwork(net)
inputSize = net.Layers(1).InputSize;
%% Replace Final Layers
layersTransfer = net.Layers(1:end-3);
numClasses = numel(categories(imdsTrain.Labels));
layers = [
layersTransfer
dropoutLayer
fullyConnectedLayer(numClasses, 'WeightLearnRateFactor', 20, 'BiasLearnRateFactor', 20)
softmaxLayer
classificationLayer];
pixelRange = [-30 30];
imageAugmenter = imageDataAugmenter(...
'RandXReflection', true, ...
'RandXTranslation', pixelRange, ...
'RandYTranslation', pixelRange);
augImdsTrain = augmentedImageDatastore(inputSize(1:2), imdsTrain, ...
'DataAugmentation', imageAugmenter);
augImdsValidation = augmentedImageDatastore(inputSize(1:2), imdsValidation);
%% Training Options
options = trainingOptions('adam', ...
'MaxEpochs',5, ...
'InitialLearnRate',1e-4, ...
'LearnRateDropFactor', 0.65, ...
'LearnRateDropPeriod', 5, ...
'Shuffle','every-epoch', ...
'ValidationData',augImdsValidation, ...
'ValidationFrequency',50, ...
'Verbose',true, ...
'Plots','training-progress');
netTransfer = trainNetwork(augImdsTrain, layers, options);
%% Classify Validation Images
[YPred, scores] = classify(netTransfer, augImdsValidation);
YValidation = imdsValidation.Labels;
accuracy = mean(YPred == YValidation);
disp(accuracy);