-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
103 lines (87 loc) · 3.24 KB
/
model.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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import tensorflow as tf
from sklearn.preprocessing import StandardScaler
from keras.models import Sequential,load_model
from keras.layers import Dense,Dropout,Activation,Flatten
from keras.optimizers import Adam
from keras.utils import to_categorical
from keras.callbacks import ModelCheckpoint
import time
import joblib
train = pd.read_csv('train.csv')
test = pd.read_csv('test.csv')
print("Shape of Train data: ",train.shape)
print("Shape of Test data: ",test.shape)
## dividing the data into dependent and independent data
xtrain = train.drop(['subject', 'Activity', 'ActivityName'], axis=1)
ytrain = train['ActivityName']
xtest = test.drop(['subject', 'Activity', 'ActivityName'], axis=1)
ytest = test['ActivityName']
print("Done....")
sc = StandardScaler()
xtrain = sc.fit_transform(xtrain)
xtest = sc.transform(xtest)
print('xtrain and ytrain : ({},{})'.format(xtrain.shape, ytrain.shape))
print('xtest and ytest : ({},{})'.format(xtest.shape, ytest.shape))
ytrain = ytrain.map({'LAYING':0, 'SITTING':1,'STANDING':2,'WALKING':3,'WALKING_DOWNSTAIRS':4,'WALKING_UPSTAIRS':5})
ytest = ytest.map({'LAYING':0, 'SITTING':1,'STANDING':2,'WALKING':3,'WALKING_DOWNSTAIRS':4,'WALKING_UPSTAIRS':5})
ytrain = to_categorical(ytrain)
ytest = to_categorical(ytest)
print(ytest.shape)
print(ytrain.shape)
model = Sequential(name="HAR-Net")
model.add(Dense(256,input_shape=(561,)))
model.add(Activation('relu'))
model.add(Dropout(0.25))
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.25))
model.add(Dense(256))
model.add(Activation('relu'))
model.add(Dropout(0.25))
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.25))
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.25))
model.add(Dense(6,activation="softmax"))
checkpointer = ModelCheckpoint('HAR-Net.h5', save_best_only=True,monitor='val_accuracy',mode='auto')
opt = tf.keras.optimizers.Adam(learning_rate=0.01,decay=0.01)
print("Model architecture is done....")
model.compile(loss = "categorical_crossentropy", optimizer=opt,metrics=["accuracy"])
print("Model compiled....")
print("Training is starting....")
start = time.time()
hist = model.fit(xtrain,ytrain,batch_size=16,epochs=200,validation_data=(xtest, ytest),callbacks=[checkpointer])
print("Model training is over....")
print("Total Time taken: ",time.time()-start)
print("Model saved....")
# plotting the figures
print("Plotting the figures....")
plt.figure(figsize=(15,10))
plt.plot(hist.history['accuracy'],c='b',label='train')
plt.plot(hist.history['val_accuracy'],c='r',label='validation')
plt.title("Model Accuracy vs Epochs")
plt.xlabel("EPOCHS")
plt.ylabel("ACCURACY")
plt.legend(loc='lower right')
plt.savefig('./img/accuracy.png')
plt.figure(figsize=(15,10))
plt.plot(hist.history['loss'],c='orange',label='train')
plt.plot(hist.history['val_loss'],c='g',label='validation')
plt.title("Model Loss vs Epochs")
plt.xlabel("EPOCHS")
plt.ylabel("LOSS")
plt.legend(loc='upper right')
plt.savefig('./img/loss.png')
print("Figures saved in the disk....")
model=load_model("HAR-Net.h5")
# testing the model
print("Testing the model....")
print("The result obtained is...\n")
model.evaluate(xtest,ytest)
joblib.dump(sc,'scaler.pkl')