forked from nmndeep/revisiting-at
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rb_architecture_util.py
211 lines (171 loc) · 8.44 KB
/
rb_architecture_util.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
'''All the submitted to RBench are defined here.
Mostly use timm but have some custom implementation: ConvStem (ConvBlock)
'''
import torch
import torch.nn as nn
from collections import OrderedDict
from typing import Tuple
from torch import Tensor
import torch.nn as nn
import timm
from timm.models import create_model
import torch.nn.functional as F
import math
IMAGENET_MEAN = [c * 1. for c in (0.485, 0.456, 0.406)]
IMAGENET_STD = [c * 1. for c in (0.229, 0.224, 0.225)]
class LayerNorm(nn.Module):
r""" LayerNorm that supports two data formats: channels_last (default) or channels_first.
The ordering of the dimensions in the inputs. channels_last corresponds to inputs with
shape (batch_size, height, width, channels) while channels_first corresponds to inputs
with shape (batch_size, channels, height, width).
"""
def __init__(self, normalized_shape, eps=1e-6, data_format="channels_first"):
super().__init__()
self.weight = nn.Parameter(torch.ones(normalized_shape))
self.bias = nn.Parameter(torch.zeros(normalized_shape))
self.eps = eps
self.data_format = data_format
if self.data_format not in ["channels_last", "channels_first"]:
raise NotImplementedError
self.normalized_shape = (normalized_shape, )
def forward(self, x):
if self.data_format == "channels_last":
return F.layer_norm(x, self.normalized_shape, self.weight, self.bias, self.eps)
elif self.data_format == "channels_first":
u = x.mean(1, keepdim=True)
s = (x - u).pow(2).mean(1, keepdim=True)
x = (x - u) / torch.sqrt(s + self.eps)
x = self.weight[:, None, None] * x + self.bias[:, None, None]
return x
class ImageNormalizer(nn.Module):
'''ADD normalization as a first layer in the models, as AA uses un-normalized inputs.'''
def __init__(self, persistent: bool = True) -> None:
super(ImageNormalizer, self).__init__()
self.register_buffer('mean', torch.as_tensor(MEAN).view(1, 3, 1, 1),
persistent=persistent)
self.register_buffer('std', torch.as_tensor(STD).view(1, 3, 1, 1),
persistent=persistent)
def forward(self, input: Tensor) -> Tensor:
return (input - self.mean) / self.std
def normalize_model(model: nn.Module) -> nn.Module:
layers = OrderedDict([
('normalize', ImageNormalizer()),
('model', model)
])
return nn.Sequential(layers)
def get_transforms(img_size=224):
'''returns torch-transform as a callable for RobustBench. Used when testing for increased resolution models.'''
crop_pct = 0.875
scale_size = int(math.floor(img_size / crop_pct))
trans = transforms.Compose([
transforms.Resize(
scale_size,
interpolation=transforms.InterpolationMode("bicubic")),
transforms.CenterCrop(img_size),
transforms.ToTensor()
])
return trans
class ConvBlock(nn.Module):
expansion = 1
def __init__(self, siz=48, end_siz=8, fin_dim=384):
super(ConvBlock, self).__init__()
self.planes = siz
fin_dim = self.planes*end_siz if fin_dim != 432 else 432
# self.bn = nn.BatchNorm2d(planes) if self.normaliz == "bn" else nn.GroupNorm(num_groups=1, num_channels=planes)
self.stem = nn.Sequential(nn.Conv2d(3, self.planes, kernel_size=3, stride=2, padding=1),
LayerNorm(self.planes, data_format="channels_first"),
nn.GELU(),
nn.Conv2d(self.planes, self.planes*2, kernel_size=3, stride=2, padding=1),
LayerNorm(self.planes*2, data_format="channels_first"),
nn.GELU(),
nn.Conv2d(self.planes*2, self.planes*4, kernel_size=3, stride=2, padding=1),
LayerNorm(self.planes*4, data_format="channels_first"),
nn.GELU(),
nn.Conv2d(self.planes*4, self.planes*8, kernel_size=3, stride=2, padding=1),
LayerNorm(self.planes*8, data_format="channels_first"),
nn.GELU(),
nn.Conv2d(self.planes*8, fin_dim, kernel_size=1, stride=1, padding=0)
)
def forward(self, x):
out = self.stem(x)
# out = self.bn(out)
return out
class ConvBlock3(nn.Module):
# expansion = 1
def __init__(self, siz=64):
super(ConvBlock3, self).__init__()
self.planes = siz
self.stem = nn.Sequential(nn.Conv2d(3, self.planes, kernel_size=3, stride=2, padding=1),
LayerNorm(self.planes, data_format="channels_first"),
nn.GELU(),
nn.Conv2d(self.planes, int(self.planes*1.5), kernel_size=3, stride=2, padding=1),
LayerNorm(int(self.planes*1.5), data_format="channels_first"),
nn.GELU(),
nn.Conv2d(int(self.planes*1.5), self.planes*2, kernel_size=3, stride=1, padding=1),
LayerNorm(self.planes*2, data_format="channels_first"),
nn.GELU()
)
def forward(self, x):
out = self.stem(x)
# out = self.bn(out)
return out
class ConvBlock1(nn.Module):
def __init__(self, siz=48, end_siz=8, fin_dim=384):
super(ConvBlock1, self).__init__()
self.planes = siz
fin_dim = self.planes*end_siz if fin_dim == None else 432
self.stem = nn.Sequential(nn.Conv2d(3, self.planes, kernel_size=3, stride=2, padding=1),
LayerNorm(self.planes, data_format="channels_first"),
nn.GELU(),
nn.Conv2d(self.planes, self.planes*2, kernel_size=3, stride=2, padding=1),
LayerNorm(self.planes*2, data_format="channels_first"),
nn.GELU()
)
def forward(self, x):
out = self.stem(x)
# out = self.bn(out)
return out
class IdentityLayer(nn.Module):
def forward(self, inputs):
return inputs
def get_new_model(modelname, pretrained=False, not_original=True):
if modelname == 'convnext_t_cvst':
model = timm.models.convnext.convnext_tiny(pretrained=pretrained)
model.stem = ConvBlock1(48, end_siz=8)
elif modelname == "convnext_s_cvst":
model = timm.models.convnext.convnext_small(pretrained=pretrained)
model.stem = ConvBlock1(48, end_siz=8)
elif modelname == "convnext_b_cvst":
model_args = dict(depths=[3, 3, 27, 3], dims=[128, 256, 512, 1024])
model = timm.models.convnext._create_convnext('convnext_base.fb_in1k', pretrained=pretrained, **model_args)
model.stem = ConvBlock3(64)
elif modelname == "convnext_l_cvst":
model = timm.models.convnext_large(pretrained=pretrained)
model.stem = ConvBlock3(96)
elif modelname == 'vit_s_cvst':
model = create_model('deit_small_patch16_224', pretrained=pretrained)
model.patch_embed.proj = ConvBlock(48, end_siz=8)
model = normalize_model(model)
elif modelname == 'vit_b_cvst':
model = timm.models.vision_transformer.vit_base_patch16_224(pretrained=pretrained)
model.patch_embed.proj = ConvBlock(48, end_siz=16, fin_dim=None)
else:
logger.error('Invalid model name, please use either cait, deit, swin, vit, effnet, or rn50')
sys.exit(1)
return model
def load_model(arch, not_original, chkpt_path):
''''
Load the model with definition from the checkpoint
arch: architecture name
not_original: If True -> CvSt
chkpt_path: location of checkpoint
'''
model = get_new_model(arch, pretrained=False, not_original=not_original)
ckpt = torch.load(chkpt_path, map_location='cpu')
ckpt = {k.replace('module.', ''): v for k, v in ckpt.items()}
ckpt = {k.replace('base_model.', ''): v for k, v in ckpt.items()}
ckpt = {k.replace('se_', 'se_module.'): v for k, v in ckpt.items()}
model.load_state_dict(ckpt)
model = model.to(device)
model.eval()
return model