-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharchitectures.py
732 lines (622 loc) · 22.6 KB
/
architectures.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
import math
import PIL
import torch.nn as nn
import torch.nn.functional as F
import torch
import numpy as np
import itertools
from tllib.modules import Classifier as ClassifierBase
from torchvision import transforms
from timm.data import create_transform
from timm.data.constants import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD
from timm.models.vision_transformer import PatchEmbed
from timm.models.layers import DropPath
from functools import partial
from collections import OrderedDict
import math
import typing
from typing import List
class MLP(nn.Module):
def __init__(self, input_dim, output_dim, n_layers=1, hidden_dim=1024):
super(MLP, self).__init__()
model = []
model += [nn.Linear(input_dim, hidden_dim), nn.ReLU()]
for _ in range(n_layers):
model += [nn.Linear(hidden_dim, hidden_dim), nn.ReLU()]
model += [nn.Linear(hidden_dim, output_dim)]
self.model = nn.Sequential(*model)
def forward(self, x):
return self.model(x)
# -------------- ViT and Adaptors ------------------
class Adapter(nn.Module):
def __init__(
self,
config=None,
d_model=None,
bottleneck=None,
dropout=0.0,
init_option="bert",
adapter_scalar="1.0",
adapter_layernorm_option="in",
):
super().__init__()
self.n_embd = config.d_model if d_model is None else d_model
self.down_size = config.attn_bn if bottleneck is None else bottleneck
# _before
self.adapter_layernorm_option = adapter_layernorm_option
self.adapter_layer_norm_before = None
if adapter_layernorm_option == "in" or adapter_layernorm_option == "out":
self.adapter_layer_norm_before = nn.LayerNorm(self.n_embd)
if adapter_scalar == "learnable_scalar":
self.scale = nn.Parameter(torch.ones(1))
else:
self.scale = float(adapter_scalar)
self.down_proj = nn.Linear(self.n_embd, self.down_size)
self.non_linear_func = nn.ReLU()
self.up_proj = nn.Linear(self.down_size, self.n_embd)
self.dropout = dropout
if init_option == "bert":
raise NotImplementedError
elif init_option == "lora":
with torch.no_grad():
nn.init.kaiming_uniform_(self.down_proj.weight, a=math.sqrt(5))
nn.init.zeros_(self.up_proj.weight)
nn.init.zeros_(self.down_proj.bias)
nn.init.zeros_(self.up_proj.bias)
def forward(self, x, add_residual=True, residual=None):
residual = x if residual is None else residual
if self.adapter_layernorm_option == "in":
x = self.adapter_layer_norm_before(x)
down = self.down_proj(x)
down = self.non_linear_func(down)
down = nn.functional.dropout(down, p=self.dropout, training=self.training)
up = self.up_proj(down)
up = up * self.scale
if self.adapter_layernorm_option == "out":
up = self.adapter_layer_norm_before(up)
if add_residual:
output = up + residual
else:
output = up
return output
class Attention(nn.Module):
def __init__(
self,
dim,
num_heads=8,
qkv_bias=False,
attn_drop=0.0,
proj_drop=0.0,
):
super().__init__()
self.num_heads = num_heads
head_dim = dim // num_heads
self.head_dim = dim // num_heads
self.scale = head_dim**-0.5
self.q_proj = nn.Linear(dim, dim, bias=qkv_bias)
self.v_proj = nn.Linear(dim, dim, bias=qkv_bias)
self.k_proj = nn.Linear(dim, dim, bias=qkv_bias)
self.attn_drop = nn.Dropout(attn_drop)
self.proj = nn.Linear(dim, dim)
self.proj_drop = nn.Dropout(proj_drop)
def _shape(self, tensor: torch.Tensor, seq_len: int, bsz: int):
return (
tensor.view(bsz, seq_len, self.num_heads, self.head_dim)
.transpose(1, 2)
.contiguous()
)
def forward(self, x):
B, N, C = x.shape
q = self.q_proj(x)
k = self._shape(self.k_proj(x), -1, B).view(
B * self.num_heads, -1, self.head_dim
)
v = self._shape(self.v_proj(x), -1, B).view(
B * self.num_heads, -1, self.head_dim
)
q = self._shape(q, N, B).view(B * self.num_heads, -1, self.head_dim)
# attn = (q @ k.transpose(-2, -1)) * self.scale
attn_weights = torch.bmm(q, k.transpose(1, 2)) * self.scale
attn_weights = nn.functional.softmax(attn_weights, dim=-1)
attn_probs = self.attn_drop(attn_weights)
attn_output = torch.bmm(attn_probs, v)
attn_output = attn_output.view(B, self.num_heads, N, self.head_dim)
attn_output = attn_output.transpose(1, 2)
attn_output = attn_output.reshape(B, N, C)
x = self.proj(attn_output)
x = self.proj_drop(x)
return x
class Block(nn.Module):
def __init__(
self,
dim,
num_heads,
mlp_ratio=4.0,
qkv_bias=False,
drop=0.0,
attn_drop=0.0,
drop_path=0.0,
act_layer=nn.GELU,
norm_layer=nn.LayerNorm,
config=None,
layer_id=None,
):
super().__init__()
self.config = config
self.norm1 = norm_layer(dim)
self.attn = Attention(
dim,
num_heads=num_heads,
qkv_bias=qkv_bias,
attn_drop=attn_drop,
proj_drop=drop,
)
# NOTE: drop path for stochastic depth, we shall see if this is better than dropout here
self.drop_path = DropPath(drop_path) if drop_path > 0.0 else nn.Identity()
self.norm2 = norm_layer(dim)
mlp_hidden_dim = int(dim * mlp_ratio)
self.fc1 = nn.Linear(dim, mlp_hidden_dim)
self.fc2 = nn.Linear(mlp_hidden_dim, dim)
self.act = act_layer()
self.mlp_drop = nn.Dropout(drop)
if config.ffn_adapt:
self.adaptmlp = Adapter(
self.config,
dropout=0.1,
bottleneck=config.ffn_num,
init_option=config.ffn_adapter_init_option,
adapter_scalar=config.ffn_adapter_scalar,
adapter_layernorm_option=config.ffn_adapter_layernorm_option,
)
def forward(self, x):
x = x + self.drop_path(self.attn(self.norm1(x)))
if self.config.ffn_adapt and self.config.ffn_option == "parallel":
adapt_x = self.adaptmlp(x, add_residual=False)
residual = x
x = self.mlp_drop(self.act(self.fc1(self.norm2(x))))
x = self.drop_path(self.mlp_drop(self.fc2(x)))
if self.config.ffn_adapt:
if self.config.ffn_option == "sequential":
x = self.adaptmlp(x)
elif self.config.ffn_option == "parallel":
x = x + adapt_x
else:
raise ValueError(self.config.ffn_adapt)
x = residual + x
return x
class VisionTransformer(nn.Module):
"""Vision Transformer with support for global average pooling"""
def __init__(
self,
global_pool=False,
img_size=224,
patch_size=16,
in_chans=3,
num_classes=1000,
embed_dim=768,
depth=12,
num_heads=12,
mlp_ratio=4.0,
qkv_bias=True,
representation_size=None,
distilled=False,
drop_rate=0.0,
attn_drop_rate=0.0,
drop_path_rate=0.0,
embed_layer=PatchEmbed,
norm_layer=None,
act_layer=None,
weight_init="",
tuning_config=None,
):
super().__init__()
self.tuning_config = tuning_config
self.num_classes = num_classes
self.num_features = self.embed_dim = (
embed_dim # num_features for consistency with other models
)
self.num_tokens = 2 if distilled else 1
norm_layer = norm_layer or partial(nn.LayerNorm, eps=1e-6)
act_layer = act_layer or nn.GELU
self.patch_embed = embed_layer(
img_size=img_size,
patch_size=patch_size,
in_chans=in_chans,
embed_dim=embed_dim,
)
num_patches = self.patch_embed.num_patches
self.cls_token = nn.Parameter(torch.zeros(1, 1, embed_dim))
self.dist_token = (
nn.Parameter(torch.zeros(1, 1, embed_dim)) if distilled else None
)
self.pos_embed = nn.Parameter(
torch.zeros(1, num_patches + self.num_tokens, embed_dim)
)
self.pos_drop = nn.Dropout(p=drop_rate)
dpr = [
x.item() for x in torch.linspace(0, drop_path_rate, depth)
] # stochastic depth decay rule
self.blocks = nn.Sequential(
*[
Block(
dim=embed_dim,
num_heads=num_heads,
mlp_ratio=mlp_ratio,
qkv_bias=qkv_bias,
drop=drop_rate,
attn_drop=attn_drop_rate,
drop_path=dpr[i],
norm_layer=norm_layer,
act_layer=act_layer,
config=tuning_config,
layer_id=i,
)
for i in range(depth)
]
)
self.norm = norm_layer(embed_dim)
# Representation layer
if representation_size and not distilled:
self.num_features = representation_size
self.pre_logits = nn.Sequential(
OrderedDict(
[
("fc", nn.Linear(embed_dim, representation_size)),
("act", nn.Tanh()),
]
)
)
else:
self.pre_logits = nn.Identity()
# Classifier head(s)
self.head = (
nn.Linear(self.num_features, num_classes)
if num_classes > 0
else nn.Identity()
)
self.head_dist = None
if distilled:
self.head_dist = (
nn.Linear(self.embed_dim, self.num_classes)
if num_classes > 0
else nn.Identity()
)
######### MAE begins ############
self.global_pool = global_pool
if self.global_pool:
self.fc_norm = norm_layer(embed_dim)
del self.norm # remove the original norm
######## Adapter begins #########
if tuning_config.vpt_on:
assert tuning_config.vpt_num > 0, tuning_config.vpt_num
# properly registered
self.embeddings = nn.ParameterList( # batch, num_prompt, embed_dim
[
nn.Parameter(torch.empty(1, self.tuning_config.vpt_num, embed_dim))
for _ in range(depth)
]
)
for eee in self.embeddings:
torch.nn.init.xavier_uniform_(eee.data)
def init_weights(self, mode=""):
raise NotImplementedError()
def get_parameters(self, base_lr=1.0, backbone_lr_ratio=0):
"""A parameter list which decides optimization hyper-parameters,
such as the relative learning rate of each layer
"""
param_list_backbone = []
param_list_base = []
for name, p in self.named_parameters():
if "head" in name or "up_proj" in name or "down_proj" in name:
param_list_base.append(p)
else:
param_list_backbone.append(p)
base_params = itertools.chain(param_list_base)
backbone_params = itertools.chain(param_list_backbone)
params = [
{"params": backbone_params, "lr": backbone_lr_ratio * base_lr},
{"params": base_params, "lr": base_lr},
]
return params
@torch.jit.ignore
def no_weight_decay(self):
return {"pos_embed", "cls_token", "dist_token"}
def get_classifier(self):
if self.dist_token is None:
return self.head
else:
return self.head, self.head_dist
def reset_classifier(self, num_classes, global_pool=""):
self.num_classes = num_classes
self.head = (
nn.Linear(self.embed_dim, num_classes) if num_classes > 0 else nn.Identity()
)
if self.num_tokens == 2:
self.head_dist = (
nn.Linear(self.embed_dim, self.num_classes)
if num_classes > 0
else nn.Identity()
)
def forward_features(self, x):
B = x.shape[0]
x = self.patch_embed(x)
cls_tokens = self.cls_token.expand(
B, -1, -1
) # stole cls_tokens impl from Phil Wang, thanks
x = torch.cat((cls_tokens, x), dim=1)
x = x + self.pos_embed
x = self.pos_drop(x)
for idx, blk in enumerate(self.blocks):
if self.tuning_config.vpt_on:
eee = self.embeddings[idx].expand(B, -1, -1)
x = torch.cat([eee, x], dim=1)
x = blk(x)
if self.tuning_config.vpt_on:
x = x[:, self.tuning_config.vpt_num :, :]
if self.global_pool:
x = x[:, 1:, :].mean(dim=1) # global pool without cls token
outcome = self.fc_norm(x)
else:
x = self.norm(x)
outcome = x[:, 0]
return outcome
def forward(self, x):
x = self.forward_features(
x,
)
if self.head_dist is not None:
x, x_dist = self.head(x[0]), self.head_dist(x[1]) # x must be a tuple
if self.training and not torch.jit.is_scripting():
# during inference, return the average of both classifier predictions
return x, x_dist
else:
return (x + x_dist) / 2
else:
pen_x = x.clone()
x = self.head(x)
if self.training:
return x, pen_x # idk why
return x
def vit_base_patch16(**kwargs):
model = VisionTransformer(
patch_size=16,
embed_dim=768,
depth=12,
num_heads=12,
mlp_ratio=4,
qkv_bias=True,
norm_layer=partial(nn.LayerNorm, eps=1e-6),
**kwargs,
)
return model
# ------------- utils for ViTs -----------------------
class NativeScalerWithGradNormCount:
state_dict_key = "amp_scaler"
def __init__(self):
self._scaler = torch.cuda.amp.GradScaler()
def __call__(
self,
loss,
optimizer,
clip_grad=None,
parameters=None,
create_graph=False,
update_grad=True,
):
self._scaler.scale(loss).backward(create_graph=create_graph)
if update_grad:
if clip_grad is not None:
assert parameters is not None
self._scaler.unscale_(
optimizer
) # unscale the gradients of optimizer's assigned params in-place
norm = torch.nn.utils.clip_grad_norm_(parameters, clip_grad)
else:
self._scaler.unscale_(optimizer)
norm = get_grad_norm_(parameters)
self._scaler.step(optimizer)
self._scaler.update()
else:
norm = None
return norm
def state_dict(self):
return self._scaler.state_dict()
def load_state_dict(self, state_dict):
self._scaler.load_state_dict(state_dict)
def get_grad_norm_(parameters, norm_type: float = 2.0) -> torch.Tensor:
if isinstance(parameters, torch.Tensor):
parameters = [parameters]
parameters = [p for p in parameters if p.grad is not None]
norm_type = float(norm_type)
if len(parameters) == 0:
return torch.tensor(0.0)
device = parameters[0].grad.device
if norm_type == np.inf:
total_norm = max(p.grad.detach().abs().max().to(device) for p in parameters)
else:
total_norm = torch.norm(
torch.stack(
[torch.norm(p.grad.detach(), norm_type).to(device) for p in parameters]
),
norm_type,
)
return total_norm
class WarmupCosineScheduler:
def __init__(self, optimizer, warmup_epochs, min_lr, T_max, lr):
self.optimizer = optimizer
self.warmup_epochs = warmup_epochs
self.min_lr = min_lr
self.T_max = T_max
self.lr = lr
def get_lr(self):
return [param_group["lr"] for param_group in self.optimizer.param_groups]
def step(self, epoch):
"""Decay the learning rate with half-cycle cosine after warmup"""
if epoch < self.warmup_epochs:
lr = self.lr * epoch / self.warmup_epochs
else:
lr = self.min_lr + (self.lr - self.min_lr) * 0.5 * (
1.0
+ math.cos(
math.pi
* (epoch - self.warmup_epochs)
/ (self.T_max - self.warmup_epochs)
)
)
for param_group in self.optimizer.param_groups:
if "lr_scale" in param_group:
param_group["lr"] = lr * param_group["lr_scale"]
else:
param_group["lr"] = lr
def param_groups_lrd(
model, weight_decay=0.05, no_weight_decay_list=[], layer_decay=0.75
):
"""
Parameter groups for layer-wise lr decay
Following BEiT: https://github.com/microsoft/unilm/blob/master/beit/optim_factory.py#L58
"""
param_group_names = {}
param_groups = {}
num_layers = len(model.blocks) + 1
layer_scales = list(layer_decay ** (num_layers - i) for i in range(num_layers + 1))
for n, p in model.named_parameters():
if not p.requires_grad:
continue
# no decay: all 1D parameters and model specific ones
if p.ndim == 1 or n in no_weight_decay_list:
g_decay = "no_decay"
this_decay = 0.0
else:
g_decay = "decay"
this_decay = weight_decay
layer_id = get_layer_id_for_vit(n, num_layers)
group_name = "layer_%d_%s" % (layer_id, g_decay)
if group_name not in param_group_names:
this_scale = layer_scales[layer_id]
param_group_names[group_name] = {
"lr_scale": this_scale,
"weight_decay": this_decay,
"params": [],
}
param_groups[group_name] = {
"lr_scale": this_scale,
"weight_decay": this_decay,
"params": [],
}
param_group_names[group_name]["params"].append(n)
param_groups[group_name]["params"].append(p)
# print("parameter groups: \n%s" % json.dumps(param_group_names, indent=2))
return list(param_groups.values())
def get_layer_id_for_vit(name, num_layers):
"""
Assign a parameter with its layer id
Following BEiT: https://github.com/microsoft/unilm/blob/master/beit/optim_factory.py#L33
"""
if name in ["cls_token", "pos_embed"]:
return 0
elif name.startswith("patch_embed"):
return 0
elif name.startswith("blocks"):
return int(name.split(".")[1]) + 1
else:
return num_layers
def build_transform(is_train, args):
mean = IMAGENET_DEFAULT_MEAN
std = IMAGENET_DEFAULT_STD
# train transform
if is_train:
# this should always dispatch to transforms_imagenet_train
transform = create_transform(
input_size=args.input_size,
is_training=True,
color_jitter=args.color_jitter,
auto_augment=args.aa,
interpolation="bicubic",
re_prob=args.reprob,
re_mode=args.remode,
re_count=args.recount,
mean=mean,
std=std,
)
return transform
# eval transform
t = []
if args.input_size <= 224:
crop_pct = 224 / 256
else:
crop_pct = 1.0
size = int(args.input_size / crop_pct)
t.append(
transforms.Resize(
size, interpolation=PIL.Image.BICUBIC
), # to maintain same ratio w.r.t. 224 images
)
t.append(transforms.CenterCrop(args.input_size))
t.append(transforms.ToTensor())
t.append(transforms.Normalize(mean, std))
return transforms.Compose(t)
# -------------- Image Classifier ------------------
class ImageClassifier(ClassifierBase):
"""ImageClassifier specific for reproducing results of `DomainBed <https://github.com/facebookresearch/DomainBed>`_.
You are free to freeze all `BatchNorm2d` layers and insert one additional `Dropout` layer, this can achieve better
results for some datasets like PACS but may be worse for others.
Args:
backbone (torch.nn.Module): Any backbone to extract features from data
num_classes (int): Number of classes
freeze_bn (bool, optional): whether to freeze all `BatchNorm2d` layers. Default: False
dropout_p (float, optional): dropout ratio for additional `Dropout` layer, this layer is only used when `freeze_bn` is True. Default: 0.1
"""
def __init__(
self,
backbone: nn.Module,
num_classes: int,
freeze_bn=False,
dropout_p=0.1,
**kwargs,
):
super(ImageClassifier, self).__init__(backbone, num_classes, **kwargs)
self.freeze_bn = freeze_bn
if freeze_bn:
self.feature_dropout = nn.Dropout(p=dropout_p)
def forward(self, x: torch.Tensor):
f = self.pool_layer(self.backbone(x))
f = self.bottleneck(f)
if self.freeze_bn:
f = self.feature_dropout(f)
predictions = self.head(f)
if self.training:
return predictions, f
else:
return predictions
def train(self, mode=True):
super(ImageClassifier, self).train(mode)
if self.freeze_bn:
for m in self.modules():
if isinstance(m, nn.BatchNorm2d):
m.eval()
def get_parameters(
self, base_lr=1.0, backbone_lr_ratio=0.1
) -> typing.List[typing.Dict]:
"""A parameter list which decides optimization hyper-parameters,
such as the relative learning rate of each layer
"""
params = [
{"params": self.backbone.parameters(), "lr": backbone_lr_ratio * base_lr},
{"params": self.bottleneck.parameters(), "lr": base_lr},
{"params": self.head.parameters(), "lr": base_lr},
]
return params
# Prior Predictor
class PriorPredictor(nn.Module):
def __init__(self, num_classes):
super(PriorPredictor, self).__init__()
self.fc1 = nn.Linear(num_classes, 256)
self.fc2 = nn.Linear(256, 256)
self.fc3 = nn.Linear(256, num_classes)
def forward(self, x):
out = self.fc1(x)
out = F.relu(out)
out = self.fc2(out)
out = F.relu(out)
out = self.fc3(out)
return out