-
Notifications
You must be signed in to change notification settings - Fork 56
/
main_visualize.py
59 lines (41 loc) · 1.36 KB
/
main_visualize.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
import argparse
import matplotlib.pyplot as plt
from codes import mvtecad
from tqdm import tqdm
from codes.utils import resize, makedirpath
parser = argparse.ArgumentParser()
parser.add_argument('--obj', default='wood')
args = parser.parse_args()
def save_maps(obj, maps):
from skimage.segmentation import mark_boundaries
N = maps.shape[0]
images = mvtecad.get_x(obj, mode='test')
masks = mvtecad.get_mask(obj)
for n in tqdm(range(N)):
fig, axes = plt.subplots(ncols=2)
fig.set_size_inches(6, 3)
image = resize(images[n], (128, 128))
mask = resize(masks[n], (128, 128))
image = mark_boundaries(image, mask, color=(1, 0, 0), mode='thick')
axes[0].imshow(image)
axes[0].set_axis_off()
axes[1].imshow(maps[n], vmax=maps[n].max(), cmap='Reds')
axes[1].set_axis_off()
plt.tight_layout()
fpath = f'anomaly_maps/{obj}/n{n:03d}.png'
makedirpath(fpath)
plt.savefig(fpath)
plt.close()
#########################
def main():
from codes.inspection import eval_encoder_NN_multiK
from codes.networks import EncoderHier
obj = args.obj
enc = EncoderHier(K=64, D=64).cuda()
enc.load(obj)
enc.eval()
results = eval_encoder_NN_multiK(enc, obj)
maps = results['maps_mult']
save_maps(obj, maps)
if __name__ == '__main__':
main()