-
Notifications
You must be signed in to change notification settings - Fork 38
/
estimator.py
340 lines (282 loc) · 15.5 KB
/
estimator.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
import os
from PIL import Image
import numpy as np
import torch
import torch.nn.functional as F
import torchvision.transforms as transforms
from torchvision.transforms import ToPILImage
import yaml
import random
from options.config_class import Config
import sys
sys.path.insert(0, os.path.join(os.getcwd(), os.path.dirname(__file__), 'image_segmentation'))
import network
from optimizer import restore_snapshot
from datasets import cityscapes
from config import assert_and_infer_cfg
sys.path.insert(0, os.path.join(os.getcwd(), os.path.dirname(__file__), 'image_synthesis'))
from image_synthesis.models.pix2pix_model import Pix2PixModel
from image_dissimilarity.models.dissimilarity_model import DissimNetPrior, DissimNet
from image_dissimilarity.models.vgg_features import VGG19_difference
from image_dissimilarity.data.cityscapes_dataset import one_hot_encoding
class AnomalyDetector():
def __init__(self, ours=True, seed=0, fishyscapes_wrapper=True):
self.set_seeds(seed)
# Common options for all models
TestOptions = Config()
self.opt = TestOptions
torch.cuda.empty_cache()
self.get_segmentation()
self.get_synthesis()
self.get_dissimilarity(ours)
self.get_transformations()
self.fishyscapes_wrapper= fishyscapes_wrapper
def estimator_image(self, image):
image_og_h = image.size[1]
image_og_w = image.size[0]
img = image.resize((2048, 1024))
img_tensor = self.img_transform(img)
# predict segmentation
with torch.no_grad():
seg_outs = self.seg_net(img_tensor.unsqueeze(0).cuda())
seg_softmax_out = F.softmax(seg_outs, dim=1)
seg_final = np.argmax(seg_outs.cpu().numpy().squeeze(), axis=0) # segmentation map
# get entropy
entropy = torch.sum(-seg_softmax_out * torch.log(seg_softmax_out), dim=1)
entropy = (entropy - entropy.min()) / entropy.max()
entropy *= 255 # for later use in the dissimilarity
# get softmax distance
distance, _ = torch.topk(seg_softmax_out, 2, dim=1)
max_logit = distance[:, 0, :, :]
max2nd_logit = distance[:, 1, :, :]
result = max_logit - max2nd_logit
distance = 1 - (result - result.min()) / result.max()
distance *= 255 # for later use in the dissimilarity
# get label map for synthesis model
label_out = np.zeros_like(seg_final)
for label_id, train_id in self.opt.dataset_cls.id_to_trainid.items():
label_out[np.where(seg_final == train_id)] = label_id
label_img = Image.fromarray((label_out).astype(np.uint8))
# prepare for synthesis
label_tensor = self.transform_semantic(label_img) * 255.0
label_tensor[label_tensor == 255] = 35 # 'unknown' is opt.label_nc
image_tensor = self.transform_image_syn(img)
# Get instance map in right format. Since prediction doesn't have instance map, we use semantic instead
instance_tensor = label_tensor.clone()
# run synthesis
syn_input = {'label': label_tensor.unsqueeze(0), 'instance': instance_tensor.unsqueeze(0),
'image': image_tensor.unsqueeze(0)}
generated = self.syn_net(syn_input, mode='inference')
image_numpy = (np.transpose(generated.squeeze().cpu().numpy(), (1, 2, 0)) + 1) / 2.0
synthesis_final_img = Image.fromarray((image_numpy * 255).astype(np.uint8))
# prepare dissimilarity
entropy = entropy.cpu().numpy()
distance = distance.cpu().numpy()
entropy_img = Image.fromarray(entropy.astype(np.uint8).squeeze())
distance = Image.fromarray(distance.astype(np.uint8).squeeze())
semantic = Image.fromarray((seg_final).astype(np.uint8))
# get initial transformation
semantic_tensor = self.base_transforms_diss(semantic) * 255
syn_image_tensor = self.base_transforms_diss(synthesis_final_img)
image_tensor = self.base_transforms_diss(img)
syn_image_tensor = self.norm_transform_diss(syn_image_tensor).unsqueeze(0).cuda()
image_tensor = self.norm_transform_diss(image_tensor).unsqueeze(0).cuda()
# get softmax difference
perceptual_diff = self.vgg_diff(image_tensor, syn_image_tensor)
min_v = torch.min(perceptual_diff.squeeze())
max_v = torch.max(perceptual_diff.squeeze())
perceptual_diff = (perceptual_diff.squeeze() - min_v) / (max_v - min_v)
perceptual_diff *= 255
perceptual_diff = perceptual_diff.cpu().numpy()
perceptual_diff = Image.fromarray(perceptual_diff.astype(np.uint8))
# finish transformation
perceptual_diff_tensor = self.base_transforms_diss(perceptual_diff).unsqueeze(0).cuda()
entropy_tensor = self.base_transforms_diss(entropy_img).unsqueeze(0).cuda()
distance_tensor = self.base_transforms_diss(distance).unsqueeze(0).cuda()
# hot encode semantic map
semantic_tensor[semantic_tensor == 255] = 20 # 'ignore label is 20'
semantic_tensor = one_hot_encoding(semantic_tensor, 20).unsqueeze(0).cuda()
# run dissimilarity
with torch.no_grad():
if self.prior:
diss_pred = F.softmax(
self.diss_model(image_tensor, syn_image_tensor, semantic_tensor, entropy_tensor,
perceptual_diff_tensor,
distance_tensor), dim=1)
else:
diss_pred = F.softmax(self.diss_model(image_tensor, syn_image_tensor, semantic_tensor), dim=1)
diss_pred = diss_pred.cpu().numpy()
# do ensemble if necessary
if self.ensemble:
diss_pred = diss_pred[:, 1, :, :] * 0.75 + entropy_tensor.cpu().numpy() * 0.25
else:
diss_pred = diss_pred[:, 1, :, :]
# Resize outputs to original input image size
diss_pred = Image.fromarray(diss_pred.squeeze()*255).resize((image_og_w, image_og_h))
seg_img = semantic.resize((image_og_w, image_og_h))
entropy = entropy_img.resize((image_og_w, image_og_h))
perceptual_diff = perceptual_diff.resize((image_og_w, image_og_h))
distance = entropy.resize((image_og_w, image_og_h))
synthesis = synthesis_final_img.resize((image_og_w, image_og_h))
out = {'anomaly_map': diss_pred, 'segmentation': seg_img, 'synthesis': synthesis,
'softmax_entropy':entropy, 'perceptual_diff': perceptual_diff, 'softmax_distance': distance}
return out
# Loop around all figures
def estimator_worker(self, image):
image_og_h = image.shape[0]
image_og_w = image.shape[1]
img = Image.fromarray(np.array(image)).convert('RGB').resize((2048, 1024))
img_tensor = self.img_transform(img)
# predict segmentation
with torch.no_grad():
seg_outs = self.seg_net(img_tensor.unsqueeze(0).cuda())
seg_softmax_out = F.softmax(seg_outs, dim=1)
seg_final = np.argmax(seg_outs.cpu().numpy().squeeze(), axis=0) # segmentation map
# get entropy
entropy = torch.sum(-seg_softmax_out * torch.log(seg_softmax_out), dim=1)
entropy = (entropy - entropy.min()) / entropy.max()
entropy *= 255 # for later use in the dissimilarity
# get softmax distance
distance, _ = torch.topk(seg_softmax_out, 2, dim=1)
max_logit = distance[:, 0, :, :]
max2nd_logit = distance[:, 1, :, :]
result = max_logit - max2nd_logit
distance = 1 - (result - result.min()) / result.max()
distance *= 255 # for later use in the dissimilarity
# get label map for synthesis model
label_out = np.zeros_like(seg_final)
for label_id, train_id in self.opt.dataset_cls.id_to_trainid.items():
label_out[np.where(seg_final == train_id)] = label_id
label_img = Image.fromarray((label_out).astype(np.uint8))
# prepare for synthesis
label_tensor = self.transform_semantic(label_img) * 255.0
label_tensor[label_tensor == 255] = 35 # 'unknown' is opt.label_nc
image_tensor = self.transform_image_syn(img)
# Get instance map in right format. Since prediction doesn't have instance map, we use semantic instead
instance_tensor = label_tensor.clone()
# run synthesis
syn_input = {'label': label_tensor.unsqueeze(0), 'instance': instance_tensor.unsqueeze(0),
'image': image_tensor.unsqueeze(0)}
generated = self.syn_net(syn_input, mode='inference')
image_numpy = (np.transpose(generated.squeeze().cpu().numpy(), (1, 2, 0)) + 1) / 2.0
synthesis_final_img = Image.fromarray((image_numpy * 255).astype(np.uint8))
# prepare dissimilarity
entropy = entropy.cpu().numpy()
distance = distance.cpu().numpy()
entropy_img = Image.fromarray(entropy.astype(np.uint8).squeeze())
distance = Image.fromarray(distance.astype(np.uint8).squeeze())
semantic = Image.fromarray((seg_final).astype(np.uint8))
# get initial transformation
semantic_tensor = self.base_transforms_diss(semantic) * 255
syn_image_tensor = self.base_transforms_diss(synthesis_final_img)
image_tensor = self.base_transforms_diss(img)
syn_image_tensor = self.norm_transform_diss(syn_image_tensor).unsqueeze(0).cuda()
image_tensor = self.norm_transform_diss(image_tensor).unsqueeze(0).cuda()
# get softmax difference
perceptual_diff = self.vgg_diff(image_tensor, syn_image_tensor)
min_v = torch.min(perceptual_diff.squeeze())
max_v = torch.max(perceptual_diff.squeeze())
perceptual_diff = (perceptual_diff.squeeze() - min_v) / (max_v - min_v)
perceptual_diff *= 255
perceptual_diff = perceptual_diff.cpu().numpy()
perceptual_diff = Image.fromarray(perceptual_diff.astype(np.uint8))
# finish transformation
perceptual_diff_tensor = self.base_transforms_diss(perceptual_diff).unsqueeze(0).cuda()
entropy_tensor = self.base_transforms_diss(entropy_img).unsqueeze(0).cuda()
distance_tensor = self.base_transforms_diss(distance).unsqueeze(0).cuda()
# hot encode semantic map
semantic_tensor[semantic_tensor == 255] = 20 # 'ignore label is 20'
semantic_tensor = one_hot_encoding(semantic_tensor, 20).unsqueeze(0).cuda()
# run dissimilarity
with torch.no_grad():
if self.prior:
diss_pred = F.softmax(
self.diss_model(image_tensor, syn_image_tensor, semantic_tensor, entropy_tensor,
perceptual_diff_tensor,
distance_tensor), dim=1)
else:
diss_pred = F.softmax(self.diss_model(image_tensor, syn_image_tensor, semantic_tensor), dim=1)
diss_pred = diss_pred.cpu().numpy()
# do ensemble if necessary
if self.ensemble:
diss_pred = diss_pred[:, 1, :, :] * 0.75 + entropy_tensor.cpu().numpy() * 0.25
else:
diss_pred = diss_pred[:, 1, :, :]
diss_pred = np.array(Image.fromarray(diss_pred.squeeze()).resize((image_og_w, image_og_h)))
out = {'anomaly_score': torch.tensor(diss_pred), 'segmentation': torch.tensor(seg_final)}
return out['anomaly_score']
def set_seeds(self, seed=0):
# set seeds for reproducibility
torch.manual_seed(0)
np.random.seed(0)
random.seed(0)
def get_segmentation(self):
# Get Segmentation Net
assert_and_infer_cfg(self.opt, train_mode=False)
self.opt.dataset_cls = cityscapes
net = network.get_net(self.opt, criterion=None)
net = torch.nn.DataParallel(net).cuda()
print('Segmentation Net Built.')
snapshot = os.path.join(os.getcwd(), os.path.dirname(__file__), self.opt.snapshot)
self.seg_net, _ = restore_snapshot(net, optimizer=None, snapshot=snapshot,
restore_optimizer_bool=False)
self.seg_net.eval()
print('Segmentation Net Restored.')
def get_synthesis(self):
# Get Synthesis Net
print('Synthesis Net Built.')
self.opt.checkpoints_dir = os.path.join(os.getcwd(), os.path.dirname(__file__), self.opt.checkpoints_dir)
self.syn_net = Pix2PixModel(self.opt)
self.syn_net.eval()
print('Synthesis Net Restored')
def get_dissimilarity(self, ours):
# Get Dissimilarity Net
if ours:
config_diss = os.path.join(os.getcwd(), os.path.dirname(__file__), 'image_dissimilarity/configs/test/ours_configuration.yaml')
else:
config_diss = os.path.join(os.getcwd(), os.path.dirname(__file__), 'image_dissimilarity/configs/test/baseline_configuration.yaml')
with open(config_diss, 'r') as stream:
config_diss = yaml.load(stream, Loader=yaml.FullLoader)
self.prior = config_diss['model']['prior']
self.ensemble = config_diss['ensemble']
if self.prior:
self.diss_model = DissimNetPrior(**config_diss['model']).cuda()
else:
self.diss_model = DissimNet(**config_diss['model']).cuda()
print('Dissimilarity Net Built.')
save_folder = os.path.join(os.getcwd(), os.path.dirname(__file__), config_diss['save_folder'])
model_path = os.path.join(save_folder,
'%s_net_%s.pth' % (config_diss['which_epoch'], config_diss['experiment_name']))
model_weights = torch.load(model_path)
self.diss_model.load_state_dict(model_weights)
self.diss_model.eval()
print('Dissimilarity Net Restored')
def get_transformations(self):
# Transform images to Tensor based on ImageNet Mean and STD
mean_std = ([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
self.img_transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize(*mean_std)])
# synthesis necessary pre-process
self.transform_semantic = transforms.Compose(
[transforms.Resize(size=(256, 512), interpolation=Image.NEAREST), transforms.ToTensor()])
self.transform_image_syn = transforms.Compose(
[transforms.Resize(size=(256, 512), interpolation=Image.BICUBIC), transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5),
(0.5, 0.5, 0.5))])
# dissimilarity pre-process
self.vgg_diff = VGG19_difference().cuda()
self.base_transforms_diss = transforms.Compose(
[transforms.Resize(size=(256, 512), interpolation=Image.NEAREST), transforms.ToTensor()])
self.norm_transform_diss = transforms.Compose(
[transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))]) # imageNet normamlization
self.to_pil = ToPILImage()
if __name__ == '__main__':
import bdlb
# define fishyscapes test parameters
fs = bdlb.load(benchmark="fishyscapes")
# automatically downloads the dataset
data = fs.get_dataset('Static')
detector = AnomalyDetector(True)
metrics = fs.evaluate(detector.estimator_worker, data)
print('My method achieved {:.2f}% AP'.format(100 * metrics['AP']))
print('My method achieved {:.2f}% FPR@95TPR'.format(100 * metrics['FPR@95%TPR']))
print('My method achieved {:.2f}% auroc'.format(100 * metrics['auroc']))