-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnoise_detection.py
232 lines (196 loc) · 10.3 KB
/
noise_detection.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
import torch
from tqdm import tqdm, trange
from sklearn.metrics import auc
import matplotlib.pyplot as plt
from brokenaxes import brokenaxes
from sklearn.cluster import KMeans
from .consts import get_base_parameters_trainer
from .trainer import Trainer
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='Model trainer')
for k, v in get_base_parameters_trainer().items():
if type(v) == bool:
parser.add_argument(f"--{k.replace('_', '-')}", action="store_true")
else:
parser.add_argument(f"--{k.replace('_', '-')}", type=type(v), default=v)
args = dict(vars(parser.parse_args()))
dataset_name = args["dataset_name"]
noise = args["noise_addition"]
model_name = args["model_name"]
epochs = args["num_train_epochs"]
pars = get_base_parameters_trainer()
pars["wandb"] = False
pars["dataset_name"] = dataset_name
pars["task_name"] = "ner"
pars["model_name"] = model_name
pars["max_seq_length"] = 50
pars["num_train_epochs"] = epochs
pars["do_eval"] = False
pars["noise_addition"] = noise
t = Trainer(**pars)
t.train()
train_losses = torch.zeros(t.train_dataloader.dataset.tensors[0].view(-1).shape[0])
train_noises = t.train_dataloader.dataset.tensors[6]
t.eval_on = 'test'
t.test_examples = t.processor.get_test_examples(t.data_dir)
test_losses = torch.zeros(t.get_dataloader(train=False, force_recompute=True, label_noise_addition=noise).dataset.tensors[0].view(-1).shape[0])
test_noises = t.test_dataloader.dataset.tensors[6]
train_labels = t.train_dataloader.dataset.tensors[3]
test_labels = t.test_dataloader.dataset.tensors[3]
train_max_logits = torch.zeros(t.train_dataloader.dataset.tensors[0].view(-1).shape[0])
test_max_logits = torch.zeros(t.get_dataloader(train=False, force_recompute=True, label_noise_addition=noise).dataset.tensors[0].view(-1).shape[0])
with torch.no_grad():
for input_ids, input_mask, segment_ids, label_ids, valid_ids, l_mask, noise_mask, selected_idxs in tqdm(t.train_dataloader):
input_ids = input_ids.cuda()
input_mask = input_mask.cuda()
segment_ids = segment_ids.cuda()
valid_ids = valid_ids.cuda()
label_ids = label_ids.cuda()
l_mask = l_mask.cuda()
noise_mask = noise_mask.cuda()
selected_idxs = selected_idxs.cuda()
_, logits = t.model(input_ids,
token_type_ids=segment_ids,
attention_mask=input_mask,
valid_ids=valid_ids,
examples_indexes=selected_idxs,
task=t.eval_on,
step=0,
nth_layer=12,
labels=label_ids)
train_losses[selected_idxs.view(-1)] = t.model.last_losses.cpu()
train_max_logits[selected_idxs.view(-1)] = logits.max(-1).values.view(-1).cpu()
for input_ids, input_mask, segment_ids, label_ids, valid_ids, l_mask, noise_mask, selected_idxs in tqdm(t.test_dataloader):
input_ids = input_ids.cuda()
input_mask = input_mask.cuda()
segment_ids = segment_ids.cuda()
valid_ids = valid_ids.cuda()
label_ids = label_ids.cuda()
l_mask = l_mask.cuda()
noise_mask = noise_mask.cuda()
selected_idxs = selected_idxs.cuda()
_, logits = t.model(input_ids,
token_type_ids=segment_ids,
attention_mask=input_mask,
valid_ids=valid_ids,
examples_indexes=selected_idxs,
task=t.eval_on,
step=0,
nth_layer=12,
labels=label_ids)
test_losses[selected_idxs.view(-1)] = t.model.last_losses.cpu()
test_max_logits[selected_idxs.view(-1)] = logits.max(-1).values.view(-1).cpu()
train_noises = train_noises.view(-1)
test_noises = test_noises.view(-1)
original_train_losses = train_losses.clone()
idxs = torch.randperm(train_losses.shape[0])
cutoff = int(idxs.shape[0] * 0.9)
train_idxs = idxs[:cutoff]
test_idxs = idxs[cutoff:]
train_losses, test_losses = train_losses[train_idxs], train_losses[test_idxs]
train_noises, test_noises = train_noises[train_idxs], train_noises[test_idxs]
train_max_logits, test_max_logits = train_max_logits[train_idxs], train_max_logits[test_idxs]
sorted_train_losses = train_losses.sort(dim=0, descending=True)
sorted_test_losses = test_losses.sort(dim=0, descending=True)
sorted_train_noises = train_noises[sorted_train_losses.indices]
sorted_test_noises = test_noises[sorted_test_losses.indices]
sorted_train_max_logits = train_max_logits[sorted_train_losses.indices]
sorted_test_max_logits = test_max_logits[sorted_test_losses.indices]
precisions = []
recalls = []
f1s = []
threshs = []
for i in trange(1, sorted_test_noises.shape[0], 10):
true = sorted_test_noises[:i]
pred = torch.ones_like(true)
TP = ((true == 1) & (pred == 1)).sum().float()
FP = ((true == 0) & (pred == 1)).sum().float()
FN = ((true == 1) & (pred == 0)).sum().float()
if TP + FP == 0:
pr = 0.
else:
pr = (TP / (TP + FP)).item()
precisions.append(pr)
if TP + FN == 0:
re = 0.
else:
re = (TP / sorted_test_noises.sum()).item()
recalls.append(re)
if pr + re == 0:
f1 = 0.
else:
f1 = 2 * pr * re / (pr + re)
f1s.append(f1)
threshs.append(sorted_test_losses.values[i])
smoothed_ap = []
for i, (p, r) in enumerate(tqdm(zip(precisions, recalls), total=len(precisions))):
smoothed_ap.append(max(precisions[i:]))
argmax_f1 = max(zip(f1s, range(len(f1s))))[1]
max_f1 = f1s[argmax_f1]
max_f1_thresh = threshs[argmax_f1]
plt.figure(figsize=(8, 6))
plt.rc('font', family='serif')
plt.rc('ytick', labelsize='x-large')
plt.rc('xtick', labelsize='x-large')
plt.grid(True)
plt.plot(recalls, smoothed_ap, color="k")
plt.title(f"AUC AP: {auc(recalls, smoothed_ap):.4f} - max F1: {max_f1:.4f}({max_f1_thresh:.4f})", fontsize=18)
plt.legend(["AP"], loc=3, fontsize=14)
plt.xlabel('Recall', fontsize=16)
plt.ylabel('Precision', fontsize=16)
plt.savefig(f'AP_test_{dataset_name}_{model_name.replace("-", "").replace("/", "")}_{int(noise * 100)}.pdf', format='pdf')
km = KMeans(n_clusters=2)
km.fit(train_losses.view(-1, 1))
km_pred = km.predict(test_losses.view(-1, 1))
km_pred = torch.tensor(km_pred)
linspace = torch.linspace(train_losses.min(), train_losses.max(), steps=1000)
km_linspace = km.predict(linspace.view(-1, 1))
v = 0
if km_linspace.nonzero()[0][0] != 0:
v = linspace[km_linspace.nonzero()[0][0]]
else:
v = linspace[km_linspace.nonzero()[0][-1]]
blue = torch.tensor([0 / 255, 42 / 255, 92 / 255]) * 0.4 + torch.tensor([66 / 255, 148 / 255, 255 / 255]) * 0.6
red = torch.tensor([112 / 255, 24 / 255, 0 / 255]) * 0.4 + torch.tensor([222 / 255, 100 / 255, 67 / 255]) * 0.6
fig = plt.figure(figsize=(8, 6))
bax = brokenaxes(
ylims=((0, 60000), ((train_losses < 0.18).sum().item() - 15000, (train_losses < 0.2).sum().item() + 15000)),
wspace=.1, despine=False, fig=fig)
plt.rc('ytick', labelsize='x-large')
plt.rc('xtick', labelsize='x-large')
normal = bax.hist(train_losses[(train_noises == 0)], bins=25, alpha=0)
noisy = bax.hist(train_losses[(train_noises == 1)], bins=25, alpha=0)
bax.set_axisbelow(True)
bax.hist(train_losses[(train_noises == 0) & (train_losses <= v)], hatch="///", edgecolor="white", label="Normal",
facecolor=blue, bins=normal[0][1], lw=0)
bax.hist(train_losses[(train_noises == 1) & (train_losses > v)], hatch="\\\\\\", edgecolor="white", label="Noisy",
facecolor=red, bins=noisy[0][1], lw=0)
bax.hist(train_losses[(train_noises == 0) & (train_losses > v)], hatch="///", edgecolor="white", facecolor=blue,
bins=normal[0][1], lw=0)
bax.hist(train_losses[(train_noises == 1) & (train_losses <= v)], hatch="\\\\\\", edgecolor="white", facecolor=red,
bins=noisy[0][1], lw=0)
bax.grid(True)
bax.hist(train_losses[train_noises == 0], histtype="step", lw=1.5, edgecolor="black", bins=normal[0][1])
bax.hist(train_losses[train_noises == 1], histtype="step", lw=1.5, edgecolor="black", bins=noisy[0][1])
bax.axvline(v, ls="--", c="gray", lw=1, label="Classifier cutoff")
bax.legend(loc=1, fontsize=14)
bax.set_xlabel('Training loss', labelpad=20, fontsize=16)
bax.set_ylabel('Number of examples', labelpad=70, fontsize=16)
plt.rc('font', family='serif')
fig.savefig(f'histogram_losses_{dataset_name}_{model_name.replace("-", "").replace("/", "")}_{int(noise * 100)}.pdf', format='pdf', bbox_inches='tight')
TP = ((km_pred == 1) & (test_noises == 1)).sum().float().item()
FP = ((km_pred == 1) & (test_noises == 0)).sum().float().item()
FN = ((km_pred == 0) & (test_noises == 1)).sum().float().item()
precision = TP / (TP + FP) if TP + FP > 0 else 0
recall = TP / (TP + FN) if TP + FN > 0 else 0
f1s = 2 * precision * recall / (precision + recall) if precision + recall > 0 else 0
TP2 = ((km_pred == 0) & (test_noises == 1)).sum().float().item()
FP2 = ((km_pred == 0) & (test_noises == 0)).sum().float().item()
FN2 = ((km_pred == 1) & (test_noises == 1)).sum().float().item()
precision2 = TP2 / (TP2 + FP2) if TP2 + FP2 > 0 else 0
recall2 = TP2 / (TP2 + FN2) if TP2 + FN2 > 0 else 0
f1s2 = 2 * precision2 * recall2 / (precision2 + recall2) if precision2 + recall2 > 0 else 0
with open(f'scores_{dataset_name}_{model_name.replace("-", "").replace("/", "")}_{int(noise * 100)}.txt', "w") as f:
f.writelines([f"{precision}\t{recall}\t{f1s}\n",
f"{precision2}\t{recall2}\t{f1s2}"])