-
Notifications
You must be signed in to change notification settings - Fork 0
/
dinomaly_mvtec_sep.py
232 lines (181 loc) · 8.62 KB
/
dinomaly_mvtec_sep.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
# 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 dinov1.utils 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, global_cosine, replace_layers, global_cosine_hm_percent, 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")
class BatchNorm1d(nn.BatchNorm1d):
def forward(self, x):
x = x.permute(0, 2, 1)
x = super(BatchNorm1d, self).forward(x)
x = x.permute(0, 2, 1)
return x
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):
setup_seed(1)
print_fn(item)
total_iters = 5000
batch_size = 16
image_size = 448
crop_size = 392
data_transform, gt_transform = get_data_transforms(image_size, crop_size)
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)
test_data = MVTecDataset(root=test_path, transform=data_transform, gt_transform=gt_transform, phase="test")
train_dataloader = torch.utils.data.DataLoader(train_data, batch_size=batch_size, shuffle=True, num_workers=4,
drop_last=True)
test_dataloader = torch.utils.data.DataLoader(test_data, batch_size=batch_size, shuffle=False, num_workers=4)
# 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]]
# target_layers = list(range(4, 19))
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-8)
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)
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:
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
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))
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 auroc_sp, ap_sp, f1_sp, auroc_px, ap_px, f1_px, aupro_px
if __name__ == '__main__':
os.environ['CUDA_LAUNCH_BLOCKING'] = "1"
import argparse
parser = argparse.ArgumentParser(description='')
parser.add_argument('--data_path', type=str, default='../mvtec_anomaly_detection')
parser.add_argument('--save_dir', type=str, default='./saved_results')
parser.add_argument('--save_name', type=str,
default='vitill_mvtec_sep_dinov2br_c392_en29_bn4dp2_de8_elaelu_md2_i1_it10k_sadm2e3_wd1e4_w1hcosa_ghmp09f01w1k_b16_ev_s1')
args = parser.parse_args()
item_list = ['carpet', 'grid', 'leather', 'tile', 'wood', 'bottle', 'cable', 'capsule',
'hazelnut', 'metal_nut', 'pill', 'screw', 'toothbrush', 'transistor', 'zipper']
# item_list = ['leather']
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)
result_list = []
for i, item in enumerate(item_list):
auroc_sp, ap_sp, f1_sp, auroc_px, ap_px, f1_px, aupro_px = train(item)
result_list.append([item, auroc_sp, ap_sp, f1_sp, auroc_px, ap_px, f1_px, aupro_px])
mean_auroc_sp = np.mean([result[1] for result in result_list])
mean_ap_sp = np.mean([result[2] for result in result_list])
mean_f1_sp = np.mean([result[3] for result in result_list])
mean_auroc_px = np.mean([result[4] for result in result_list])
mean_ap_px = np.mean([result[5] for result in result_list])
mean_f1_px = np.mean([result[6] for result in result_list])
mean_aupro_px = np.mean([result[7] for result in result_list])
print_fn(result_list)
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(
mean_auroc_sp, mean_ap_sp, mean_f1_sp,
mean_auroc_px, mean_ap_px, mean_f1_px, mean_aupro_px))