-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompute_auc.py
More file actions
395 lines (275 loc) · 15.3 KB
/
compute_auc.py
File metadata and controls
395 lines (275 loc) · 15.3 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
"""
Module used by `exp1`, `exp2` and `exp3` experiments.
Computes the AUC metric considering models fit and serialized by the three experiments:
* Cornac and ClayRS models at 5, 10, 20, 50 epochs (`exp1` experiment)
* ClayRS models fit on two different Content Analyzer generated representations,
using the caffe reference model for both but with different pre-processing operations, at 10, 20 epochs
(`exp2` experiment)
* ClayRS models fit on two different Content Analyzer generated representations,
using the vgg19 and resnet50 pre-trained neural networks with their required pre-processing operations,
at 10, 20 epochs (`exp3` experiment)
"""
import os
import pickle
import time
from typing import List, Tuple
import cornac
import numpy as np
import pandas as pd
from tqdm import tqdm
import numpy_indexed as npi
from cornac.data import Dataset, ImageModality
from cornac.eval_methods import ranking_eval
import cornac.models.vbpr.recom_vbpr
import clayrs.content_analyzer as ca
import clayrs.recsys as rs
from src import MODEL_DIR, PROCESSED_DIR, REPORTS_DIR, ExperimentConfig
from src.utils import load_user_map, load_item_map, load_train_test_instances
def auc_cornac(vbpr_cornac: cornac.models.vbpr.recom_vbpr.VBPR, train_dataset: List[Tuple], test_dataset: List[Tuple]):
"""
Compute AUC metric on Cornac model invoking `ranking_eval` function from the framework itself
Args:
vbpr_cornac: fit vbpr algorithm using the Cornac framework
train_dataset: list containing the interaction tuples (representing the train set)
test_dataset: list containing the interaction tuples (representing the test set)
Returns:
sys_results: average AUC over all users
user_results: DataFrame containing for each user integer id its corresponding AUC value
"""
sys_result, users_results = ranking_eval(vbpr_cornac, [cornac.metrics.AUC()], train_dataset, test_dataset)
sys_result = sys_result[0]
users_results = users_results[0]
users_results = pd.DataFrame({
'user_idx': list(users_results.keys()),
'AUC': list(users_results.values())
})
return sys_result, users_results
# pylint: disable=too-many-locals
def auc_clayrs(vbpr_clayrs: rs.ContentBasedRS, train_set: ca.Ratings, test_set: ca.Ratings):
"""
Compute AUC metric on ClayRS model using a custom defined implementation of the AUC metric via the numpy library.
Args:
vbpr_clayrs: fit vbpr algorithm using the ClayRS framework
train_set: list containing the interaction tuples (representing the train set)
test_set: list containing the interaction tuples (representing the test set)
Returns:
sys_results: average AUC over all users
user_results: DataFrame containing for each user integer id its corresponding AUC value
"""
n_items = len(test_set.item_map)
item_idxs = np.arange(0, n_items)
query_vector = np.full(item_idxs.shape, True)
pbar = tqdm(test_set.user_idx_column)
pbar.set_description("Computing AUC...")
per_user_result = {"user_idx": [], "AUC": []}
for user_idx in pbar:
positive_items_idxs = train_set.item_idx_column[train_set.get_user_interactions(user_idx, as_indices=True)]
query_vector[positive_items_idxs] = False
filter_list = item_idxs[query_vector]
predictions = vbpr_clayrs.fit_alg.return_scores(user_idx, filter_list)
test_idx = npi.indices(filter_list,
test_set.item_idx_column[test_set.get_user_interactions(user_idx, as_indices=True)])
test_predictions = predictions[test_idx]
negative_predictions = np.delete(predictions, test_idx)
user_auc = (test_predictions > negative_predictions).sum() / (len(negative_predictions) * len(test_predictions))
per_user_result["user_idx"].append(user_idx)
per_user_result["AUC"].append(user_auc)
# reset for next cycle
query_vector[positive_items_idxs] = True
if len(per_user_result["AUC"]) % 100 == 0 or len(per_user_result["AUC"]) == len(test_set):
pbar.set_description(f"AUC after evaluating {len(per_user_result['AUC'])}/{len(test_set)} users ---> "
f"{sum(per_user_result['AUC']) / len(per_user_result['AUC']):.3f}")
sys_result = np.nanmean(per_user_result["AUC"])
per_user_result = pd.DataFrame(per_user_result)
return sys_result, per_user_result
# pylint: disable=too-many-locals
def evaluate_clayrs(models_exp_dir: str, repr_id: str, epoch: int):
"""
Evaluate the ClayRS model fit on the specified number of epochs and on the specified representation key
by first loading it into memory together with the train and test set and invoke the `auc_clayrs()` method to
compute the AUC metric
Args:
models_exp_dir: path to the directory where the trained ClayRS models to evaluate are stored
repr_id: string used to retrieve the corresponding ClayRS model trained on that content representation
epoch: integer used to retrieve the corresponding ClayRS model trained on that number of epochs
Returns:
sys_results: DataFrame containing the average AUC value over all users and the amount of time required
by the evaluation
user_results: DataFrame containing for each user integer id its corresponding AUC value
"""
with open(os.path.join(models_exp_dir, f"vbpr_clayrs_{repr_id}_{epoch}.ml"), "rb") as file:
rec_sys = pickle.load(file)
user_map = load_user_map()
item_map = load_item_map()
train_tuples = load_train_test_instances(mode="train")
test_tuples = load_train_test_instances(mode="test")
train_set = ca.Ratings.from_list(train_tuples, user_map=user_map, item_map=item_map)
test_set = ca.Ratings.from_list(test_tuples, user_map=user_map, item_map=item_map)
start = time.time()
sys_result, users_result = auc_clayrs(rec_sys, train_set, test_set)
end = time.time()
elapsed_m, elapsed_s = divmod(end - start, 60)
sys_result = pd.DataFrame({
"AUC": [sys_result],
"Elapsed time": [f"{int(elapsed_m)}m {int(elapsed_s)}s"]
})
return sys_result, users_result
# pylint: disable=too-many-locals
def evaluate_cornac(models_exp_dir: str, epoch: int):
"""
Evaluate the Cornac model fit on the specified number of epochs by first loading it into memory together with the
train and test set and invoke the `auc_cornac()` method to compute the AUC metric
Args:
models_exp_dir: path to the directory where the trained Cornac models to evaluate are stored
epoch: integer used to retrieve the corresponding Cornac model trained on that number of epochs
Returns:
sys_results: DataFrame containing the average AUC value over all users and the amount of time required
by the evaluation
user_results: DataFrame containing for each user integer id its corresponding AUC value
"""
with open(os.path.join(models_exp_dir, f"vbpr_cornac_{epoch}.ml"), "rb") as file:
vbpr_cornac = pickle.load(file)
user_map = load_user_map()
item_map = load_item_map()
train_tuples = load_train_test_instances(mode="train")
test_tuples = load_train_test_instances(mode="test")
features_matrix = np.load(os.path.join(PROCESSED_DIR, "features_matrix.npy"))
train_dataset = Dataset.build(train_tuples, global_uid_map=user_map, global_iid_map=item_map)
test_dataset = Dataset.build(test_tuples, global_uid_map=user_map, global_iid_map=item_map)
# mock iterator to disable shuffle for replicability
train_dataset.uij_iter = lambda batch_size, shuffle: Dataset.uij_iter(train_dataset, batch_size, shuffle=False)
test_dataset.uij_iter = lambda batch_size, shuffle: Dataset.uij_iter(test_dataset, batch_size, shuffle=False)
# Instantiate a ImageModality for the two datasets
item_image_modality = ImageModality(features=features_matrix, ids=list(item_map.keys()), normalized=True)
item_image_modality.build()
train_dataset.add_modalities(item_image=item_image_modality)
test_dataset.add_modalities(item_image=item_image_modality)
vbpr_cornac.train_set = train_dataset
start = time.time()
sys_result, users_result = auc_cornac(vbpr_cornac, train_dataset, test_dataset)
end = time.time()
elapsed_m, elapsed_s = divmod(end - start, 60)
sys_result = pd.DataFrame({
"AUC": [sys_result],
"Elapsed time": [f"{int(elapsed_m)}m {int(elapsed_s)}s"]
})
return sys_result, users_result
def common_eval_clayrs(models_exp_dir: str, field_representation_list: list, results_output_dir: str):
"""
Encapsulates the common operations carried out to compute the AUC metric on models trained using the
ClayRS framework.
The AUC results will be stored in dataframes and saved locally using the following formats:
* "sys_result_clayrs_{repr_id}_{epoch_num}.csv"
* "users_results_clayrs_{repr_id}_{epoch_num}.csv"
Each result will be uniquely identified by the content representation that was used to train the model and
the number of training epochs
Args:
models_exp_dir: path to the directory where the trained ClayRS models to evaluate are stored
field_representation_list: list containing the id of each representation to take into account during evaluation
results_output_dir: path to the directory where the results of the evaluation will be stored
"""
print("".center(80, "*"))
for repr_id in field_representation_list:
print("Considering representation: ", repr_id)
print("".center(80, "*"))
for epoch_num in ExperimentConfig.epochs:
print(f"Considering number of epochs {epoch_num}")
print("".center(80, '-'))
sys_result_clayrs, users_results_clayrs = evaluate_clayrs(models_exp_dir, repr_id, epoch_num)
print(f"AUC: {float(sys_result_clayrs['AUC'][0])}, "
f"Elapsed time: {str(sys_result_clayrs['Elapsed time'][0])}\n")
sys_result_output_path = os.path.join(results_output_dir,
f"sys_result_clayrs_{repr_id}_{epoch_num}.csv")
users_results_output_path = os.path.join(results_output_dir,
f"users_results_clayrs_{repr_id}_{epoch_num}.csv")
sys_result_clayrs.to_csv(sys_result_output_path,
index=False)
users_results_clayrs.to_csv(users_results_output_path,
index=False)
print(f"AUC sys results saved into {sys_result_output_path}!")
print(f"AUC per user results saved into {users_results_output_path}!")
# if this is the last epoch we do not print the epoch separator ("-")
if epoch_num != ExperimentConfig.epochs[-1]:
print("".center(80, '-'))
# if this is the last representation to use we do not print the representation separator ("*")
if repr_id != field_representation_list[-1]:
print("".center(80, "*"))
def main_exp1():
"""
Actual main function of the module for the `exp1` experiment.
It will compute the AUC metric system-wise and for each user considering ClayRS and Cornac VBPR fit models on all
number of epochs specified via the `-epo` cmd argument (invoking `evaluate_clayrs()`, `evaluate_cornac()`).
Results will be saved into `reports/exp1/results_clayrs` and `reports/exp1/results_cornac`.
"""
print("Evaluating ClayRS:")
models_clayrs_dir = os.path.join(MODEL_DIR, "exp1", "vbpr_clayrs")
models_cornac_dir = os.path.join(MODEL_DIR, "exp1", "vbpr_cornac")
results_clayrs_dir = os.path.join(REPORTS_DIR, "exp1", "results_clayrs")
results_cornac_dir = os.path.join(REPORTS_DIR, "exp1", "results_cornac")
os.makedirs(results_clayrs_dir, exist_ok=True)
os.makedirs(results_cornac_dir, exist_ok=True)
common_eval_clayrs(models_exp_dir=models_clayrs_dir,
field_representation_list=["imported_features"],
results_output_dir=results_clayrs_dir)
print()
print()
print("Evaluating Cornac:")
print("".center(80, "-"))
for epoch in ExperimentConfig.epochs:
print(f"Considering number of epochs {epoch}")
print("".center(80, "-"))
sys_result_cornac, users_results_cornac = evaluate_cornac(models_cornac_dir, epoch)
print(f"AUC: {float(sys_result_cornac['AUC'][0])}, "
f"Elapsed time: {str(sys_result_cornac['Elapsed time'][0])}\n")
sys_result_cornac.to_csv(os.path.join(results_cornac_dir,
f"sys_result_cornac_{epoch}.csv"), index=False)
users_results_cornac.to_csv(os.path.join(results_cornac_dir,
f"users_results_cornac_{epoch}.csv"), index=False)
print(f"AUC sys results saved into "
f"{os.path.join(results_cornac_dir, f'sys_result_cornac_{epoch}.csv')}")
print(f"AUC per user results saved into "
f"{os.path.join(results_cornac_dir, f'users_results_cornac_{epoch}.csv')}")
# if this is the last epoch we do not print the separator
# pylint: disable=duplicate-code
if epoch != ExperimentConfig.epochs[-1]:
print("".center(80, '-'))
def main_exp2():
"""
Actual main function of the module for the `exp2` experiment.
It will compute the AUC metric system-wise and for each user considering ClayRS VBPR fit models on all
number of epochs specified via the `-epo` cmd argument and on the representations identified by the following ids:
'caffe' and 'caffe_center_crop' (invoking `evaluate_clayrs()`).
Results will be saved into `reports/exp2`.
"""
print("Evaluating ClayRS:")
models_clayrs_dir = os.path.join(MODEL_DIR, "exp2")
results_clayrs_dir = os.path.join(REPORTS_DIR, "exp2")
os.makedirs(results_clayrs_dir, exist_ok=True)
field_representation_list = ["caffe", "caffe_center_crop"]
common_eval_clayrs(models_exp_dir=models_clayrs_dir,
field_representation_list=field_representation_list,
results_output_dir=results_clayrs_dir)
def main_exp3():
"""
Actual main function of the module for the `exp3` experiment.
It will compute the AUC metric system-wise and for each user considering ClayRS VBPR fit models on all
number of epochs specified via the `-epo` cmd argument and on the representations identified by the following ids:
'vgg19' and 'resnet50' (invoking `evaluate_clayrs()`).
Results will be saved into `reports/exp3`.
"""
print("Evaluating ClayRS:")
models_clayrs_dir = os.path.join(MODEL_DIR, "exp3")
results_clayrs_dir = os.path.join(REPORTS_DIR, "exp3")
os.makedirs(results_clayrs_dir, exist_ok=True)
field_representation_list = ["vgg19", "resnet50"]
common_eval_clayrs(models_exp_dir=models_clayrs_dir,
field_representation_list=field_representation_list,
results_output_dir=results_clayrs_dir)
if __name__ == "__main__":
# pylint: disable=duplicate-code
if ExperimentConfig.experiment == "exp1":
main_exp1()
elif ExperimentConfig.experiment == "exp2":
main_exp2()
else:
main_exp3()