-
Notifications
You must be signed in to change notification settings - Fork 4
/
predict.py
287 lines (210 loc) · 8 KB
/
predict.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
from cnn import CNN
from privacy_policies_dataset import PrivacyPoliciesDataset as PPD
from os.path import join, isfile
from os import listdir
from collections import OrderedDict
import time
import torch
import pickle
import csv
import numpy as np
import matplotlib.pyplot as plt
import data_processing as dp
from data_processing import get_policy_of_interest_tokens
def _cm(y, y_hat):
#Empty dict where the data will be stored
cm = OrderedDict()
#Computation fo true positives, false positives, true negatives and false negatives
tp = (y * y_hat).sum()
tn = ((1 - y) * (1 - y_hat)).sum()
fp = (y_hat * (1 - y)).sum()
fn = ((1 - y_hat) * y).sum()
#Storage of results in the dictionary
cm['TP'] = tp.item()
cm['TN'] = tn.item()
cm['FP'] = fp.item()
cm['FN'] = fn.item()
return cm
def _cms(y, y_hat):
#Empty tensor where the data will be stored
cms = torch.tensor([])
#Computation of cm for every label and pack them in cms
for label in range(12):
cm = torch.tensor(_cm(y[:,label], y_hat[:,label]).values()).unsqueeze(1)
cms = torch.cat([cms,cm],1)
return cms
def _metrics(cm):
tp, tn, fp, fn = cm.values()
eps = 1e-10
#Computation of F1 score, precision and recall
p = tp / (tp + fp + eps)
r = tp / (tp + fn + eps)
f1 = 2 * p * r / (p + r + eps)
return f1, p, r
def _metrics_t(y, y_pred, t):
y_hat = y_pred > t
cm = _cm(y, y_hat.double())
return _metrics(cm)
def _metrics_wrt_t(y, y_pred):
#Initialization of range of thresholds and empty lists to store results
ts = np.arange(0, 1, 0.01)
f1s = []
ps = []
rs = []
#loop that computes metrics for every threshold
for t in ts:
f1, p, r = _metrics_t(y, y_pred, t)
#Storage of results
f1s.append(f1)
ps.append(p)
rs.append(r)
return f1s, ps, rs, ts
def _best_t_idx(y, y_pred):
idxs = []
for label in range(12):
f1, p, r, ts = _metrics_wrt_t(y[:,label], y_pred[:,label])
index = np.array(f1).argmax().item()
idxs.append(index)
return idxs
def macro_metrics(y, y_hat):
eps = 1e-10
cms = _cms(y, y_hat)
ps = cms[0] / (cms[0] + cms[2] + eps)
rs = cms[0] / (cms[0] + cms[3] + eps)
p = torch.mean(ps)
r = torch.mean(rs)
f1 = torch.mean(2 * ps * rs / (ps + rs + eps))
return f1, p, r
def micro_metrics(y, y_hat):
eps = 1e-10
cms = _cms(y, y_hat)
cm = cms.sum(1)
p = cm[0] / (cm[0] + cm[2] + eps)
r = torch.mean(cm[0] / (cm[0] + cm[3] + eps))
f1 = torch.mean(2 * p * r / (p + r + eps))
return f1, p, r
def best_metrics(y, y_pred):
f1s = []
ps = []
rs = []
ts = []
idxs = _best_t_idx(y, y_pred)
for idx, label in zip(idxs, range(12)):
f1, p, r, t = _metrics_wrt_t(y[:,label], y_pred[:,label])
f1s.append(f1[idx])
ps.append(p[idx])
rs.append(r[idx])
ts.append(t[idx])
return f1s, ps, rs, ts
def save_metrics(y, y_pred, path):
def label_scores(y, y_pred, label, idx):
f1s, ps, rs, ts = predict._metrics_wrt_t(y[:,label], y_pred[:,label])
best_scores = f1s[idx], ps[idx], rs[idx]
scores_05 = predict._metrics_t(y[:,label], y_pred[:,label], 0.5)
return scores_05 + best_scores
with open(path, 'w') as f:
writer = csv.writer(f)
idxs = predict._best_t_idx(y, y_pred)
for label, idx in zip(range(12), idxs):
scores = label_scores(y, y_pred, label, idx)
writer.writerows([scores])
def load_model(path, label, epochs_num):
#We set the name of the model and its parameters
models_files = join(path, 'cnn_300_200_[100, 25]_1_[3]_e{}_label{}_polisis_state.pt')
model_file = models_files.format(epochs_num , label)
params_files = join(path, 'cnn_300_200_[100, 25]_1_[3]_e{}_label{}_polisis_params.pkl')
params_file = params_files.format(epochs_num , label)
#We now load the parameters
with open(params_file, 'rb') as f:
params = pickle.load(f)
#We now load the model and pass the parameters
model = CNN(**params)
model.load_state_dict(torch.load(model_file))
return model
def load_12CNN_model(path):
#We instantiate an empty dictionary that will contain the models
model12cnn = OrderedDict()
epochs_num = [60, 60, 150, 150, 70, 100, 150, 100, 70, 65, 80, 60]
for label in range(12):
model12cnn['model{}'.format(label)] = load_model(path, label, epochs_num[label])
return model12cnn
def predict(data, models):
#We instantiate an empty y and instantiate the x
x = PPD.collate_data(data)[0]
y = torch.tensor([])
#We start a timer to compute predicions time and compute them
start = time.time()
for key, model in models.items():
y_label = model(x)
y = torch.cat([y, y_label],1)
end = time.time()
print("Prediction time: {} seconds". format(end - start))
return
def main():
#We set the folder path containing the models and load the labels
# folder = 'trained_models/New folder'
# models = load_12CNN_model(folder)
# data_folder = 'datasets'
# data_file = join(data_folder, 'test_dataset_label6.pkl')
# data = PPD.unpickle_dataset(data_file)
# predictions = predict(data, models)
# We set the name of the model and its parameters
path = 'trained_models'
model_file = join(path, 'cnn_300_200_[100]_12_[3]_zeros_60-20-20_polisis_state.pt')
params_file = join(path, 'cnn_300_200_[100]_12_[3]_zeros_60-20-20_polisis_params.pkl')
#We set the folder containing the data already prepared for predicting
data_folder = 'datasets'
data_file = join(data_folder, 'test_dataset_label6.pkl')
# We now load the parameters
with open(params_file, 'rb') as f:
params = pickle.load(f)
model = CNN(**params)
model.load_state_dict(torch.load(model_file))
model.eval()
#We load 8the labels
#with open('labels.pkl', 'rb') as f:
#labels = pickle.load(f)
# labels = ('First Party Collection/Use', 'Third Party Sharing/Collection', 'User Access, Edit and Deletion', 'Data Retention',
# 'Data Security', 'International and Specific Audiences', 'Do Not Track', 'Policy Change', 'User Choice/Control',
# 'Introductory/Generic', 'Practice not covered', 'Privacy contact information')
#
# all_tokens , all_paragraphs = get_policy_of_interest_tokens("random_pp", "embeddings_data")
# segments_tensor = dp.process_policy_of_interest(all_tokens , all_paragraphs)
# predictions = model(segments_tensor)
# y_pred = predictions > 0.5
#
# for row in range(len(all_paragraphs)):
# predictedValues = y_pred[row, :]
# for label in range(12):
# if predictedValues[label] == 1:
# print("paragraph " + str(row) + " : " + labels[label])
# print('--------------------------------------')
#
#
data = PPD.unpickle_dataset(data_file)
x = PPD.collate_data(data)[0]
y_pred = model(x) > 0.5
predictions = model(x)
#
# for row in range(len(y_pred)):
# predictedValues = y_pred[row, :]
# for label in range(12):
# if predictedValues[label] == 1:
# print("paragraph " + str(row) + " : " + labels[label])
# print('--------------------------------------')
#Computation of all metrics
f1s, ps, rs, ts = _metrics_wrt_t(data.labels_tensor,predictions)
figure = plt.figure(figsize=(18,5))
figure.suptitle('Micro Averages with respect to threshold')
ax_f1 = figure.add_subplot(131)
ax_f1.set_ylim(0.2,0.72)
ax_p = figure.add_subplot(132)
ax_p.set_ylim(0,1)
ax_r = figure.add_subplot(133)
ax_r.set_ylim(0,1)
ax_f1.plot(ts, f1s)
ax_p.plot(ts, ps)
ax_r.plot(ts, rs)
plt.show()
if __name__ == '__main__':
main()