-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.py
397 lines (321 loc) · 15.7 KB
/
utils.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#!/usr/bin/env python
# coding: utf-8
import numpy as np
from sklearn.model_selection import StratifiedKFold
import matplotlib.pyplot as plt
import pandas as pd
import os
import hickle as hkl
import tensorflow as tf
import seaborn as sns
import logging
from tensorflow.python.client import device_lib
def get_available_gpus():
local_device_protos = device_lib.list_local_devices()
return [x.name for x in local_device_protos if x.device_type == 'GPU']
def get_available_cpus():
local_device_protos = device_lib.list_local_devices()
return [x.name for x in local_device_protos if x.device_type == 'CPU']
class dataHolder:
clientDataTrain = []
clientLabelTrain = []
clientDataTest = []
clientLabelTest = []
centralTrainData = []
centralTrainLabel = []
centralTestData = []
centralTestLabel = []
clientOrientationTrain = []
clientOrientationTest = []
orientationsNames = None
activityLabels = []
clientCount = None
def returnClientByDataset(dataSetName):
if(dataSetName=='UCI' or dataSetName == 'UCI_ORIGINAL'):
return 5
elif(dataSetName == "RealWorld" ):
return 15
elif(dataSetName == "MotionSense"):
return 24
elif(dataSetName == 'SHL'):
return 9
elif(dataSetName == "HHAR"):
return 51
else:
raise ValueError('Unknown dataset')
def load_file(filepath):
dataframe = pd.read_csv(filepath, header=None)
return dataframe.values
def load_group(filenames, prefix=''):
loaded = list()
for name in filenames:
data = load_file(prefix + name)
loaded.append(data)
loaded = np.dstack(loaded)
return loaded
def load_dataset(group,mainDir,prefix=''):
filepath = mainDir + 'datasetStandardized/'+prefix + '/' + group + '/'
filenames = list()
filenames += ['AccX'+prefix+'.csv', 'AccY' +
prefix+'.csv', 'AccZ'+prefix+'.csv']
filenames += ['GyroX'+prefix+'.csv', 'GyroY' +
prefix+'.csv', 'GyroZ'+prefix+'.csv']
X = load_group(filenames, filepath)
y = load_file(mainDir + 'datasetStandardized/'+prefix +
'/' + group + '/Label'+prefix+'.csv')
return X, y
def projectTSNE(fileName,filepath,ACTIVITY_LABEL,labels_argmax,tsne_projections,unique_labels):
plt.figure(figsize=(16,16))
# plt.title('HART Embeddings T-SNE')
graph = sns.scatterplot(
x=tsne_projections[:,0], y=tsne_projections[:,1],
hue=labels_argmax,
palette=sns.color_palette(n_colors = len(unique_labels)),
s=50,
alpha=1.0,
rasterized=True
)
legend = graph.legend_
for j, label in enumerate(unique_labels):
legend.get_texts()[j].set_text(ACTIVITY_LABEL[int(label)])
plt.tick_params(
axis='both',
which='both',
bottom=False,
top=False,
labelleft=False,
labelbottom=False)
ax = plt.gca()
ax.axes.xaxis.set_visible(False)
ax.axes.yaxis.set_visible(False)
plt.savefig(filepath+fileName+".svg", bbox_inches="tight", format="svg")
plt.show()
def projectTSNEWithPosition(dataSetName,fileName,filepath,ACTIVITY_LABEL,labels_argmax,orientationsNames,clientOrientationTest,tsne_projections,unique_labels):
classData = [ACTIVITY_LABEL[i] for i in labels_argmax]
orientationData = [orientationsNames[i] for i in np.hstack((clientOrientationTest))]
if(dataSetName == 'RealWorld'):
orientationName = 'Position'
else:
orientationName = 'Device'
pandaData = {'col1': tsne_projections[:,0], 'col2': tsne_projections[:,1],'Classes':classData, orientationName :orientationData}
pandaDataFrame = pd.DataFrame(data=pandaData)
plt.figure(figsize=(16,16))
# plt.title('HART Embeddings T-SNE')
sns.scatterplot(data=pandaDataFrame, x="col1", y="col2", hue="Classes", style=orientationName,
palette=sns.color_palette(n_colors = len(unique_labels)),
s=50, alpha=1.0,rasterized=True,)
plt.tick_params(
axis='both',
which='both',
bottom=False,
top=False,
labelleft=False,
labelbottom=False)
ax = plt.gca()
ax.axes.xaxis.set_visible(False)
ax.axes.yaxis.set_visible(False)
plt.savefig(filepath+fileName+".png", bbox_inches="tight")
plt.show()
def create_segments_and_labels_Mobiact(df, time_steps, step, label_name = "LabelsEncoded", n_features= 6):
segments = []
labels = []
for i in range(0, len(df) - time_steps, step):
acc_x = df['acc_x'].values[i: i + time_steps]
acc_y = df['acc_y'].values[i: i + time_steps]
acc_z = df['acc_z'].values[i: i + time_steps]
gyro_x = df['gyro_x'].values[i: i + time_steps]
gyro_y = df['gyro_y'].values[i: i + time_steps]
gyro_z = df['gyro_z'].values[i: i + time_steps]
# Retrieve the most often used label in this segment
label = scipy.stats.mode(df[label_name][i: i + time_steps])[0][0]
segments.append([acc_x,acc_y,acc_z,gyro_x,gyro_y,gyro_z])
labels.append(label)
# Bring the segments into a better shape
reshaped_segments = np.asarray(segments, dtype=np.float32).reshape(-1, time_steps, n_features)
labels = np.asarray(labels)
return reshaped_segments, labels
def loadDataset(dataSetName, clientCount, dataConfig, randomSeed, mainDir, StratifiedSplit = True):
# loading datasets
clientDataTrain = []
clientLabelTrain = []
clientDataTest = []
clientLabelTest = []
centralTrainData = []
centralTrainLabel = []
centralTestData = []
centralTestLabel = []
clientOrientationTrain = []
clientOrientationTest = []
orientationsNames = None
if(dataSetName == "UCI"):
centralTrainData = hkl.load(mainDir + 'datasetStandardized/'+str(dataSetName)+'/trainX.hkl')
centralTestData = hkl.load(mainDir + 'datasetStandardized/'+str(dataSetName)+'/testX.hkl')
centralTrainLabel = hkl.load(mainDir + 'datasetStandardized/'+str(dataSetName)+'/trainY.hkl')
centralTestLabel = hkl.load(mainDir + 'datasetStandardized/'+str(dataSetName)+'/testY.hkl')
elif(dataSetName == "SHL"):
clientData = hkl.load(mainDir + 'datasetStandardized/'+str(dataSetName)+'/clientsData.hkl')
clientLabel = hkl.load(mainDir + 'datasetStandardized/'+str(dataSetName)+'/clientsLabel.hkl')
clientCount = clientData.shape[0]
for i in range(0,clientCount):
skf = StratifiedKFold(n_splits=5, shuffle=False)
skf.get_n_splits(clientData[i], clientLabel[i])
trainIndex = []
testIndex = []
for enu_index, (train_index, test_index) in enumerate(skf.split(clientData[i], clientLabel[i])):
# let indices at index 4 be used for test
if(enu_index != 2):
trainIndex.append(test_index)
else:
testIndex = test_index
trainIndex = np.hstack((trainIndex))
clientDataTrain.append(clientData[i][trainIndex])
clientLabelTrain.append(clientLabel[i][trainIndex])
clientDataTest.append(clientData[i][testIndex])
clientLabelTest.append(clientLabel[i][testIndex])
clientDataTrain = np.asarray(clientDataTrain,dtype = object)
clientDataTest = np.asarray(clientDataTest,dtype = object)
clientLabelTrain = np.asarray(clientLabelTrain,dtype = object)
clientLabelTest = np.asarray(clientLabelTest,dtype = object)
centralTrainData = np.vstack((clientDataTrain))
centralTrainLabel = np.hstack((clientLabelTrain))
centralTestData = np.vstack((clientDataTest))
centralTestLabel = np.hstack((clientLabelTest))
elif(dataSetName == "RealWorld"):
orientationsNames = ['chest','forearm','head','shin','thigh','upperarm','waist']
clientDataTrain = {new_list: [] for new_list in range(clientCount)}
clientLabelTrain = {new_list: [] for new_list in range(clientCount)}
clientDataTest = {new_list: [] for new_list in range(clientCount)}
clientLabelTest = {new_list: [] for new_list in range(clientCount)}
clientOrientationData = hkl.load(mainDir + 'datasetStandardized/'+str(dataSetName)+'/clientsData.hkl')
clientOrientationLabel = hkl.load(mainDir + 'datasetStandardized/'+str(dataSetName)+'/clientsLabel.hkl')
clientOrientationTest = {new_list: [] for new_list in range(clientCount)}
clientOrientationTrain = {new_list: [] for new_list in range(clientCount)}
orientationIndex = 0
for clientData,clientLabel in zip(clientOrientationData,clientOrientationLabel):
for i in range(0,clientCount):
skf = StratifiedKFold(n_splits=5, shuffle=False)
skf.get_n_splits(clientData[i], clientLabel[i])
trainIndex = []
testIndex = []
for enu_index, (train_index, test_index) in enumerate(skf.split(clientData[i], clientLabel[i])):
# let indices at index 2 be used for test
if(enu_index != 2):
trainIndex.append(test_index)
else:
testIndex = test_index
trainIndex = np.hstack((trainIndex))
clientDataTrain[i].append(clientData[i][trainIndex])
clientLabelTrain[i].append(clientLabel[i][trainIndex])
clientDataTest[i].append(clientData[i][testIndex])
clientLabelTest[i].append(clientLabel[i][testIndex])
clientOrientationTest[i].append(np.full((len(testIndex)),orientationIndex))
clientOrientationTrain[i].append(np.full((len(trainIndex)),orientationIndex))
orientationIndex += 1
for i in range(0,clientCount):
clientDataTrain[i] = np.vstack((clientDataTrain[i]))
clientDataTest[i] = np.vstack((clientDataTest[i]))
clientLabelTrain[i] = np.hstack((clientLabelTrain[i]))
clientLabelTest[i] = np.hstack((clientLabelTest[i]))
clientOrientationTest[i] = np.hstack((clientOrientationTest[i]))
clientOrientationTrain[i] = np.hstack((clientOrientationTrain[i]))
clientOrientationTrain = np.asarray(list(clientOrientationTrain.values()),dtype = object)
clientOrientationTest = np.asarray(list(clientOrientationTest.values()),dtype = object)
clientDataTrain = np.asarray(list(clientDataTrain.values()),dtype = object)
clientDataTest = np.asarray(list(clientDataTest.values()),dtype = object)
clientLabelTrain = np.asarray(list(clientLabelTrain.values()),dtype = object)
clientLabelTest = np.asarray(list(clientLabelTest.values()),dtype = object)
centralTrainData = np.vstack((clientDataTrain))
centralTrainLabel = np.hstack((clientLabelTrain))
centralTestData = np.vstack((clientDataTest))
centralTestLabel = np.hstack((clientLabelTest))
else:
clientData = []
clientLabel = []
for i in range(0,clientCount):
clientData.append(hkl.load(mainDir + 'datasetStandardized/'+dataSetName+'/UserData'+str(i)+'.hkl'))
clientLabel.append(hkl.load(mainDir + 'datasetStandardized/'+dataSetName+'/UserLabel'+str(i)+'.hkl'))
if(dataSetName == "HHAR"):
orientations = hkl.load(mainDir + 'datasetStandardized/HHAR/deviceIndex.hkl')
orientationsNames = ['nexus4', 'lgwatch','s3', 's3mini','gear','samsungold']
for i in range (0,clientCount):
skf = StratifiedKFold(n_splits=5, shuffle=False)
skf.get_n_splits(clientData[i], clientLabel[i])
partitionedData = list()
partitionedLabel = list()
dataIndex = []
trainIndex = []
testIndex = []
for enu_index, (train_index, test_index) in enumerate(skf.split(clientData[i], clientLabel[i])):
if(enu_index != 2):
trainIndex.append(test_index)
else:
testIndex = test_index
trainIndex = np.hstack((trainIndex))
clientDataTrain.append(clientData[i][trainIndex])
clientLabelTrain.append(clientLabel[i][trainIndex])
clientDataTest.append(clientData[i][testIndex])
clientLabelTest.append(clientLabel[i][testIndex])
clientOrientationTrain.append(trainIndex)
clientOrientationTest.append(testIndex)
if(dataSetName == "HHAR"):
for i in range(0,clientCount):
clientOrientationTest[i] = orientations[i][clientOrientationTest[i]]
clientOrientationTrain[i] = orientations[i][clientOrientationTrain[i]]
centralTrainData = (np.vstack((clientDataTrain)))
centralTrainLabel = (np.hstack((clientLabelTrain)))
centralTestData = (np.vstack((clientDataTest)))
centralTestLabel = (np.hstack((clientLabelTest)))
dataReturn = dataHolder
dataReturn.clientDataTrain = clientDataTrain
dataReturn.clientLabelTrain = clientLabelTrain
dataReturn.clientDataTest = clientDataTest
dataReturn.clientLabelTest = clientLabelTest
dataReturn.centralTrainData = centralTrainData
dataReturn.centralTrainLabel = centralTrainLabel
dataReturn.centralTestData = centralTestData
dataReturn.centralTestLabel = centralTestLabel
dataReturn.clientOrientationTrain = clientOrientationTrain
dataReturn.clientOrientationTest = clientOrientationTest
dataReturn.orientationsNames = orientationsNames
return dataReturn
def plot_learningCurve(history, epochs, filepath):
# 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.plot(epoch_range, history.history['val_accuracy'],markevery=[np.argmax(history.history['val_accuracy'])], ls="", marker="o",color="orange")
plt.plot(epoch_range, history.history['accuracy'],markevery=[np.argmax(history.history['accuracy'])], ls="", marker="o",color="blue")
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Val'], loc='lower right')
plt.savefig(filepath+"LearningAccuracy.svg", bbox_inches="tight", format="svg")
plt.show()
plt.clf()
# Plot training & validation loss values
plt.plot(epoch_range, history.history['loss'])
plt.plot(epoch_range, history.history['val_loss'])
plt.plot(epoch_range, history.history['loss'],markevery=[np.argmin(history.history['loss'])], ls="", marker="o",color="blue")
plt.plot(epoch_range, history.history['val_loss'],markevery=[np.argmin(history.history['val_loss'])], ls="", marker="o",color="orange")
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Val'], loc='upper right')
plt.savefig(filepath+"ModelLoss.svg", bbox_inches="tight", format="svg")
plt.show()
plt.clf()
def roundNumber(toRoundNb):
return round(toRoundNb, 4) * 100
def extract_intermediate_model_from_base_model(base_model, intermediate_layer=7):
"""
Create an intermediate model from base mode, which outputs embeddings of the intermediate layer
Parameters:
base_model
the base model from which the intermediate model is built
intermediate_layer
the index of the intermediate layer from which the activations are extracted
Returns:
model (tf.keras.Model)
"""
model = tf.keras.Model(inputs=base_model.inputs, outputs=base_model.layers[intermediate_layer].output, name=base_model.name + "_layer_" + str(intermediate_layer))
return model