-
Notifications
You must be signed in to change notification settings - Fork 23
/
eval_WinCLIP.py
167 lines (124 loc) · 5.46 KB
/
eval_WinCLIP.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
import argparse
from datasets import *
from datasets import dataset_classes
from utils.csv_utils import *
from utils.metrics import *
from utils.training_utils import *
from WinCLIP import *
from utils.eval_utils import *
def test(model,
dataloader: DataLoader,
device: str,
is_vis: bool,
img_dir: str,
class_name: str,
cal_pro: bool,
train_data: DataLoader,
resolution: int):
# change the model into eval mode
model.eval_mode()
logger.info('begin build text feature gallery...')
model.build_text_feature_gallery(class_name)
logger.info('build text feature gallery finished.')
if train_data is not None:
logger.info('begin build image feature gallery...')
for (data, mask, label, name, img_type) in train_data:
data = [model.transform(Image.fromarray(cv2.cvtColor(f.numpy(), cv2.COLOR_BGR2RGB))) for f in data]
data = torch.stack(data, dim=0)
data = data.to(device)
model.build_image_feature_gallery(data)
logger.info('build image feature gallery finished.')
scores = []
test_imgs = []
gt_list = []
gt_mask_list = []
names = []
for (data, mask, label, name, img_type) in dataloader:
data = [model.transform(Image.fromarray(f.numpy())) for f in data]
data = torch.stack(data, dim=0)
for d, n, l, m in zip(data, name, label, mask):
test_imgs += [denormalization(d.cpu().numpy())]
l = l.numpy()
m = m.numpy()
m[m > 0] = 1
names += [n]
gt_list += [l]
gt_mask_list += [m]
data = data.to(device)
score = model(data)
scores += score
test_imgs, scores, gt_mask_list = specify_resolution(test_imgs, scores, gt_mask_list, resolution=(resolution, resolution))
result_dict = metric_cal(np.array(scores), gt_list, gt_mask_list, cal_pro=cal_pro)
if is_vis:
plot_sample_cv2(names, test_imgs, {'WinClip': scores}, gt_mask_list, save_folder=img_dir)
return result_dict
def main(args):
kwargs = vars(args)
logger.info('==========running parameters=============')
for k, v in kwargs.items():
logger.info(f'{k}: {v}')
logger.info('=========================================')
seeds = [111, 333, 999]
kwargs['seed'] = seeds[kwargs['experiment_indx']]
setup_seed(kwargs['seed'])
if kwargs['use_cpu'] == 0:
device = f"cuda:0"
else:
device = f"cpu"
kwargs['device'] = device
# prepare the experiment dir
model_dir, img_dir, logger_dir, model_name, csv_path = get_dir_from_args(**kwargs)
# get the train dataloader
if kwargs['k_shot'] > 0:
train_dataloader, train_dataset_inst = get_dataloader_from_args(phase='train', perturbed=False, **kwargs)
else:
train_dataloader, train_dataset_inst = None, None
# get the test dataloader
test_dataloader, test_dataset_inst = get_dataloader_from_args(phase='test', perturbed=False, **kwargs)
kwargs['out_size_h'] = kwargs['resolution']
kwargs['out_size_w'] = kwargs['resolution']
# get the model
model = WinClipAD(**kwargs)
model = model.to(device)
# as the pro metric calculation is costly, we only calculate it in the last evaluation
metrics = test(model, test_dataloader, device, is_vis=True, img_dir=img_dir,
class_name=kwargs['class_name'], cal_pro=kwargs['cal_pro'], train_data=train_dataloader,
resolution=kwargs['resolution'])
logger.info(f"\n")
for k, v in metrics.items():
logger.info(f"{kwargs['class_name']}======={k}: {v:.2f}")
save_metric(metrics, dataset_classes[kwargs['dataset']], kwargs['class_name'],
kwargs['dataset'], csv_path)
def str2bool(v):
return v.lower() in ("yes", "true", "t", "1")
def get_args():
parser = argparse.ArgumentParser(description='Anomaly detection')
parser.add_argument('--dataset', type=str, default='visa', choices=['mvtec', 'visa'])
parser.add_argument('--class-name', type=str, default='candle')
parser.add_argument('--img-resize', type=int, default=240)
parser.add_argument('--img-cropsize', type=int, default=240)
parser.add_argument('--resolution', type=int, default=400)
parser.add_argument('--batch-size', type=int, default=128)
parser.add_argument('--vis', type=str2bool, choices=[True, False], default=True)
parser.add_argument("--root-dir", type=str, default="./result_winclip")
parser.add_argument("--load-memory", type=str2bool, default=True)
parser.add_argument("--cal-pro", type=str2bool, default=False)
parser.add_argument("--experiment_indx", type=int, default=0)
parser.add_argument("--gpu-id", type=int, default=0)
# pure test
parser.add_argument("--pure-test", type=str2bool, default=False)
# method related parameters
parser.add_argument('--k-shot', type=int, default=0)
parser.add_argument('--scales', nargs='+', type=int, default=(2, 3, ))
parser.add_argument("--backbone", type=str, default="ViT-B-16-plus-240",
choices=['ViT-B-16-plus-240'])
parser.add_argument("--pretrained_dataset", type=str, default="laion400m_e32")
parser.add_argument("--use-cpu", type=int, default=0)
args = parser.parse_args()
return args
if __name__ == '__main__':
import os
args = get_args()
os.environ['CURL_CA_BUNDLE'] = ''
os.environ['CUDA_VISIBLE_DEVICES'] = f"{args.gpu_id}"
main(args)