-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRNN.m
143 lines (128 loc) · 5.17 KB
/
RNN.m
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
%% ------------------------------------------------------------------------
% GAIT RECOGNITION BASED ON IMU DATA AND ML ALGORITHM
% Albi Matteo, Cardone Andrea, Oselin Pierfrancesco
%
% Required packages:
% Parallel Computing Toolbox
% Neural Network Toolbox
% Signal Toolbox
% Statistics Toolbox
% -------------------------------------------------------------------------
clear ;
close all;
clc
addpath("include");
addpath("output");
load("results.mat");
%% DATA IMPORTING
try
file01 = readtable('data/record_walk_7-12-21_caviglia/personaA4kmh.csv', "VariableNamingRule","preserve");
file02 = readtable('data/record_walk_7-12-21_caviglia/personaB4kmh.csv', "VariableNamingRule","preserve");
file03 = readtable('data/record_walk_7-12-21_caviglia/personaC4kmh.csv', "VariableNamingRule","preserve");
file04 = readtable('data/record_walk_7-12-21_caviglia/personaD4kmh.csv', "VariableNamingRule","preserve");
file05 = readtable('data/record_walk_7-12-21_caviglia/personaE4kmh.csv', "VariableNamingRule","preserve");
file06 = readtable('data/record_walk_7-12-21_caviglia/personaA6kmh.csv', "VariableNamingRule","preserve");
file07 = readtable('data/record_walk_7-12-21_caviglia/personaB6kmh.csv', "VariableNamingRule","preserve");
file08 = readtable('data/record_walk_7-12-21_caviglia/personaC5_8kmh.csv', "VariableNamingRule","preserve");
file09 = readtable('data/record_walk_7-12-21_caviglia/personaD6kmh.csv', "VariableNamingRule","preserve");
file10 = readtable('data/record_walk_7-12-21_caviglia/personaE6kmh.csv', "VariableNamingRule","preserve");
%adding cutted lab data
file11 = readtable('data/record_lab_15-12-21/IMU1_1.csv', "VariableNamingRule","preserve");
file12 = readtable('data/record_lab_15-12-21/IMU1_2.csv', "VariableNamingRule","preserve");
file13 = readtable('data/record_lab_15-12-21/IMU2_1.csv', "VariableNamingRule","preserve");
file14 = readtable('data/record_lab_15-12-21/IMU3_1.csv', "VariableNamingRule","preserve");
file15 = readtable('data/record_lab_15-12-21_afternoon/IMU4_1.csv', "VariableNamingRule","preserve");
disp("Data successfully imported");
catch ME
if strcmp(ME.identifier, 'MATLAB:textio:textio:FileNotFound')
disp("ERROR: some data cannot be found");
return;
end
end
%define usage of each file
train = {file01, file02, file03, file04, file06, file07, file08, file09, file11, file12, file13, file14, file15};
test = {file05, file10};
useful_data = [ 'AccX (g)', 'AccY (g)', 'AccZ (g)', ...
'GyroX (deg/s)', 'GyroY (deg/s)', 'GyroZ (deg/s)', ...
'EulerX (deg)', 'EulerY (deg)', 'EulerZ (deg)', ...
'LinAccX (g)', 'LinAccY (g)', 'LinAccZ (g)', ...
'ID'];
%% Labeling and preparing data to train and test the network
% see dataPreprocessing.m
[XTrain,YTrain] = dataPreprocessing(train,useful_data);
[XTest,YTest] = dataPreprocessing(test,useful_data);
%% Setting up the RNN network
%% -General settings
numFeatures = height(XTrain{1});
numHiddenUnits = 100;
numClasses = 4;
layers = [ ...
% input layer
sequenceInputLayer(numFeatures)
% recurrent layers:
% numHiddenUnits indicates number of hidden layers
% OutputMode = 'sequence' indicates that the net must classify each
% time instant of the streaming
lstmLayer(numHiddenUnits,'OutputMode','sequence')
% decision layer
fullyConnectedLayer(numClasses)
softmaxLayer
% output layer
classificationLayer];
%% -Setting the options for training
miniBatchSize = 1000;
maxEpochs = 250;
gradientThreshold = 2;
executionEnvironment = 'cpu';
options = trainingOptions(...
'adam', ...
'MiniBatchSize',miniBatchSize, ...
'MaxEpochs',maxEpochs, ...
'GradientThreshold', gradientThreshold, ...
'Verbose', 0, ...
'Plots','none', ...
'ExecutionEnvironment', executionEnvironment);
%train or load
% net = trainNetwork(XTrain,YTrain,layers,options);
net = results{1,1,1,1}.net;
%% Plot of the testing data
% figure
% plot(testX{1}')
% xlabel("Time Step")
% legend("Feature " + (1:numFeatures))
% title("Test Data")
%% Prediction of the classes (GAIT PHASES) and ACCURACY
% phase accuracy
disp("Accuracy per phase:")
correct = zeros(1,4); % correct predictions
totPhases = zeros(1,4); % tot number of predictions
% for each test case
for i = 1:length(XTest)
% evaluate network
YPred = classify(net,XTest{i});
% for each phase
for j = 1:4
% update number of time instant with that phase
totPhases(j) = totPhases(j) + sum(YTest{i} == categorical(j));
% update number of correct predictions for that phase
correct(j) = correct(j) + sum(YPred(YTest{i} == categorical(j)) == categorical(j));
end
end
% display ratio result
disp(correct./totPhases);
% test accuracy
disp("Accuracy per test set")
acc = zeros(1,length(XTest));
% for each test
for i = 1:length(XTest)
% evaluate network
YPred = classify(net,XTest{i});
% compute accuracy for the used test
% correct predictions / tot number of predictions
acc(i) = sum(YPred == YTest{i})/numel(YTest{i});
end
% display ratio result
disp(acc);
%% SIMULATE THE DATASTREAM
% see simulateStream.m
disp(simulateStream(net, file10, 0, 1));