-
Notifications
You must be signed in to change notification settings - Fork 0
/
dinomaly_visa_uni.py
243 lines (189 loc) · 9.34 KB
/
dinomaly_visa_uni.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
# This is a sample Python script.
# Press ⌃R to execute it or replace it with your code.
# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings.
import torch
import torch.nn as nn
from dataset import get_data_transforms, get_strong_transforms
from torchvision.datasets import ImageFolder
import numpy as np
import random
import os
from torch.utils.data import DataLoader, ConcatDataset
from models.uad import ViTill, ViTillv2
from models import vit_encoder
from torch.nn.init import trunc_normal_
from models.vision_transformer import Block as VitBlock, bMlp, Attention, LinearAttention, \
LinearAttention2
from dataset import MVTecDataset
import torch.backends.cudnn as cudnn
import argparse
from utils import evaluation_batch, evaluation_batch_fast, global_cosine_hm_percent, global_cosine, \
regional_cosine_hm, WarmCosineScheduler
from torch.nn import functional as F
from functools import partial
from ptflops import get_model_complexity_info
from optimizers import StableAdamW
import warnings
import copy
import logging
from sklearn.metrics import roc_auc_score, average_precision_score
import itertools
warnings.filterwarnings("ignore")
def get_logger(name, save_path=None, level='INFO'):
logger = logging.getLogger(name)
logger.setLevel(getattr(logging, level))
log_format = logging.Formatter('%(message)s')
streamHandler = logging.StreamHandler()
streamHandler.setFormatter(log_format)
logger.addHandler(streamHandler)
if not save_path is None:
os.makedirs(save_path, exist_ok=True)
fileHandler = logging.FileHandler(os.path.join(save_path, 'log.txt'))
fileHandler.setFormatter(log_format)
logger.addHandler(fileHandler)
return logger
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
def setup_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def train(item_list):
setup_seed(1)
total_iters = 10000
batch_size = 16
image_size = 448
crop_size = 392
data_transform, gt_transform = get_data_transforms(image_size, crop_size)
train_data_list = []
test_data_list = []
for i, item in enumerate(item_list):
train_path = os.path.join(args.data_path, item, 'train')
test_path = os.path.join(args.data_path, item)
train_data = ImageFolder(root=train_path, transform=data_transform)
train_data.classes = item
train_data.class_to_idx = {item: i}
train_data.samples = [(sample[0], i) for sample in train_data.samples]
test_data = MVTecDataset(root=test_path, transform=data_transform, gt_transform=gt_transform, phase="test")
train_data_list.append(train_data)
test_data_list.append(test_data)
train_data = ConcatDataset(train_data_list)
train_dataloader = torch.utils.data.DataLoader(train_data, batch_size=batch_size, shuffle=True, num_workers=4,
drop_last=True)
# test_dataloader_list = [torch.utils.data.DataLoader(test_data, batch_size=batch_size, shuffle=False, num_workers=4)
# for test_data in test_data_list]
# encoder_name = 'dinov2reg_vit_small_14'
encoder_name = 'dinov2reg_vit_base_14'
# encoder_name = 'dinov2reg_vit_large_14'
target_layers = [2, 3, 4, 5, 6, 7, 8, 9]
fuse_layer_encoder = [[0, 1, 2, 3], [4, 5, 6, 7]]
fuse_layer_decoder = [[0, 1, 2, 3], [4, 5, 6, 7]]
# fuse_layer_encoder = [[0], [1], [2], [3], [4], [5], [6], [7]]
# fuse_layer_decoder = [[0], [1], [2], [3], [4], [5], [6], [7]]
encoder = vit_encoder.load(encoder_name)
if 'small' in encoder_name:
embed_dim, num_heads = 384, 6
elif 'base' in encoder_name:
embed_dim, num_heads = 768, 12
elif 'large' in encoder_name:
embed_dim, num_heads = 1024, 16
target_layers = [4, 6, 8, 10, 12, 14, 16, 18]
else:
raise "Architecture not in small, base, large."
bottleneck = []
decoder = []
bottleneck.append(bMlp(embed_dim, embed_dim * 4, embed_dim, drop=0.2))
bottleneck = nn.ModuleList(bottleneck)
for i in range(8):
blk = VitBlock(dim=embed_dim, num_heads=num_heads, mlp_ratio=4.,
qkv_bias=True, norm_layer=partial(nn.LayerNorm, eps=1e-8), attn_drop=0.,
attn=LinearAttention2)
decoder.append(blk)
decoder = nn.ModuleList(decoder)
model = ViTill(encoder=encoder, bottleneck=bottleneck, decoder=decoder, target_layers=target_layers,
mask_neighbor_size=0, fuse_layer_encoder=fuse_layer_encoder, fuse_layer_decoder=fuse_layer_decoder)
model = model.to(device)
trainable = nn.ModuleList([bottleneck, decoder])
for m in trainable.modules():
if isinstance(m, nn.Linear):
trunc_normal_(m.weight, std=0.01, a=-0.03, b=0.03)
if isinstance(m, nn.Linear) and m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.LayerNorm):
nn.init.constant_(m.bias, 0)
nn.init.constant_(m.weight, 1.0)
optimizer = StableAdamW([{'params': trainable.parameters()}],
lr=2e-3, betas=(0.9, 0.999), weight_decay=1e-4, amsgrad=True, eps=1e-10)
lr_scheduler = WarmCosineScheduler(optimizer, base_value=2e-3, final_value=2e-4, total_iters=total_iters,
warmup_iters=100)
print_fn('train image number:{}'.format(len(train_data)))
it = 0
for epoch in range(int(np.ceil(total_iters / len(train_dataloader)))):
model.train()
loss_list = []
for img, label in train_dataloader:
img = img.to(device)
label = label.to(device)
en, de = model(img)
p_final = 0.9
p = min(p_final * it / 1000, p_final)
loss = global_cosine_hm_percent(en, de, p=p, factor=0.1)
# loss = global_cosine(en, de)
optimizer.zero_grad()
loss.backward()
nn.utils.clip_grad_norm(trainable.parameters(), max_norm=0.1)
optimizer.step()
loss_list.append(loss.item())
lr_scheduler.step()
if (it + 1) % 5000 == 0:
# torch.save(model.state_dict(), os.path.join(args.save_dir, args.save_name, 'model.pth'))
auroc_sp_list, ap_sp_list, f1_sp_list = [], [], []
auroc_px_list, ap_px_list, f1_px_list, aupro_px_list = [], [], [], []
for item, test_data in zip(item_list, test_data_list):
test_dataloader = torch.utils.data.DataLoader(test_data, batch_size=batch_size, shuffle=False,
num_workers=4)
results = evaluation_batch(model, test_dataloader, device, max_ratio=0.01, resize_mask=256)
auroc_sp, ap_sp, f1_sp, auroc_px, ap_px, f1_px, aupro_px = results
auroc_sp_list.append(auroc_sp)
ap_sp_list.append(ap_sp)
f1_sp_list.append(f1_sp)
auroc_px_list.append(auroc_px)
ap_px_list.append(ap_px)
f1_px_list.append(f1_px)
aupro_px_list.append(aupro_px)
print_fn(
'{}: I-Auroc:{:.4f}, I-AP:{:.4f}, I-F1:{:.4f}, P-AUROC:{:.4f}, P-AP:{:.4f}, P-F1:{:.4f}, P-AUPRO:{:.4f}'.format(
item, auroc_sp, ap_sp, f1_sp, auroc_px, ap_px, f1_px, aupro_px))
print_fn(
'Mean: I-Auroc:{:.4f}, I-AP:{:.4f}, I-F1:{:.4f}, P-AUROC:{:.4f}, P-AP:{:.4f}, P-F1:{:.4f}, P-AUPRO:{:.4f}'.format(
np.mean(auroc_sp_list), np.mean(ap_sp_list), np.mean(f1_sp_list),
np.mean(auroc_px_list), np.mean(ap_px_list), np.mean(f1_px_list), np.mean(aupro_px_list)))
model.train()
it += 1
if it == total_iters:
break
if (it + 1) % 100 == 0:
print_fn('iter [{}/{}], loss:{:.4f}'.format(it, total_iters, np.mean(loss_list)))
loss_list = []
# torch.save(model.state_dict(), os.path.join(args.save_dir, args.save_name, 'model.pth'))
return
if __name__ == '__main__':
os.environ['CUDA_LAUNCH_BLOCKING'] = "1"
import argparse
parser = argparse.ArgumentParser(description='')
parser.add_argument('--data_path', type=str, default='../VisA_pytorch/1cls')
parser.add_argument('--save_dir', type=str, default='./saved_results')
parser.add_argument('--save_name', type=str,
default='vitill_visa_uni_dinov2br_c392r_en29_bn4dp2_de8_laelu_md2_i1_it10k_sams2e3_wd1e4_w1hcosa_ghmp09f01w01_b16_ev_s1')
args = parser.parse_args()
# vitill_visa_uni_dinov2br_c392r_en29_bn4dp2_de8_elaelu_md2_i1_it10k_sams2e3_wd1e4_w1hcosa_ghmp09f01w01_adev_b16_s1
item_list = ['candle', 'capsules', 'cashew', 'chewinggum', 'fryum', 'macaroni1', 'macaroni2',
'pcb1', 'pcb2', 'pcb3', 'pcb4', 'pipe_fryum']
logger = get_logger(args.save_name, os.path.join(args.save_dir, args.save_name))
print_fn = logger.info
device = 'cuda:1' if torch.cuda.is_available() else 'cpu'
print_fn(device)
train(item_list)