-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.py
384 lines (319 loc) · 13.5 KB
/
common.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
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
import copy
import os
import pickle
from typing import List
from typing import Union
import faiss
import numpy as np
import scipy.ndimage as ndimage
import torch
import torch.nn.functional as F
class FaissNN(object):
def __init__(self, on_gpu: bool = False, num_workers: int = 4) -> None:
"""FAISS Nearest neighbourhood search.
Args:
on_gpu: If set true, nearest neighbour searches are done on GPU.
num_workers: Number of workers to use with FAISS for similarity search.
"""
faiss.omp_set_num_threads(num_workers)
self.on_gpu = on_gpu
self.search_index = None
def _gpu_cloner_options(self):
return faiss.GpuClonerOptions()
def _index_to_gpu(self, index):
if self.on_gpu:
# For the non-gpu faiss python package, there is no GpuClonerOptions
# so we can not make a default in the function header.
return faiss.index_cpu_to_gpu(
faiss.StandardGpuResources(), 0, index, self._gpu_cloner_options()
)
return index
def _index_to_cpu(self, index):
if self.on_gpu:
return faiss.index_gpu_to_cpu(index)
return index
def _create_index(self, dimension):
if self.on_gpu:
return faiss.GpuIndexFlatL2(
faiss.StandardGpuResources(), dimension, faiss.GpuIndexFlatConfig()
)
return faiss.IndexFlatL2(dimension)
def fit(self, features: np.ndarray) -> None:
"""
Adds features to the FAISS search index.
Args:
features: Array of size NxD.
"""
if self.search_index:
self.reset_index()
self.search_index = self._create_index(features.shape[-1])
self._train(self.search_index, features)
self.search_index.add(features)
def _train(self, _index, _features):
pass
def run(
self,
n_nearest_neighbours,
query_features: np.ndarray,
index_features: np.ndarray = None,
) -> Union[np.ndarray, np.ndarray, np.ndarray]:
"""
Returns distances and indices of nearest neighbour search.
Args:
query_features: Features to retrieve.
index_features: [optional] Index features to search in.
"""
if index_features is None:
return self.search_index.search(query_features, n_nearest_neighbours)
# Build a search index just for this search.
search_index = self._create_index(index_features.shape[-1])
self._train(search_index, index_features)
search_index.add(index_features)
return search_index.search(query_features, n_nearest_neighbours)
def save(self, filename: str) -> None:
faiss.write_index(self._index_to_cpu(self.search_index), filename)
def load(self, filename: str) -> None:
self.search_index = self._index_to_gpu(faiss.read_index(filename))
def reset_index(self):
if self.search_index:
self.search_index.reset()
self.search_index = None
class ApproximateFaissNN(FaissNN):
def _train(self, index, features):
index.train(features)
def _gpu_cloner_options(self):
cloner = faiss.GpuClonerOptions()
cloner.useFloat16 = True
return cloner
def _create_index(self, dimension):
index = faiss.IndexIVFPQ(
faiss.IndexFlatL2(dimension),
dimension,
512, # n_centroids
64, # sub-quantizers
8,
) # nbits per code
return self._index_to_gpu(index)
class _BaseMerger:
def __init__(self):
"""Merges feature embedding by name."""
def merge(self, features: list):
features = [self._reduce(feature) for feature in features]
return np.concatenate(features, axis=1)
class AverageMerger(_BaseMerger):
@staticmethod
def _reduce(features):
# NxCxWxH -> NxC
return features.reshape([features.shape[0], features.shape[1], -1]).mean(
axis=-1
)
class ConcatMerger(_BaseMerger):
@staticmethod
def _reduce(features):
# NxCxWxH -> NxCWH
return features.reshape(len(features), -1)
class Preprocessing(torch.nn.Module):
def __init__(self, input_dims, output_dim):
super(Preprocessing, self).__init__()
self.input_dims = input_dims
self.output_dim = output_dim
self.preprocessing_modules = torch.nn.ModuleList()
for input_dim in input_dims:
module = MeanMapper(output_dim)
self.preprocessing_modules.append(module)
def forward(self, features):
_features = []
for module, feature in zip(self.preprocessing_modules, features):
_features.append(module(feature))
return torch.stack(_features, dim=1)
class MeanMapper(torch.nn.Module):
def __init__(self, preprocessing_dim):
super(MeanMapper, self).__init__()
self.preprocessing_dim = preprocessing_dim
def forward(self, features):
features = features.reshape(len(features), 1, -1)
return F.adaptive_avg_pool1d(features, self.preprocessing_dim).squeeze(1)
class Aggregator(torch.nn.Module):
def __init__(self, target_dim):
super(Aggregator, self).__init__()
self.target_dim = target_dim
def forward(self, features):
"""Returns reshaped and average pooled features."""
# batchsize x number_of_layers x input_dim -> batchsize x target_dim
features = features.reshape(len(features), 1, -1)
features = F.adaptive_avg_pool1d(features, self.target_dim)
return features.reshape(len(features), -1)
class RescaleSegmentor:
def __init__(self, device, target_size=224):
self.device = device
self.target_size = target_size
self.smoothing = 4
def convert_to_segmentation(self, patch_scores):
with torch.no_grad():
if isinstance(patch_scores, np.ndarray):
patch_scores = torch.from_numpy(patch_scores)
_scores = patch_scores.to(self.device)
_scores = _scores.unsqueeze(1)
_scores = F.interpolate(
_scores, size=self.target_size, mode="bilinear", align_corners=False
)
_scores = _scores.squeeze(1)
patch_scores = _scores.cpu().numpy()
return [
ndimage.gaussian_filter(patch_score, sigma=self.smoothing)
for patch_score in patch_scores
]
class NetworkFeatureAggregator(torch.nn.Module):
"""Efficient extraction of network features."""
def __init__(self, backbone, layers_to_extract_from, device):
super(NetworkFeatureAggregator, self).__init__()
"""Extraction of network features.
Runs a network only to the last layer of the list of layers where
network features should be extracted from.
Args:
backbone: torchvision.model
layers_to_extract_from: [list of str]
"""
self.layers_to_extract_from = layers_to_extract_from
self.backbone = backbone
self.device = device
if not hasattr(backbone, "hook_handles"):
self.backbone.hook_handles = []
for handle in self.backbone.hook_handles:
handle.remove()
self.outputs = {}
for extract_layer in layers_to_extract_from:
forward_hook = ForwardHook(
self.outputs, extract_layer, layers_to_extract_from[-1]
)
if "." in extract_layer:
extract_block, extract_idx = extract_layer.split(".")
network_layer = backbone.__dict__["_modules"][extract_block]
if extract_idx.isnumeric():
extract_idx = int(extract_idx)
network_layer = network_layer[extract_idx]
else:
network_layer = network_layer.__dict__["_modules"][extract_idx]
else:
network_layer = backbone.__dict__["_modules"][extract_layer]
if isinstance(network_layer, torch.nn.Sequential):
self.backbone.hook_handles.append(
network_layer[-1].register_forward_hook(forward_hook)
)
else:
self.backbone.hook_handles.append(
network_layer.register_forward_hook(forward_hook)
)
self.to(self.device)
def forward(self, images):
self.outputs.clear()
with torch.no_grad():
# The backbone will throw an Exception once it reached the last
# layer to compute features from. Computation will stop there.
try:
_ = self.backbone(images)
except LastLayerToExtractReachedException:
pass
return self.outputs
def feature_dimensions(self, input_shape):
"""Computes the feature dimensions for all layers given input_shape."""
_input = torch.ones([1] + list(input_shape)).to(self.device)
_output = self(_input)
return [_output[layer].shape[1] for layer in self.layers_to_extract_from]
class ForwardHook:
def __init__(self, hook_dict, layer_name: str, last_layer_to_extract: str):
self.hook_dict = hook_dict
self.layer_name = layer_name
self.raise_exception_to_break = copy.deepcopy(
layer_name == last_layer_to_extract
)
def __call__(self, module, input, output):
self.hook_dict[self.layer_name] = output
if self.raise_exception_to_break:
raise LastLayerToExtractReachedException()
return None
class LastLayerToExtractReachedException(Exception):
pass
class NearestNeighbourScorer(object):
def __init__(self, n_nearest_neighbours: int, nn_method=FaissNN(False, 4)) -> None:
"""
Neearest-Neighbourhood Anomaly Scorer class.
Args:
n_nearest_neighbours: [int] Number of nearest neighbours used to
determine anomalous pixels.
nn_method: Nearest neighbour search method.
"""
self.feature_merger = ConcatMerger()
self.n_nearest_neighbours = n_nearest_neighbours
self.nn_method = nn_method
self.imagelevel_nn = lambda query: self.nn_method.run(
n_nearest_neighbours, query
)
self.pixelwise_nn = lambda query, index: self.nn_method.run(1, query, index)
def fit(self, detection_features: List[np.ndarray]) -> None:
"""Calls the fit function of the nearest neighbour method.
Args:
detection_features: [list of np.arrays]
[[bs x d_i] for i in n] Contains a list of
np.arrays for all training images corresponding to respective
features VECTORS (or maps, but will be resized) produced by
some backbone network which should be used for image-level
anomaly detection.
"""
self.detection_features = self.feature_merger.merge(
detection_features,
)
self.nn_method.fit(self.detection_features)
def predict(
self, query_features: List[np.ndarray]
) -> Union[np.ndarray, np.ndarray, np.ndarray]:
"""Predicts anomaly score.
Searches for nearest neighbours of test images in all
support training images.
Args:
detection_query_features: [dict of np.arrays] List of np.arrays
corresponding to the test features generated by
some backbone network.
"""
query_features = self.feature_merger.merge(
query_features,
)
query_distances, query_nns = self.imagelevel_nn(query_features)
anomaly_scores = np.mean(query_distances, axis=-1)
return anomaly_scores, query_distances, query_nns
@staticmethod
def _detection_file(folder, prepend=""):
return os.path.join(folder, prepend + "nnscorer_features.pkl")
@staticmethod
def _index_file(folder, prepend=""):
return os.path.join(folder, prepend + "nnscorer_search_index.faiss")
@staticmethod
def _save(filename, features):
if features is None:
return
with open(filename, "wb") as save_file:
pickle.dump(features, save_file, pickle.HIGHEST_PROTOCOL)
@staticmethod
def _load(filename: str):
with open(filename, "rb") as load_file:
return pickle.load(load_file)
def save(
self,
save_folder: str,
save_features_separately: bool = False,
prepend: str = "",
) -> None:
self.nn_method.save(self._index_file(save_folder, prepend))
if save_features_separately:
self._save(
self._detection_file(save_folder, prepend), self.detection_features
)
def save_and_reset(self, save_folder: str) -> None:
self.save(save_folder)
self.nn_method.reset_index()
def load(self, load_folder: str, prepend: str = "") -> None:
self.nn_method.load(self._index_file(load_folder, prepend))
if os.path.exists(self._detection_file(load_folder, prepend)):
self.detection_features = self._load(
self._detection_file(load_folder, prepend)
)