-
Notifications
You must be signed in to change notification settings - Fork 0
/
novelty_detector.py
329 lines (244 loc) · 12.2 KB
/
novelty_detector.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
# Copyright 2018-2020 Stanislav Pidhorskyi
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
import torch.utils.data
from torchvision.utils import save_image
from net import *
import os
import utils
from checkpointer import Checkpointer
import numpy as np
import logging
import scipy.optimize
import pickle
from dataloader import *
from utils.jacobian import *
from torchvision.utils import save_image
#from dataloading import make_datasets, make_dataloader, create_set_with_outlier_percentage
from model import Model
from launcher import run
from defaults import get_cfg_defaults
from evaluation import get_f1, evaluate
from utils.threshold_search import find_maximum
from utils.save_plot import save_plot
import matplotlib.pyplot as plt
import scipy.stats
from sklearn.metrics import roc_auc_score
from scipy.special import loggamma
from timeit import default_timer as timer
from scipy.optimize import minimize, dual_annealing
from utils.threshold_search import find_maximum_mv, find_maximum_mv_it
def r_pdf(x, bins, counts):
if bins[0] < x < bins[-1]:
i = np.digitize(x, bins) - 1
return max(counts[i], 1e-308)
if x < bins[0]:
return max(counts[0] * x / bins[0], 1e-308)
return 1e-308
def extract_statistics(cfg, train_set, model, output_folder, no_plots=False):
rlist = []
zlist = []
data_loader = make_dataloader(train_set, cfg.TEST.BATCH_SIZE, torch.cuda.current_device())
for y, x in data_loader:
x = x.view(x.shape[0], cfg.MODEL.INPUT_IMAGE_CHANNELS, cfg.MODEL.INPUT_IMAGE_SIZE, cfg.MODEL.INPUT_IMAGE_SIZE)
z, _ = model.encode(x)
rec = model.generator(z, False)
recon_batch = rec.cpu().detach().numpy()
x = x.cpu().detach().numpy()
for i in range(x.shape[0]):
distance = np.linalg.norm(x[i].flatten() - recon_batch[i].flatten())
rlist.append(distance)
z = z.cpu().detach().numpy()
zlist.append(z)
zlist = np.concatenate(zlist)
counts, bin_edges = np.histogram(rlist, bins=30, density=True)
if cfg.MAKE_PLOTS and not no_plots:
plt.plot(bin_edges[1:], counts, linewidth=2)
save_plot(r"Distance, $\left \|\| I - \hat{I} \right \|\|$",
'Probability density',
r"PDF of distance for reconstruction error, $p\left(\left \|\| I - \hat{I} \right \|\| \right)$",
output_folder + '/reconstruction_error.pdf')
for i in range(cfg.MODEL.LATENT_SPACE_SIZE):
plt.hist(zlist[:, i], density=True, bins='auto', histtype='step')
if cfg.MAKE_PLOTS and not no_plots:
save_plot(r"$z$",
'Probability density',
r"PDF of embeding $p\left(z \right)$",
output_folder + '/embeddingz.pdf')
def fmin(func, x0, args, disp):
x0 = [2.0, 0.0, 1.0]
return scipy.optimize.fmin(func, x0, args, xtol=1e-12, ftol=1e-12, disp=0)
gennorm_param = np.zeros([3, cfg.MODEL.LATENT_SPACE_SIZE])
for i in range(cfg.MODEL.LATENT_SPACE_SIZE):
betta, loc, scale = scipy.stats.gennorm.fit(zlist[:, i], optimizer=fmin)
gennorm_param[0, i] = betta
gennorm_param[1, i] = loc
gennorm_param[2, i] = scale
return counts, bin_edges, gennorm_param
def eval_model_on_valid(cfg, logger, model_s, folding_id, inliner_classes):
train_set, valid_set, test_set = make_datasets(cfg, logger, folding_id, inliner_classes)
print('Validation set size: %d' % len(valid_set))
output_folder = os.path.join('results_' + str(folding_id) + "_" + "_".join([str(x) for x in inliner_classes]))
output_folder = os.path.join(cfg.OUTPUT_DIR, output_folder)
os.makedirs(output_folder, exist_ok=True)
with torch.no_grad():
counts, bin_edges, gennorm_param = extract_statistics(cfg, train_set, model_s, output_folder)
novelty_detector = model_s, bin_edges, counts, gennorm_param
p = 50
alpha, beta, threshold, f1 = compute_threshold_coeffs(cfg, logger, valid_set, inliner_classes, p, novelty_detector)
return f1
def run_novely_prediction_on_dataset(cfg, dataset, inliner_classes, percentage, novelty_detector, concervative=False):
model_s, bin_edges, counts, gennorm_param, = novelty_detector
dataset.shuffle()
dataset = create_set_with_outlier_percentage(dataset, inliner_classes, percentage, concervative)
result = []
gt_novel = []
data_loader = make_dataloader(dataset, cfg.TEST.BATCH_SIZE, torch.cuda.current_device())
include_jacobian = False
N = cfg.MODEL.INPUT_IMAGE_CHANNELS * cfg.MODEL.INPUT_IMAGE_SIZE * cfg.MODEL.INPUT_IMAGE_SIZE - cfg.MODEL.LATENT_SPACE_SIZE
logC = loggamma(N / 2.0) - (N / 2.0) * np.log(2.0 * np.pi)
def logPe_func(x):
# p_{\|W^{\perp}\|} (\|w^{\perp}\|)
# \| w^{\perp} \|}^{m-n}
return logC - (N - 1) * np.log(x), np.log(r_pdf(x, bin_edges, counts))
for label, x in data_loader:
x = x.view(x.shape[0], cfg.MODEL.INPUT_IMAGE_CHANNELS, cfg.MODEL.INPUT_IMAGE_SIZE, cfg.MODEL.INPUT_IMAGE_SIZE)
z, _ = model_s.encode(x)
rec = model_s.generator(z, False)
if include_jacobian:
# J = compute_jacobian(x, z)
J = compute_jacobian_using_finite_differences_v3(z, model_s.generator)
J = J.cpu().numpy()
z = z.cpu().detach().numpy()
recon_batch = rec.cpu().detach().numpy()
x = x.cpu().detach().numpy()
for i in range(x.shape[0]):
if include_jacobian:
u, s, vh = np.linalg.svd(J[i, :, :], full_matrices=False)
logD = np.sum(np.log(np.abs(1.0 / s))) # | \mathrm{det} S^{-1} |
# logD = np.log(np.abs(1.0/(np.prod(s))))
else:
logD = 0
p = scipy.stats.gennorm.pdf(z[i], gennorm_param[0, :], gennorm_param[1, :], gennorm_param[2, :])
logPz = np.sum(np.log(p))
# Sometimes, due to rounding some element in p may be zero resulting in Inf in logPz
# In this case, just assign some large negative value to make sure that the sample
# is classified as unknown.
if not np.isfinite(logPz):
logPz = -1000
distance = np.linalg.norm(x[i].flatten() - recon_batch[i].flatten())
logPe_p1, logPe_p2 = logPe_func(distance)
result.append((logD, logPz, logPe_p1, logPe_p2))
gt_novel.append(label[i].item() in inliner_classes)
result = np.asarray(result, dtype=np.float32)
ground_truth = np.asarray(gt_novel, dtype=np.float32)
return result, ground_truth
def compute_threshold_coeffs(cfg, logger, valid_set, inliner_classes, percentage, novelty_detector):
y_scores_components, y_true = run_novely_prediction_on_dataset(cfg, valid_set, inliner_classes, percentage, novelty_detector, concervative=True)
y_scores_components = np.asarray(y_scores_components, dtype=np.float32)
use_auc = False
def evaluate_auc(threshold, beta, alpha):
coeff = np.asarray([[-1, beta, alpha, 1]], dtype=np.float32)
y_scores = (y_scores_components * coeff).mean(axis=1)
try:
auc = roc_auc_score(y_true, y_scores)
except:
auc = 0
return auc
def evaluate_f1(threshold, beta, alpha):
coeff = np.asarray([[-1, beta, alpha, 1]], dtype=np.float32)
y_scores = (y_scores_components * coeff).mean(axis=1)
y_false = np.logical_not(y_true)
y = np.greater(y_scores, threshold)
true_positive = np.sum(np.logical_and(y, y_true))
false_positive = np.sum(np.logical_and(y, y_false))
false_negative = np.sum(np.logical_and(np.logical_not(y), y_true))
return get_f1(true_positive, false_positive, false_negative)
def func(x):
beta, alpha = x
if use_auc:
return evaluate_auc(0, beta, alpha)
# Find threshold
def eval(th):
return evaluate_f1(th, beta, alpha)
best_th, best_f1 = find_maximum(eval, *cfg.THRESHOLD_NARROW_WINDOW, 1e-2)
return best_f1
if cfg.ALPHA_BETA_TUNING:
cmax, vmax = find_maximum_mv(func, [0.0, 0.0], [30.0, 1.0], xtoll=0.001, ftoll=0.001, verbose=True,
n=8, max_iter=6)
beta, alpha = cmax
else:
beta, alpha = cfg.BETA, cfg.ALPHA
# Find threshold
def eval(th):
return evaluate_f1(th, beta, alpha)
threshold, best_f1 = find_maximum(eval, *cfg.THRESHOLD_FINAL_WINDOW, 1e-3)
logger.info("Best e: %f Best beta: %f Best a: %f best f1: %f" % (threshold, beta, alpha, best_f1))
return alpha, beta, threshold, best_f1
def test(cfg, logger, test_set, inliner_classes, percentage, novelty_detector, alpha, beta, threshold, output_folder):
y_scores_components, y_true = run_novely_prediction_on_dataset(cfg, test_set, inliner_classes, percentage, novelty_detector, concervative=True)
y_scores_components = np.asarray(y_scores_components, dtype=np.float32)
coeff = np.asarray([[-1, beta, alpha, 1]], dtype=np.float32)
y_scores = (y_scores_components * coeff).mean(axis=1)
with open(os.path.join(output_folder, "test_eval_normal.pkl"), "wb") as f:
pickle.dump((y_scores, y_true), f)
return evaluate(cfg, logger, percentage, inliner_classes, y_scores, threshold, y_true)
def main(cfg, logger, local_rank, folding_id, inliner_classes):
torch.cuda.set_device(local_rank)
train_set, valid_set, test_set = make_datasets(cfg, logger, folding_id, inliner_classes)
train_set.shuffle()
print('Validation set size: %d' % len(valid_set))
print('Test set size: %d' % len(test_set))
model_s = Model(
startf=cfg.MODEL.START_CHANNEL_COUNT,
layer_count=cfg.MODEL.LAYER_COUNT,
maxf=cfg.MODEL.MAX_CHANNEL_COUNT,
latent_size=cfg.MODEL.LATENT_SPACE_SIZE,
channels=cfg.MODEL.INPUT_IMAGE_CHANNELS,
generator=cfg.MODEL.GENERATOR,
encoder=cfg.MODEL.ENCODER)
model_s.cuda(local_rank)
model_s.eval()
model_s.requires_grad_(False)
model_dict = {
'encoder_s': model_s.encoder,
'generator_s': model_s.generator,
}
output_folder = os.path.join('results_' + str(folding_id) + "_" + "_".join([str(x) for x in inliner_classes]))
output_folder = os.path.join(cfg.OUTPUT_DIR, output_folder)
os.makedirs(output_folder, exist_ok=True)
checkpointer = Checkpointer(output_folder,
model_dict,
logger=logger,
save=False,
test=True)
extra_checkpoint_data = checkpointer.load()
last_epoch = list(extra_checkpoint_data['auxiliary']['scheduler'].values())[0]['last_epoch']
logger.info("Model trained for %d epochs" % last_epoch)
with torch.no_grad():
counts, bin_edges, gennorm_param = extract_statistics(cfg, train_set, model_s, output_folder)
novelty_detector = model_s, bin_edges, counts, gennorm_param,
percentages = cfg.DATASET.PERCENTAGES
# percentages = [50]
results = {}
for p in percentages:
# plt.figure(num=None, figsize=(8, 6), dpi=180, facecolor='w', edgecolor='k')
alpha, beta, threshold, _ = compute_threshold_coeffs(cfg, logger, valid_set, inliner_classes, p, novelty_detector)
with open(os.path.join(output_folder, 'coeffs_percentage_%d.txt' % int(p)), 'w') as f:
f.write("%f %f %f\n" % (alpha, beta, threshold))
results[p] = test(cfg, logger, test_set, inliner_classes, p, novelty_detector, alpha, beta, threshold, output_folder)
return results
if __name__ == "__main__":
run(main, get_cfg_defaults(), description='', default_config='configs/mnist.yaml',
world_size=1, folding_id=0, inliner_classes=[3])