forked from parisafm/CSI-HAR-Dataset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCNN.py
197 lines (149 loc) · 6.8 KB
/
CNN.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
from keras.models import Sequential
from keras.layers import Convolution2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
import keras.metrics as metrics
from keras.layers import AveragePooling2D
from keras.preprocessing.image import ImageDataGenerator
from keras.layers import BatchNormalization
from keras.layers import Dropout
from sklearn.metrics import classification_report
from keras.optimizers import Adam
import numpy as np
import tensorflow as tf
from keras import regularizers
from keras.layers import LeakyReLU
# path_drive = 'P://fm//py//images'
# Initialising the CNN
Classifier = Sequential()
# Step 1 - Convolution
Classifier.add(Convolution2D(32, 3, 3, input_shape = (64,64,3)))
Classifier.add(LeakyReLU(alpha=0.1))
# Classifier.add(BatchNormalization()) #no need
# Step 2 - Pooling
Classifier.add(MaxPooling2D(pool_size = (2,2)))
Classifier.add(Dropout(0.25))
# Classifier.add(Dense(64, activation= 'relu'))
# # second layer
Classifier.add(Convolution2D(64, 3, 3))
Classifier.add(LeakyReLU(alpha=0.1))
# Classifier.add(BatchNormalization())
Classifier.add(MaxPooling2D(pool_size = (2,2)))
# Classifier.add(Dropout(0.5))
# Classifier.add(Dense(64,input_dim = 64,
# kernel_regularizer = regularizers.l2(0.00001),
# activity_regularizer = regularizers.l1(0.00001)))
# #third layer
# Classifier.add(Convolution2D(32, 3, 3, activation = 'relu'))
# #Classifier.add(BatchNormalization())
# Classifier.add(MaxPooling2D(pool_size = (2,2)))
# Classifier.add(Dropout(0.25))
#forth layer
# Classifier.add(Convolution2D(32, 3, 3, activation = 'relu'))
# #Classifier.add(BatchNormalization())
# Classifier.add(MaxPooling2D(pool_size = (2,2)))
# Step 3 - Flattening
Classifier.add(Flatten())
# Step 4 - Full connection
#output_dim = 128
Classifier.add(Dense( 128, activation = 'linear'))
Classifier.add(Dropout(0.1,name='Dropout_Regularization')) #dropout=1 for fixing last epoch fluctuation
Classifier.add(Dense( 7, activation = 'softmax'))
# Compiling the CNN
opt = Adam(learning_rate=0.0001)
Classifier.compile(optimizer = opt, loss = 'categorical_crossentropy',
metrics=['accuracy'])
# Part 2 - Fitting the CNN to the images
train_datagen = ImageDataGenerator( featurewise_center=False)
test_datagen = ImageDataGenerator( featurewise_center=False)
train_set = train_datagen.flow_from_directory('.\\CSI-HAR-Dataset\\Train\\',
target_size=(64,64),
color_mode="rgb",
classes=['bend','fall','lie down', 'run', 'sitdown','standup','walk'],
class_mode="categorical",
batch_size=32,
shuffle=True,
seed=None,
save_to_dir=None,
save_prefix="",
save_format="jpg",
follow_links=False,
subset=None,
interpolation="nearest")
test_set = test_datagen.flow_from_directory( '.\\CSI-HAR-Dataset\\Test\\' ,
target_size=(64,64),
color_mode="rgb",
classes=['bend','fall','lie down', 'run', 'sitdown','standup','walk'],
class_mode="categorical",
batch_size=32,
shuffle=False,
seed=None,
save_to_dir=None,
save_prefix="",
save_format="jpg",
follow_links=False,
subset=None,
interpolation="nearest")
print(train_set.filenames, test_set.filenames, sep = '\n')
print(len(train_set), len(test_set))
from keras.callbacks import ModelCheckpoint
checkpoint = ModelCheckpoint("model_weights.h5",monitor='val_accuracy',verbose=1,save_best_only=True,mode='max')
callbacks_list=[checkpoint]
history = Classifier.fit(train_set,
epochs = 150,
validation_data = test_set,
callbacks=[checkpoint])
#Save and serialize model structure to JSON
Classifier_json = Classifier.to_json()
with open("Classifier.json","w") as json_file:
json_file.write(Classifier_json)
# plot the evolution of Loss and Acuracy
import matplotlib.pyplot as plt
plt.figure(figsize=(20, 10))
plt.subplot(1, 2, 1)
plt.suptitle('Optimizer : Adam', fontsize=10)
plt.ylabel('Loss', fontsize=16)
plt.plot(history.history['loss'], label='Training Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.legend(loc='upper right')
plt.subplot(1,2,2)
plt.ylabel('Accuracy', fontsize=16)
plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.legend(loc='lower right')
plt.show()
# show the confusion matrix of our predictions
# compute predictions
predictions = Classifier.predict_generator(generator=test_set)
y_pred = [np.argmax(probas) for probas in predictions]
y_test = test_set.classes
class_names = test_set.class_indices.keys()
from sklearn.metrics import confusion_matrix
import itertools
def plot_confusion_matrix(cm, classes, title='Confusion matrix', cmap=plt.cm.Blues):
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
plt.figure(figsize=(10,10))
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
fmt = '.2f'
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, format(cm[i,j], fmt),
horizontalalignment="center",
color="white" if cm[i,j] > thresh else "black")
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.tight_layout()
# compute confusion matrix
cnf_matrix = confusion_matrix(y_test, y_pred)
np.set_printoptions(precision=2)
# plot normalized confusion matrix
plt.figure()
plot_confusion_matrix(cnf_matrix, classes=class_names, title='Normalized confusion matrix')
plt.show()
Classifier.summary()