-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFOIT_ultra.py
276 lines (257 loc) · 11.4 KB
/
FOIT_ultra.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
'''
Author: your name
Date: 2020-08-05 08:55:19
LastEditTime: 2020-12-08 01:28:49
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: /FOIT/FOIT_ultra.py
'''
# sklearn
from sklearn.linear_model import LogisticRegression
from sklearn import svm
from sklearn.calibration import CalibratedClassifierCV
from sklearn.utils import shuffle
# package
import joblib
import numpy as np
import time
# utils
import utils
def FOIT(dataset_name='seed4', rho=1, clf_name='lr', threshold=0.6, with_balance=True, FOIT_type='cross-all'):
# time and accs
c0_acc = []
c0u_acc = []
foit_acc = []
time_c0 = []
time_c0u = []
time_foit = []
cd_count = 16 if dataset_name=='seed4' else 9 if dataset_name=='seed3' else print('Wrong dataset_name')
iteration_number = 3 if FOIT_type=='cross-subject' else 15
number_trial, number_label, labels = utils.get_number_of_label_n_trial(dataset_name)
data, label = utils.load_source_data(dataset_name=dataset_name, FOIT_type=FOIT_type)
for ite in range(iteration_number):
# The data of 15th sub is taken as the cd and ud data in cross-subject for each session (iteration_number=3)
# The data of 3rd session is taken as the cd and ud data in cross-session for each subject (iteration_number=14)
# For each iteration, the data of one sub from session 2 is taken as the cd and ud data in cross-all situation
# print("Iteration: {}".format(ite))
'''
Parameters
'''
session_id = -1
sub_id = -1
accs_number = -1
if FOIT_type == 'cross-subject':
session_id = ite
sub_id = 14
accs_number = 14
elif FOIT_type == 'cross-session':
session_id = 2
sub_id = ite
accs_number = 2
elif FOIT_type == 'cross-all':
session_id = 1
sub_id = ite
accs_number = 15
else:
print('Wrong FOIT type!')
'''
Data
'''
# data
cd_data, cd_label, ud_data, ud_label = utils.pick_one_data(dataset_name, session_id=session_id, cd_count=cd_count, sub_id=sub_id)
cd_data, cd_label = shuffle(cd_data, cd_label, random_state=0)
ud_data, ud_label = shuffle(ud_data, ud_label, random_state=0)
cd_data_min, cd_data_max = np.min(cd_data), np.max(cd_data)
cd_data = utils.normalization(cd_data)
ud_data = utils.normalization(ud_data)
if FOIT_type == 'cross-all':
data_ite, label_ite = data.copy(), label.copy()
for i in range(len(data)):
data_ite[i], label_ite[i] = shuffle(data_ite[i], label_ite[i], random_state=0)
# data_ite, label_ite = shuffle(data, label, random_state=0)
for i in range(len(data)):
data_ite[i] = utils.norm_with_range(data_ite[i], cd_data_min, cd_data_max)
# data_ite = utils.normalization(data_ite)
elif FOIT_type == 'cross-session':
data_ite, label_ite = data[ite], label[ite]
for i in range(len(data_ite)):
data_ite[i], label_ite[i] = shuffle(data_ite[i], label_ite[i], random_state=0)
# data_ite[i] = utils.normalization(data_ite[i])
data_ite[i] = utils.norm_with_range(data_ite[i], cd_data_min, cd_data_max)
# data_ite = utils.normalization(data_ite)
else:
data_ite, label_ite = data[ite], label[ite]
for i in range(len(data_ite)):
data_ite[i], label_ite[i] = shuffle(data_ite[i], label_ite[i], random_state=0)
# data_ite, label_ite = shuffle(data_ite, label_ite, random_state=0)
for i in range(len(data_ite)):
# data_ite[i] = utils.normalization(data_ite[i])
data_ite[i] = utils.norm_with_range(data_ite[i], cd_data_min, cd_data_max)
# data_ite = utils.normalization(data_ite)
# print(len(data_ite), len(label_ite[0]), len(label_ite[0][0]))
'''
A)
'''
if clf_name=='svm':
clf = svm.LinearSVC(max_iter=30000)
# clf = svm.SVC(probability=True, max_iter=10000)
elif clf_name=='lr':
clf = LogisticRegression(max_iter=30000)
else:
print('Unexcepted clf name, using LR as the baseline now.')
clf = svm.LinearSVC(max_iter=30000)
clf = CalibratedClassifierCV(clf, cv=5)
since = time.time()
clf.fit(cd_data, cd_label.squeeze())
time_baseline = time.time() - since
# print('Baseline training complete in {:.4f}'.format(time_baseline))
scoreA = utils.test(clf, ud_data, ud_label.squeeze())
# print('Baseline score: {}'.format(scoreA))
time_c0.append(time_baseline)
c0_acc.append(scoreA)
'''
B)
'''
accs = []
clf_sources = []
for i in range(accs_number):
if FOIT_type == 'cross-subject':
path = 'models/' + dataset_name + '/csu/sesn' + str(ite) + '/lr' + str(i) + '.m'
elif FOIT_type == 'cross-session':
path = 'models/' + dataset_name + '/csn/sub' + str(ite) + '/lr' + str(i) + '.m'
else:
path = 'models/' + dataset_name + '/csun/lr' + str(i) + '.m'
temp_clf = joblib.load(path)
clf_sources.append(temp_clf)
score = utils.test(temp_clf, ud_data, ud_label.squeeze())
accs.append(score)
if FOIT_type == 'cross-session':
pass
else:
accs = utils.normalization(accs)
# print('Accs of classifiers, normalized: {}'.format(accs))
'''
C)
'''
s_data_all, s_label_all = utils.stack_list(data_ite, label_ite)
s_data_all_predict_proba = clf.predict_proba(s_data_all)
s_label_all_proba = utils.get_one_hot(s_label_all.squeeze(), number_label)
confidence = np.zeros((s_label_all_proba.shape[0], 1))
for i in range(s_label_all_proba.shape[0]):
confidence[i] = s_label_all_proba[i].dot(s_data_all_predict_proba[i].T)
if with_balance:
## divide into 4 categories
data_ite_divided = []
label_ite_divided = []
conf_divided = []
for i in range(number_label):
data_ite_divided.append([])
label_ite_divided.append([])
conf_divided.append([])
for i in range(len(s_data_all)):
temp_label = s_label_all[i][0]
data_ite_divided[temp_label].append(s_data_all[i])
label_ite_divided[temp_label].append(s_label_all[i])
conf_divided[temp_label].append(confidence[i])
indices = []
for i in range(number_label):
indices.append(np.argsort(conf_divided[i], axis=0)[::-1])
topK_indices = [indices[i][:int(rho*len(cd_label)/4)] for i in range(len(indices))]
S_data = None
S_label = None
for i in range(len(topK_indices)):
for j in topK_indices[i]:
# temp_conf = conf_divided[i][j[0]]
one_data = data_ite_divided[i][j[0]]
one_label= label_ite_divided[i][j[0]]
if S_data is not None:
S_data = np.vstack((S_data, one_data))
S_label = np.vstack((S_label, one_label))
else:
S_data = one_data
S_label = one_label
elif with_balance is False:
indices = np.argsort(confidence, axis=0)[::-1]
topK_indices = indices[:int(rho*len(cd_label))]
S_data = None
S_label = None
for i in topK_indices:
one_data = s_data_all[i]
one_label = s_label_all[i]
if S_data is not None:
S_data = np.vstack((S_data, one_data))
S_label = np.vstack((S_label, one_label))
else:
S_data = one_data
S_label = one_label
else:
print('Unexcepted value of with balance!')
raise EnvironmentError
'''
D)
'''
L_S_data = cd_data.copy()
L_S_label = cd_label.copy()
L_S_data = np.vstack((L_S_data, S_data))
L_S_label = np.vstack((L_S_label, S_label))
L_S_data, L_S_label = shuffle(L_S_data, L_S_label, random_state=0)
# L_S_data = utils.normalization(L_S_data) # to decide
clf.fit(L_S_data, L_S_label.squeeze())
time_updated_baseline = time.time() - since
# print('Updated baseline training complete in {:.4f}s'.format(time_updated_baseline))
time_c0u.append(time_updated_baseline)
scoreD = utils.test(clf, ud_data, ud_label.squeeze())
# print('Updated model score: {}'.format(scoreD))
c0u_acc.append(scoreD)
'''
E)
'''
weight = (len(accs) + 1) / 2
proba_result_all = clf.predict_proba(ud_data) * weight
if FOIT_type == 'cross-session':
weight_for_clfs = utils.decide_which_clf_to_use(scoreD, accs)
for j in range(len(weight_for_clfs)):
proba_result_all += clf_sources[j].predict_proba(utils.normalization(ud_data)) * weight_for_clfs[j]
else:
for i in range(len(clf_sources)):
if accs[i] > threshold:
proba_result_all += clf_sources[i].predict_proba(ud_data) * accs[i]
corrects = np.sum(np.argmax(proba_result_all, axis=1) == ud_label.squeeze())
time_ensemble = time.time() - since
time_foit.append(time_ensemble)
scoreE = corrects / len(ud_label)
# print('Ensembled model score: {}'.format(scoreE))
foit_acc.append(scoreE)
# print(c0_acc)
# print(c0u_acc)
# print(foit_acc)
print('Mean acc and std of A: {} {}'.format(np.mean(c0_acc), np.std(c0_acc)))
print('Mean acc and std of D: {} {}'.format(np.mean(c0u_acc), np.std(c0u_acc)))
print('Mean acc and std of E: {} {}'.format(np.mean(foit_acc), np.std(foit_acc)))
print("Time cost for training baseline: ", np.mean(time_c0))
print("Time cost for training updated baseline: ", np.mean(time_c0u))
print("Time cost for training FOIT: ", np.mean(time_foit))
# print('A: ', c0_acc)
# print('D: ', c0u_acc)
# print('E: ', foit_acc)
# FOIT(dataset_name='seed4', rho=2, clf_name='lr', threshold=0.4, with_balance=True, FOIT_type='cross-session')
if __name__ == "__main__":
# FOIT(dataset_name='seed4', FOIT_type='cross-all')
# rho = 2
rho = 4
clf_name = 'svm'
threshold = 0.6
with_balance = False
FOIT_type_all = ['cross-all', 'cross-session', 'cross-subject']
dataset_name_all = ['seed4', 'seed3']
# FOIT_type_all = ['cross-all']
# dataset_name_all = ['seed4']
for dataset_name in dataset_name_all:
print('Dataset name: {}'.format(dataset_name))
print('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
for FOIT_type in FOIT_type_all:
print('FOIT type: {}'.format(FOIT_type))
FOIT(dataset_name=dataset_name, rho=rho, clf_name=clf_name, threshold=threshold, with_balance=with_balance, FOIT_type=FOIT_type)
# print()
# print('\n')
# FOIT(dataset_name='seed3', rho=4, clf_name='svm', threshold=0.6, with_balance=False, FOIT_type='cross-session')