forked from hmyao22/DADF
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualization.py
More file actions
87 lines (72 loc) · 2.46 KB
/
Copy pathvisualization.py
File metadata and controls
87 lines (72 loc) · 2.46 KB
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
import matplotlib.pyplot as plt
import os
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
import argparse
import os
import torch
import yaml
from ignite.contrib import metrics
import constants as const
import dataset
import fastflow
import utils
from PIL import Image
from torchvision import transforms
from fuse_main import build_model
from thop import profile
from thop import clever_format
image_transform = transforms.Compose(
[
transforms.Resize(256),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
])
Config = const.DefaultConfig()
backbone_name = 'WResnet50'
class_name = 'cable'
Config.parse({'backbone_name': backbone_name})
Config.parse({'class_name': class_name})
model = build_model(Config)
model = model.cuda()
checkpoint = torch.load(os.path.join(os.path.join(const.CHECKPOINT_DIR, Config.class_name), Config.backbone_name+"_fuseflow.pt"))
model.nf_flows.load_state_dict(checkpoint["model_nf_flows"])
model.norms.load_state_dict(checkpoint["model_norms_state_dict"])
model = model.eval()
image_file = r'D:\IMSN-YHM\dataset\cable\test\cable_swap\001.png'
image = Image.open(image_file).convert('RGB').resize((256, 256))
image_tensor = image_transform(image).unsqueeze(0).cuda()
import time
for i in range(10):
t1 = time.time()
output = model(image_tensor)
t2 = time.time()
print(t2-t1)
# with torch.no_grad():
# memory_before = torch.cuda.memory_allocated()
# output = model(image_tensor)
# # Get final memory usage
# memory_after = torch.cuda.memory_allocated()
# memory_usage = (memory_after - memory_before) / (1024 ** 2) # in MB
# print("Memory usage: ", memory_usage, " MB")
with torch.no_grad():
features = model.feature_extractor(image_tensor)
x_local, x_global = model.reconstructor.forward_(features)
print(features[0].shape)
print(x_local[0].shape)
print(x_global[0].shape)
plt.figure()
plt.subplot(131)
plt.imshow(features[0][0][2].clone().cpu().detach().numpy())
plt.subplot(132)
plt.imshow(x_local[0][0][2].clone().cpu().detach().numpy())
plt.subplot(133)
plt.imshow(x_global[0][0][2].clone().cpu().detach().numpy())
plt.show()
anomaly_map = output["anomaly_map"].cpu().detach().squeeze(0).squeeze(0)
plt.figure()
plt.subplot(121)
plt.imshow(image)
plt.subplot(122)
plt.imshow((1+anomaly_map)**2, cmap='jet')
plt.colorbar()
plt.show()