-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlexNet.m
More file actions
47 lines (34 loc) · 1.95 KB
/
AlexNet.m
File metadata and controls
47 lines (34 loc) · 1.95 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
clc; clear all; close all; warning off;
DatasetPath = 'E:\SER\RAVDESS\'; % location to your dataset, subfolders should be labels of corresponding image categories
% reading images from the image database folder
images = imageDatastore(DatasetPath, 'IncludeSubfolders', true, 'LabelSource', 'foldernames');
numClasses = numel(categories(images.Labels));
net = alexnet;
layersTransfer = net.Layers(1:end-3); % preserving all layers except last three layers
layers = [layersTransfer
fullyConnectedLayer(numClasses, 'WeightLearnRateFactor', 20, 'BiasLearnRateFactor', 20)
softmaxLayer
classificationLayer];
[TrainImages, TestImages] = splitEachLabel(images, 0.8, 'randomized');
augTrainImages = augmentedImageDatastore([227 227 3], TrainImages);
augTestImages = augmentedImageDatastore([227 227 3], TestImages);
miniBatchSize = 8;
numObservationsTrain = numel(augTrainImages.Files); % Total Training Observations
numIterationsPerEpoch = floor(numObservationsTrain/miniBatchSize); % Validation Frequency
% training options
options = trainingOptions('adam', 'MiniBatchSize', miniBatchSize, 'MaxEpochs', 20, 'InitialLearnRate', 1e-4, 'Shuffle', 'every-epoch', 'ValidationData', augTestImages, 'ValidationFrequency', numIterationsPerEpoch, 'Verbose', false, 'Plots', 'training-progress', 'OutputNetwork', 'best-validation');
% training the AlexNet
netTransfer = trainNetwork(augTrainImages, layers, options);
% Classifying images
YPred = classify(netTransfer, augTestImages);
YValidation = TestImages.Labels;
accuracy = sum(YPred == YValidation)/numel(YValidation);
% plotting Confusion Matrix
figure;
plotconfusion(YValidation, YPred)
title('Confusion Matrix Using AlexNet')
% Optional: Save the training curve and the fine-tuned model
% currentfig = findall(groot,'Type','Figure'); savefig(currentfig,'AlexNet Learning Curve.fig') %
% Folder = 'D:\Trained Networks';
% File = 'AlexNet.mat';
% save(fullfile(Folder,File),'netTransfer');