-
Notifications
You must be signed in to change notification settings - Fork 26
/
JiangYin_2015.py
160 lines (125 loc) · 5.02 KB
/
JiangYin_2015.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
import sys
import custom_model as cm
import numpy as np
import random
from sklearn.metrics.classification import accuracy_score, recall_score, f1_score
import scipy.stats as st
from keras.layers import Input, Dense, Dropout, Conv2D, Flatten, MaxPooling2D, Activation, AveragePooling2D
from keras.callbacks import ReduceLROnPlateau, EarlyStopping, Callback
from keras.models import Model
from keras import backend as K
K.set_image_data_format('channels_first')
def custom_model1(inp, n_classes):
#Original architecture
H = Conv2D(filters=5, kernel_size=(5, 5))(inp)
H = Activation('relu')(H)
H = AveragePooling2D(pool_size=(4, 4))(H)
H = Conv2D(filters=10, kernel_size=(5, 5))(H)
H = Activation('relu')(H)
H = AveragePooling2D(pool_size=(2, 2))(H)
H = Flatten()(H)
H = Dense(120)(H)
H = Activation('relu')(H)
H = Dense(n_classes)(H)
H = Activation('softmax')(H)
model = Model([inp], H)
return model
def custom_model2(inp, n_classes):
# Adapted architecture
H = Conv2D(filters=5, kernel_size=(4, 4), padding='same')(inp)
H = Activation('relu')(H)
H = AveragePooling2D(pool_size=(2, 2))(H)
H = Conv2D(filters=10, kernel_size=(4, 4), padding='same')(H)
H = Activation('relu')(H)
H = AveragePooling2D(pool_size=(2, 2))(H)
H = Flatten()(H)
H = Dense(120)(H)
H = Activation('relu')(H)
H = Dense(n_classes)(H)
H = Activation('softmax')(H)
model = Model([inp], H)
return model
def check_neighboring(sis, elem1, elem2):
if(len(sis)==1):
return False
for i in range(0, len(sis)-1):
if sis[i] == elem1 and sis[i+1] == elem2:
return True
if sis[i] == elem2 and sis[i + 1] == elem1:
return True
return False
def activity_image(raw_signals):
seq = np.arange(0, raw_signals.shape[3], 1)
sis = []
n_sis = 1
i = 0
j = i+1
sis.append(i)
while i!=j:
if j==len(seq):
j=0
elif check_neighboring(sis, i, j) == False:
sis.append(j)
i = j
j = j+1
else:
j = j + 1
output = []
for sample in raw_signals:
signal_image = sample[0]
signal_image = signal_image[:, sis]
signal_image = np.transpose(signal_image)
fshift = np.fft.fftshift(signal_image)
fshift = np.transpose(fshift)
# import cv2
# cv2.imshow('tete', magnitude_spectrum)
# cv2.waitKey(1000)
output.append([fshift])
output = np.array(output)
return output
if __name__ == '__main__':
#Paper: Human Activity Recognition using Wearable Sensors by Deep Convolutional Neural Networks
np.random.seed(12227)
if (len(sys.argv) > 1):
data_input_file = sys.argv[1]
else:
data_input_file = 'data/LOSO/MHEALTH.npz'
tmp = np.load(data_input_file)
X = tmp['X']
y = tmp['y']
folds = tmp['folds']
n_class = y.shape[1]
X = activity_image(X)
_, _, img_rows, img_cols = X.shape
avg_acc = []
avg_recall = []
avg_f1 = []
print('Jiang and Yin 2015 {}'.format(data_input_file))
for i in range(0, len(folds)):
train_idx = folds[i][0]
test_idx = folds[i][1]
X_train = X[train_idx]
X_test = X[test_idx]
inp = Input((1, img_rows, img_cols))
#model = custom_model1(inp, n_classes=n_class)
model = custom_model2(inp, n_classes=n_class)
model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='Adadelta')
model.fit(X_train, y[train_idx], batch_size=cm.bs, epochs=cm.n_ep,
verbose=0, callbacks=[cm.custom_stopping(value=cm.loss, verbose=1)], validation_data=(X_train, y[train_idx]))
y_pred = model.predict(X_test)
y_pred = np.argmax(y_pred, axis=1)
y_true = np.argmax(y[test_idx], axis=1)
acc_fold = accuracy_score(y_true, y_pred)
avg_acc.append(acc_fold)
recall_fold = recall_score(y_true, y_pred, average='macro')
avg_recall.append(recall_fold)
f1_fold = f1_score(y_true, y_pred, average='macro')
avg_f1.append(f1_fold)
print('Accuracy[{:.4f}] Recall[{:.4f}] F1[{:.4f}] at fold[{}]'.format(acc_fold, recall_fold, f1_fold, i))
print('______________________________________________________')
ic_acc = st.t.interval(0.9, len(avg_acc) - 1, loc=np.mean(avg_acc), scale=st.sem(avg_acc))
ic_recall = st.t.interval(0.9, len(avg_recall) - 1, loc=np.mean(avg_recall), scale=st.sem(avg_recall))
ic_f1 = st.t.interval(0.9, len(avg_f1) - 1, loc=np.mean(avg_f1), scale=st.sem(avg_f1))
print('Mean Accuracy[{:.4f}] IC [{:.4f}, {:.4f}]'.format(np.mean(avg_acc), ic_acc[0], ic_acc[1]))
print('Mean Recall[{:.4f}] IC [{:.4f}, {:.4f}]'.format(np.mean(avg_recall), ic_recall[0], ic_recall[1]))
print('Mean F1[{:.4f}] IC [{:.4f}, {:.4f}]'.format(np.mean(avg_f1), ic_f1[0], ic_f1[1]))