-
Notifications
You must be signed in to change notification settings - Fork 6
/
train_seg.py
257 lines (186 loc) · 9.6 KB
/
train_seg.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
import argparse
import torch.optim.lr_scheduler
from datasets import *
from datasets import dataset_classes
from utils.csv_utils import *
from utils.metrics import *
from utils.training_utils import *
from PromptAD import *
from utils.eval_utils import *
from torchvision import transforms
from tqdm import tqdm
TASK = 'SEG'
def save_check_point(model, path):
selected_keys = [
'feature_gallery1',
'feature_gallery2',
'text_features',
]
state_dict = model.state_dict()
selected_state_dict = {k: v for k, v in state_dict.items() if k in selected_keys}
torch.save(selected_state_dict, path)
def fit(model,
args,
dataloader: DataLoader,
device: str,
img_dir: str,
check_path: str,
train_data: DataLoader,
):
# change the model into eval mode
model.eval_mode()
features1 = []
features2 = []
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).to(device)
_, _, feature_map1, feature_map2 = model.encode_image(data)
features1.append(feature_map1)
features2.append(feature_map2)
features1 = torch.cat(features1, dim=0)
features2 = torch.cat(features2, dim=0)
model.build_image_feature_gallery(features1, features2)
optimizer = torch.optim.SGD(model.prompt_learner.parameters(), lr=args.lr, momentum=args.momentum, weight_decay=args.weight_decay)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=args.Epoch, eta_min=1e-5)
criterion = nn.CrossEntropyLoss().to(device)
criterion_tip = TripletLoss(margin=0.0)
best_result_dict = None
for epoch in range(args.Epoch):
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).to(device)
data = data.to(device)
normal_text_prompt, abnormal_text_prompt_handle, abnormal_text_prompt_learned = model.prompt_learner()
optimizer.zero_grad()
normal_text_features = model.encode_text_embedding(normal_text_prompt, model.tokenized_normal_prompts)
abnormal_text_features_handle = model.encode_text_embedding(abnormal_text_prompt_handle, model.tokenized_abnormal_prompts_handle)
abnormal_text_features_learned = model.encode_text_embedding(abnormal_text_prompt_learned, model.tokenized_abnormal_prompts_learned)
abnormal_text_features = torch.cat([abnormal_text_features_handle, abnormal_text_features_learned], dim=0)
# compute mean
mean_ad_handle = torch.mean(F.normalize(abnormal_text_features_handle, dim=-1), dim=0)
mean_ad_learned = torch.mean(F.normalize(abnormal_text_features_learned, dim=-1), dim=0)
loss_match_abnormal = (mean_ad_handle - mean_ad_learned).norm(dim=0) ** 2.0
_, feature_map, _, _ = model.encode_image(data)
# compute v2t loss and triplet loss
normal_text_features_ahchor = normal_text_features.mean(dim=0).unsqueeze(0)
normal_text_features_ahchor = normal_text_features_ahchor / normal_text_features_ahchor.norm(dim=-1, keepdim=True)
abnormal_text_features_ahchor = abnormal_text_features.mean(dim=0).unsqueeze(0)
abnormal_text_features_ahchor = abnormal_text_features_ahchor / abnormal_text_features_ahchor.norm(dim=-1, keepdim=True)
abnormal_text_features = abnormal_text_features / abnormal_text_features.norm(dim=-1, keepdim=True)
l_pos = torch.einsum('nic,cj->nij', feature_map, normal_text_features_ahchor.transpose(0, 1))
l_neg_v2t = torch.einsum('nic,cj->nij', feature_map, abnormal_text_features.transpose(0, 1))
if model.precision == 'fp16':
logit_scale = model.model.logit_scale.half()
else:
logit_scale = model.model.logit_scalef
logits_v2t = torch.cat([l_pos, l_neg_v2t], dim=-1) * logit_scale
target_v2t = torch.zeros([logits_v2t.shape[0], logits_v2t.shape[1]], dtype=torch.long).to(device)
loss_v2t = criterion(logits_v2t.transpose(1, 2), target_v2t)
trip_loss = criterion_tip(feature_map, normal_text_features_ahchor, abnormal_text_features_ahchor)
loss = loss_v2t + trip_loss + loss_match_abnormal * args.lambda1
loss.backward()
optimizer.step()
scheduler.step()
model.build_text_feature_gallery()
score_maps = []
test_imgs = []
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())]
m = m.numpy()
m[m > 0] = 1
names += [n]
gt_mask_list += [m]
data = data.to(device)
score_map = model(data, 'seg')
score_maps += score_map
test_imgs, score_maps, gt_mask_list = specify_resolution(test_imgs, score_maps, gt_mask_list, resolution=(args.resolution, args.resolution))
result_dict = metric_cal_pix(np.array(score_maps), gt_mask_list)
if best_result_dict is None:
best_result_dict = result_dict
save_check_point(model, check_path)
if args.vis:
plot_sample_cv2(names, test_imgs, {'PromptAD': score_maps}, gt_mask_list, save_folder=img_dir)
elif best_result_dict['p_roc'] < result_dict['p_roc']:
best_result_dict = result_dict
save_check_point(model, check_path)
if args.vis:
plot_sample_cv2(names, test_imgs, {'PromptAD': score_maps}, gt_mask_list, save_folder=img_dir)
return best_result_dict
def main(args):
kwargs = vars(args)
if kwargs['seed'] is None:
kwargs['seed'] = 111
setup_seed(kwargs['seed'])
if kwargs['use_cpu'] == 0:
device = f"cuda:0"
else:
device = f"cpu"
kwargs['device'] = device
# prepare the experiment dir
img_dir, csv_path, check_path = get_dir_from_args(TASK, **kwargs)
# get the train dataloader
train_dataloader, train_dataset_inst = get_dataloader_from_args(phase='train', perturbed=False, **kwargs)
# 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 = PromptAD(**kwargs)
model = model.to(device)
# as the pro metric calculation is costly, we only calculate it in the last evaluation
metrics = fit(model, args, test_dataloader, device, img_dir=img_dir, check_path=check_path, train_data=train_dataloader)
p_roc = round(metrics['p_roc'], 2)
object = kwargs['class_name']
print(f'Object:{object} =========================== Pixel-AUROC:{p_roc}\n')
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='mvtec', choices=['mvtec', 'visa'])
parser.add_argument('--class_name', type=str, default='carpet')
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=400)
parser.add_argument('--vis', type=str2bool, choices=[True, False], default=True)
parser.add_argument("--root-dir", type=str, default="./result")
parser.add_argument("--load-memory", type=str2bool, default=True)
parser.add_argument("--cal-pro", type=str2bool, default=False)
parser.add_argument("--seed", type=int, default=111)
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=1)
parser.add_argument("--backbone", type=str, default="ViT-B-16-plus-240",
choices=['ViT-B-16-plus-240', 'ViT-B-16'])
parser.add_argument("--pretrained_dataset", type=str, default="laion400m_e32")
parser.add_argument("--version", type=str, default='')
parser.add_argument("--use-cpu", type=int, default=0)
# prompt tuning hyper-parameter
parser.add_argument("--n_ctx", type=int, default=4)
parser.add_argument("--n_ctx_ab", type=int, default=1)
parser.add_argument("--n_pro", type=int, default=1)
parser.add_argument("--n_pro_ab", type=int, default=4)
parser.add_argument("--Epoch", type=int, default=100)
# optimizer
parser.add_argument("--lr", type=float, default=0.002)
parser.add_argument("--momentum", type=float, default=0.9)
parser.add_argument("--weight_decay", type=float, default=0.0005)
# loss hyper parameter
parser.add_argument("--lambda1", type=float, default=0.001)
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)