-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_id.py
More file actions
431 lines (327 loc) · 16.4 KB
/
user_id.py
File metadata and controls
431 lines (327 loc) · 16.4 KB
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import pickle
from lightgbm import LGBMClassifier
from sklearn.ensemble import RandomForestClassifier, ExtraTreesClassifier
from sklearn.linear_model import LogisticRegression
from xgboost import XGBClassifier
from tqdm.notebook import tqdm
from sklearn.ensemble import VotingClassifier
import pickle
from IPython.display import display
import joblib
import warnings
warnings.filterwarnings('ignore')
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
from sklearn.metrics import confusion_matrix
from imblearn.over_sampling import SMOTE
SEED = 42
LINUX = True
if (LINUX):
RESULTS_DIR = os.path.join('./id_results/')
MODELS_DIR = os.path.join('./id_models/')
else:
RESULTS_DIR = os.path.join('C:\\Users\\sarab\\Desktop\\id_results', 'authentication', 'user_evaluation_1')
MODELS_DIR = os.path.join('C:\\Users\\sarab\\Desktop\\id_models', 'authentication', 'user_evaluation_1')
# Training flags
TRAIN = False
TRAIN_VOTING = True
# Heigth normalization flag
NORM_HEIGHT = True
# Traffic inclusion flag
TRAFFIC = True
# Movement inclusion flag
MOV = True
############## Game Identification Class ##############
class GameIdentification:
"""Main identification system class."""
def __init__(self, id_data):
self.forklift_dataset = id_data['forklift_simulator']
self.beat_saber_dataset = id_data['beat_saber']
self.forklift_models = {}
self.beat_saber_models = {}
self.forklift_test_set = {}
self.beat_saber_test_set = {}
self.models = {}
self.performance_metrics = pd.DataFrame(columns=['User_ID', 'Subset_no', 'Game_Type', 'Model_Type', 'Accuracy', 'Precision',
'Recall', 'F1'])
self.confusion_matrices = {}
def preprocess_data(self, data):
"""Preprocess data for training/testing."""
processed_data = data.copy()
X = processed_data.drop(columns=['time','ID'])
y = processed_data['ID']
return X, y
def train_multiple_models(self, filename="normal"):
"""Train and compare multiple identification models."""
self.models = {
'ET': ExtraTreesClassifier( bootstrap = False, max_depth = None,
min_samples_split= 2, n_estimators = 800,
random_state=SEED),
'RF': RandomForestClassifier(n_estimators=100, random_state=SEED),
'LR': LogisticRegression(random_state=SEED, max_iter=1000),
'XGB': XGBClassifier(random_state=SEED),
'LGBM': LGBMClassifier(random_state=SEED, verbose=-1),
}
# Add the VotingClassifier
voting_classifiers = [
('ET', self.models.get('ET')), # ExtraTreesClassifier
('RF', self.models.get('RF')), # RandomForestClassifier
('XGB', self.models.get('XGB')), # XGBoost
('LGBM', self.models.get('LGBM')), # LGBMClassifier
('LR', self.models.get('LR')), # LogisticRegression
]
voting_classifier = VotingClassifier(
estimators=voting_classifiers,
voting='soft',
n_jobs=-1
)
self.models["VotingClassifier"] = voting_classifier
self.train_game_models(
self.forklift_dataset,
self.forklift_models,
'forklift_simulator',
self.models,
filename=filename
)
self.train_game_models(
self.beat_saber_dataset,
self.beat_saber_models,
'beat_saber',
self.models,
filename=filename
)
def split_data(self, data, train_minutes=8):
data['time'] = data['time'].map(int)
train_data = data[data['time']<train_minutes]
test_data = data[data['time']>=train_minutes]
return train_data, test_data
def train_game_models(self, game_dataset, model_dict, game_type, models, test_intruders_count = 4, apply_smote=False, filename="normal"):
"""Train models for a specific game."""
models_dir = os.path.join(MODELS_DIR, game_type)
os.makedirs(models_dir, exist_ok=True)
dataset_metrics = []
np.random.seed(SEED)
self.confusion_matrices[game_type] = {}
train_data, test_data = self.split_data(game_dataset)
# Sample dataset info for one of the sets
dataset_info = {
'Game_Type': game_type,
'Train_Size': train_data.shape[0],
'Test_Size': test_data.shape[0]
}
dataset_metrics.append(dataset_info)
accuracy = []
recall = []
precision = []
confusion = []
f1 = []
X_train, y_train = self.preprocess_data(train_data)
X_test, y_test = self.preprocess_data(test_data)
for model_name, model in models.items():
#print(model_name)
model.fit(X_train, y_train)
model_dict[model_name] = model
model_path = os.path.join(models_dir, f'{model_name}_{filename}.joblib')
os.makedirs(models_dir, exist_ok=True)
joblib.dump(model, model_path)
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred, zero_division=0, average='macro')
recall = recall_score(y_test, y_pred, zero_division=0, average='macro')
f1 = f1_score(y_test, y_pred, zero_division=0, average='macro')
self.confusion_matrices[game_type][model_name] = confusion_matrix(y_test, y_pred)
self.performance_metrics = pd.concat([
self.performance_metrics,
pd.DataFrame({
'Game_Type': [game_type],
'Model_Type': [model_name],
'Accuracy': [accuracy],
'Precision': [precision],
'Recall': [recall],
'F1': [f1]
})
], ignore_index=True)
os.makedirs(RESULTS_DIR, exist_ok=True)
dataset_metrics_df = pd.DataFrame(dataset_metrics)
dataset_metrics_path = os.path.join(RESULTS_DIR, f'dataset_metrics_{game_type}_{filename}.csv')
dataset_metrics_df.to_csv(dataset_metrics_path, index=False) # - problem to solve: path too long
model_metrics_path = os.path.join(RESULTS_DIR, f'model_metrics_{filename}.csv')
self.performance_metrics.to_csv(model_metrics_path, index=False)
model_metrics_path = os.path.join(RESULTS_DIR, f'confusion_{filename}.pkl')
with open(model_metrics_path, 'wb') as outp: # Overwrites any existing file.
pickle.dump(self.confusion_matrices, outp, pickle.HIGHEST_PROTOCOL)
def get_performance_summary(self):
"""Get performance summary."""
return self.performance_metrics
def get_confusion(self, model_name):
"""Get performance summary."""
return self.confusion_matrices[game][model_name]
def identify_user(self, gameplay_data, model=None):
"""Identify a user based on gameplay data."""
res = model.predict_proba(gameplay_data)
return res
def perform_voting(self, voting_threshold=0.5, model_name="ET", filename="initial", output_filename = 'id_voting_results_nofilename.pkl'):
output_prefix = f"voting_effect_{filename}_{model_name}"
results = []
user_results_by_game = {'forklift_simulator': {}, 'beat_saber': {}}
for game_type in ['beat_saber', 'forklift_simulator']:
print(f'Game: {game_type}')
if game_type == 'forklift_simulator':
game_dataset = self.forklift_dataset
else:
game_dataset = self.beat_saber_dataset
train_data, test_data = self.split_data(game_dataset)
model_path = os.path.join(MODELS_DIR, game_type, f'{model_name}_{filename}.joblib')
loaded_model = joblib.load(model_path)
y_test = test_data['ID'].values
user_results_by_game[game_type] = []
samples = len(np.where(y_test==0)[0])
for n_samples in range(1, int(np.floor(samples / 3))):
acc = 0
tot = 0
print(n_samples)
for user in range(30):
user_data = test_data[test_data['ID'] == user]
user_copy = user_data.copy()
user_features = user_copy.drop(columns=['ID', 'time'])
preds = self.identify_user(user_features, model = loaded_model)
for offset in range(0, samples + 1 - n_samples, 1): # 24 so that when n sample is 1 range is between 0 and 24 (i.e., 0-23), and when n samples is 23 range is between 0 and 2
group = preds[offset: offset + n_samples]
id_dist = np.zeros(30)
for i in range(n_samples):
id_dist += group[i]
id_dist /= np.sum(id_dist)
if (np.argmax(id_dist) == user):
acc += 1
tot +=1
accuracy = acc / tot
result = {
'Game_Type': game_type,
'Model_Type': model_name,
'Window_size': n_samples,
'Accuracy': accuracy,
}
user_results_by_game[game_type].append(result)
results.append(result)
results_df = pd.DataFrame(results)
csv_path = os.path.join(RESULTS_DIR, f'{output_prefix}.csv')
results_df.to_csv(csv_path, index=False)
with open(output_filename, 'wb') as outp: # Overwrites any existing file.
pickle.dump(results_df, outp, pickle.HIGHEST_PROTOCOL)
return results_df
def visualize_voting_results(self,results):
accuracy_mean = results.groupby(['Game_Type', 'Window_size','Model_Type']).mean().reset_index()
for game_type in ['forklift_simulator', 'beat_saber']:
test_game = accuracy_mean.loc[accuracy_mean['Game_Type']==game_type]
plt.figure(figsize=(14, 8))
plt.plot(test_game['Window_size'], test_game['Accuracy'], label=game_type)
plt.title(f'Overall Accuracy by Voting Samples - {game_type.replace("_", " ").title()}', fontsize=14)
plt.xlabel('Number of Samples Used for Voting', fontsize=12)
plt.ylabel('Overall Accuracy', fontsize=12)
plt.grid(True, alpha=0.3)
plt.legend(loc='lower right', bbox_to_anchor=(1, 0), ncol=3)
plt.ylim(0.4, 1.01)
plt.tight_layout()
plt.show()
############## Functions ##############
def save_object(obj, filename):
with open(filename, 'wb') as outp: # Overwrites any existing file.
pickle.dump(obj, outp, pickle.HIGHEST_PROTOCOL)
def prepare_id_data(forklift_simulator, beat_saber, include_traffic=False, include_mov=True):
"""Prepare data for authentication systems."""
data_forklift_simulator = forklift_simulator.copy()
data_forklift_simulator.drop(columns=data_forklift_simulator.columns[0], axis=1, inplace=True) # first column contains row index
data_beat_saber = beat_saber.copy()
data_beat_saber.drop(columns=data_beat_saber.columns[0], axis=1, inplace=True) # first column contains row index
if not include_traffic:
cols_to_drop = []
cols = forklift_simulator.columns
for col in cols:
if 'Traffic' in col:
cols_to_drop.append(col)
data_forklift_simulator = data_forklift_simulator.drop(columns=cols_to_drop)
data_beat_saber = data_beat_saber.drop(columns=cols_to_drop)
if not include_mov:
data_copy = data_forklift_simulator.copy()
cols = list(data_copy.columns)
to_keep = []
for col in cols:
if 'Traffic' in col:
to_keep.append(col)
to_keep.append('ID')
to_keep.append('time')
data_forklift_simulator = data_copy[to_keep]
data_copy = data_beat_saber.copy()
data_beat_saber = data_copy[to_keep]
data_forklift_simulator['ID'] -= 30
return {
'forklift_simulator': data_forklift_simulator,
'beat_saber': data_beat_saber
}
############## Main code ##############
def main():
# Load data
if (LINUX):
forklift_simulator = pd.read_csv('./data/processed/slow_features.csv').fillna(0)
beat_saber = pd.read_csv('./data/processed/fast_features.csv').fillna(0)
else:
forklift_simulator = pd.read_csv('G:\\Il mio Drive\\Esperimenti Oculus\\VR authentication\\vr-user-identification-authentication-main\\vr-user-identification-authentication-main\\data\\processed\\users\\slow_features.csv').fillna(0)
beat_saber = pd.read_csv('G:\\Il mio Drive\\Esperimenti Oculus\\VR authentication\\vr-user-identification-authentication-main\\vr-user-identification-authentication-main\\data\\processed\\users\\fast_features.csv').fillna(0)
forklift_simulator = forklift_simulator[forklift_simulator["ID"].isin(range(30,60))]
beat_saber = beat_saber[beat_saber["ID"].isin(range(0,30))]
#traffic_forklift_simulator =traffic_forklift_simulator[traffic_forklift_simulator["ID"].isin(range(30,60))]
#traffic_beat_saber = traffic_beat_saber[traffic_beat_saber["ID"].isin(range(0,30))]
if MOV:
filename = 'MOV_'
else:
filename = ''
if TRAFFIC:
filename += 'TRAFFIC'
if NORM_HEIGHT:
filename += '_NO_HEIGHT'
output_filename = filename + '.pkl'
voting_results = 'id_voting_results_' + filename + '_confidence.pkl'
for col in forklift_simulator.columns.values:
if "PosY" in col and "Acc" not in col and "Vel" not in col:
for id in range(30):
beat_saber.loc[beat_saber['ID'] == id,col] = beat_saber.loc[beat_saber['ID'] == id,col] / np.mean(beat_saber.loc[beat_saber['ID'] == id,col])
for id in range(30,60):
forklift_simulator.loc[forklift_simulator['ID'] == id,col] = forklift_simulator.loc[forklift_simulator['ID'] == id,col] / np.mean(forklift_simulator.loc[forklift_simulator['ID'] == id,col])
else:
output_filename = 'id_mov_traffic.pkl'
voting_results = 'id_voting_results_mov_traffic_confidence.pkl'
# Prepare data for identification
identification_data = prepare_id_data(
forklift_simulator,
beat_saber,
include_traffic = TRAFFIC,
include_mov = MOV
)
if TRAIN:
# Create game identifier instance
identifier = GameIdentification(identification_data)
# Train multiple models
identifier.train_multiple_models(filename = filename)
# Save authenticator
save_object(identifier, output_filename)
else:
with open(output_filename, 'rb') as file:
identifier = pickle.load(file)
# Get performance metrics
performance_metrics = identifier.get_performance_summary()
# Show average performance by model type
model_performance = performance_metrics.groupby(['Game_Type', 'Model_Type']).mean().reset_index()
print("Average model performance across all users:")
display(model_performance[['Game_Type', 'Model_Type', 'Accuracy', 'Precision', 'Recall', 'F1']])
# Implement voting
if TRAIN_VOTING:
results = identifier.perform_voting(voting_threshold=0.5, model_name="ET", filename=filename, output_filename=voting_results)
else:
with open(voting_results, 'rb') as file:
results = pickle.load(file)
identifier.visualize_voting_results(results)
if __name__ == '__main__':
main()