-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils_architecture.py
322 lines (258 loc) · 13 KB
/
utils_architecture.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
'''All the models are defined here.
Custom implementation: ConvStem (ConvBlock) with standard models built on timm.
'''
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 functools import partial
from timm.models import create_model
from timm.models.convnext import _create_convnext as CNXT
import torch.nn.functional as F
from functools import partial
import math
from timm.models.vision_transformer import VisionTransformer
def interpolate_pos_encoding(
pos_embed: Tensor,
new_img_size: int,
old_img_size: int = 224,
patch_size: int = 16) -> Tensor:
"""Interpolates the positional encoding of ViTs for new image resolution
(adapted from https://github.com/facebookresearch/dino/blob/main/vision_transformer.py#L174).
It currently handles only square images.
"""
N = pos_embed.shape[1] - 1
npatch = (new_img_size // patch_size) ** 2
w, h = new_img_size, new_img_size
if npatch == N and w == h:
print(f'Positional encoding not changed.')
return pos_embed
print(f'Interpolating positional encoding from {N} to {npatch} patches (size={patch_size}).')
class_pos_embed = pos_embed[:, 0]
patch_pos_embed = pos_embed[:, 1:]
dim = pos_embed.shape[-1]
w0 = w // patch_size
h0 = h // patch_size
# we add a small number to avoid floating point error in the interpolation
# see discussion at https://github.com/facebookresearch/dino/issues/8
w0, h0 = w0 + 0.1, h0 + 0.1
patch_pos_embed = nn.functional.interpolate(
patch_pos_embed.reshape(1, int(math.sqrt(N)), int(math.sqrt(N)), dim).permute(0, 3, 1, 2),
scale_factor=(w0 / math.sqrt(N), h0 / math.sqrt(N)),
mode='bicubic',
)
assert int(w0) == patch_pos_embed.shape[-2] and int(h0) == patch_pos_embed.shape[-1]
patch_pos_embed = patch_pos_embed.permute(0, 2, 3, 1).view(1, -1, dim)
return torch.cat((class_pos_embed.unsqueeze(0), patch_pos_embed), dim=1)
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):
def __init__(self, mean: Tuple[float, float, float],
std: Tuple[float, float, float],
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 timm_gelu(inplace):
return nn.GELU()
def convert_relu_to_gelu(model):
for child_name, child in model.named_children():
if isinstance(child, nn.ReLU):
setattr(model, child_name, nn.GELU())
else:
convert_relu_to_gelu(child)
def normalize_model(model: nn.Module, mean: Tuple[float, float, float],
std: Tuple[float, float, float]) -> nn.Module:
layers = OrderedDict([
('normalize', ImageNormalizer(mean, std)),
('model', model)
])
return nn.Sequential(layers)
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 ConvBlock2(nn.Module):
"""Used only for det-medium"""
expansion = 1
def __init__(self, siz=48, end_siz=8, fin_dim=384):
super(ConvBlock2, 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, 512, 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):
# expansion = 1
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=True, not_original=False, updated=False):
if modelname == 'resnet50':
model = timm.models.resnet.resnet50(pretrained=pretrained)
elif modelname == 'resnet50_gelu':
model = timm.models.resnet.resnet50(pretrained=pretrained,
act_layer=timm_gelu)
# elif modelname == 'convnext_iso':
# model = cnxt_iso.convnext_isotropic_small(pretrained=pretrained, dim=384, depth=18)
# if not_original:
# setattr(model, 'stem', ConvBlock(48, end_siz=8, fin_dim=432 if updated else 384))
elif modelname == 'convnext_tiny':
model = timm.models.convnext.convnext_tiny(pretrained=pretrained)
if not_original:
model.stem = ConvBlock1(48, end_siz=8)
elif modelname == "convnext_tiny_21k":
model = timm.models.convnext._create_convnext('convnext_tiny.fb_in22k_ft_in1k', pretrained=pretrained)
elif modelname == "convnext_small":
model_args = dict(depths=[3, 3, 27, 3], dims=[96, 192, 384, 768])
model = timm.models.convnext.convnext_small(pretrained=pretrained)
if not_original:
## only for removing patch-stem
model.stem = ConvBlock1(48, end_siz=8)
elif modelname == "convnext_base":
model_args = dict(depths=[3, 3, 27, 3], dims=[128, 256, 512, 1024])
# model = timm.models.create_model('convnext_base', pretrained=pretrained, pretrained_cfg='convnext_base.fb_in1k')
model = timm.models.convnext._create_convnext('convnext_base.fb_in1k', pretrained=pretrained, **model_args)
if not_original:
## only for removing patch-stem
model.stem = ConvBlock3(64)
elif modelname == "convnext_large":
model = timm.models.convnext_large(pretrained=pretrained)
if not_original:
model.stem = ConvBlock3(96)
elif modelname == 'vit_s':
model = create_model('vit_small_patch16_224', pretrained=pretrained)
if not_original:
## only for removing patch-stem
model.patch_embed.proj = ConvBlock(48, end_siz=8)
elif modelname == 'deit_s':
from timm.models.deit import deit3_small_patch16_224, _create_deit
model_kwargs = dict(
patch_size=16, embed_dim=384, depth=12, num_heads=6, no_embed_class=False,
init_values=None)
model = create_model('deit_small_patch16_224', pretrained=pretrained)
if not_original:
model.patch_embed.proj = ConvBlock(48, end_siz=8)
elif modelname == 'vit_m':
model = timm.models.deit.deit3_medium_patch16_224(pretrained=pretrained)
if not_original:
## only for removing patch-stem
model.patch_embed.proj = ConvBlock2(48)
elif modelname == 'vit_s_21k':
model = create_model('deit3_small_patch16_224_in21ft1k', pretrained=pretrained)
elif modelname == 'vit_b':
model = timm.models.vision_transformer.vit_base_patch16_224(pretrained=pretrained) # used for 21k pretrained on 206
if not_original:
model.patch_embed.proj = ConvBlock(48, end_siz=16, fin_dim=None)
elif modelname == "resnet101":
model = timm.models.resnet.resnet101(pretrained=False)
elif modelname=="wrn_50_2":
model = timm.models.resnet.wide_resnet50_2(pretrained=False)
elif modelname == "densnet201":
model = timm.models.densenet.densenet201(pretrained=pretrained)
elif modelname == "inception":
model = create_model('inception_v3', pretrained=pretrained)
else:
logger.error('Invalid model name, please use either cait, deit, swin, vit, effnet, or rn50')
sys.exit(1)
return model