-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccelCNN.py
131 lines (107 loc) · 3.88 KB
/
accelCNN.py
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
'''
https://kgptalkie.com/human-activity-recognition-using-accelerometer-data/
https://github.com/laxmimerit/Human-Activity-Recognition-Using-Accelerometer-Data-and-CNN
'''
import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Flatten, Dense, Dropout, BatchNormalization
from tensorflow.keras.layers import Conv2D, MaxPool2D
from tensorflow.keras.optimizers import Adam
print(tf.__version__)
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import csv
from os import listdir
import scipy.stats as stats
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, LabelEncoder
def load_datasets():
subjects = list()
for folder in range(1,4):
for filename in listdir('./trainingdata/data{0}'.format(str(folder))):
if filename.endswith("csv"):
values = csv.reader(open('./trainingdata/data{0}/'.format(str(folder)) + filename, "r"), delimiter = ",") # opens training data
processedlist = []
for row in values:
temp = [row[0],row[1],row[2],row[3],row[4]]
processedlist.append(temp)
subjects.append(processedlist)
return subjects
def plot_subject(subject):
num = []
x = []
y = []
z = []
for row in subject:
num.append(float(row[0]))
x.append(float(row[1]))
y.append(float(row[2]))
z.append(float(row[3]))
fig, axis = plt.subplots(3)
axis[0].plot(num, x)
axis[1].plot(num, y)
axis[2].plot(num, z)
plt.show()
def make_pandas(dataset):
columns = ["time", "x", "y", "z", "label"]
datasets = []
for i in range(0,len(dataset)):
datasets.append(pd.DataFrame(data = dataset[i], columns = columns))
return datasets
def get_frames(df):
frames = []
labels = []
for dataset in df:
frame = []
for i in range(0,len(dataset)):
x = dataset['x'][i]
y = dataset['y'][i]
z = dataset['z'][i]
frame.append([[int(x)], [int(y)], [int(z)]])
frames.append(frame)
#print(dataset["label"])
labels.append(int(dataset['label'][0]))
frames = np.asarray(frames)
labels = np.asarray(labels)
#print(labels[:10])
return frames, labels
subjects = load_datasets()
datasets = make_pandas(subjects)
X, Y = get_frames(datasets)
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=4)
X_train = X_train.reshape(len(X_train), 1000, 3, 1)
X_test = X_test.reshape(len(X_test), 1000, 3, 1)
print(X_train[0].shape)
model = Sequential()
model.add(Conv2D(16, (2, 2), activation = 'relu', input_shape = X_train[0].shape))
model.add(Dropout(0.1))
model.add(Conv2D(32, (2, 2), activation='relu'))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(64, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(6, activation='softmax'))
epochnum = 60
model.compile(optimizer=Adam(learning_rate = 0.001), loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])
history = model.fit(X_train, y_train, epochs = epochnum, validation_data= (X_test, y_test), verbose=1)
def plot_learningCurve(history, epochs):
# Plot training & validation accuracy values
epoch_range = range(1, epochs+1)
plt.plot(epoch_range, history.history['accuracy'])
plt.plot(epoch_range, history.history['val_accuracy'])
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Val'], loc='upper left')
plt.show()
# Plot training & validation loss values
plt.plot(epoch_range, history.history['loss'])
plt.plot(epoch_range, history.history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Val'], loc='upper left')
plt.show()
plot_learningCurve(history, epochnum)
model.save("Model", save_format = "tf")