-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodels.py
216 lines (184 loc) · 7.66 KB
/
models.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
from timm.models import cait, poolformer, resnet, xcit
from timm.models.helpers import build_model_with_cfg
from timm.models.registry import register_model
from torch import nn
from src import utils
default_cfgs = {
'cait_s12_224': cait._cfg(input_size=(3, 224, 224)),
'xcit_medium_12_p16_224': xcit._cfg(),
'xcit_large_12_p16_224': xcit._cfg(),
'xcit_large_12_h8_p16_224': xcit._cfg(),
'xcit_small_12_p4_32': xcit._cfg(input_size=(3, 32, 32)),
'xcit_medium_12_p4_32': xcit._cfg(input_size=(3, 32, 32)),
'xcit_large_12_p4_32': xcit._cfg(input_size=(3, 32, 32)),
'resnet18_gelu': resnet._cfg(),
'resnet50_32': resnet._cfg(input_size=(3, 32, 32), interpolation='bicubic', crop_pct=1, pool_size=(4, 4)),
'resnet50_gelu': resnet._cfg(interpolation='bicubic', crop_pct=0.95),
'resnet152_gelu': resnet._cfg(interpolation='bicubic', crop_pct=0.95),
'resnext152_32x8d': resnet._cfg(input_size=(3, 380, 380)),
'poolformer_m12': poolformer._cfg(crop_pct=0.95)
}
@register_model
def cait_s12_224(pretrained=False, **kwargs):
model_kwargs = dict(patch_size=16, embed_dim=384, depth=12, num_heads=8, init_values=1.0, **kwargs)
return build_model_with_cfg(cait.Cait,
'cait_s12_224',
pretrained,
pretrained_filter_fn=cait.checkpoint_filter_fn,
**model_kwargs)
@register_model
def xcit_medium_12_p16_224(pretrained=False, **kwargs):
model_kwargs = dict(patch_size=16,
embed_dim=512,
depth=12,
num_heads=8,
eta=1.0,
tokens_norm=True,
**kwargs)
model = xcit._create_xcit('xcit_medium_12_p16_224', pretrained=pretrained, **model_kwargs)
return model
@register_model
def xcit_large_12_p16_224(pretrained=False, **kwargs):
model_kwargs = dict(patch_size=16,
embed_dim=768,
depth=12,
num_heads=16,
eta=1.0,
tokens_norm=True,
**kwargs)
model = xcit._create_xcit('xcit_large_12_p16_224', pretrained=pretrained, **model_kwargs)
return model
@register_model
def xcit_large_12_h8_p16_224(pretrained=False, **kwargs):
model_kwargs = dict(patch_size=16,
embed_dim=768,
depth=12,
num_heads=8,
eta=1.0,
tokens_norm=True,
**kwargs)
model = xcit._create_xcit('xcit_large_12_h8_p16_224', pretrained=pretrained, **model_kwargs)
return model
@register_model
def xcit_small_12_p8_32(pretrained=False, **kwargs):
model_kwargs = dict(
patch_size=16, # 16 because the pre-trained model has 16
embed_dim=384,
depth=12,
num_heads=8,
eta=1.0,
tokens_norm=True,
**kwargs)
model = xcit._create_xcit('xcit_small_12_p4_32', pretrained=pretrained, **model_kwargs)
assert isinstance(model, xcit.XCiT)
model = utils.adapt_model_patches(model, 8)
return model
@register_model
def xcit_small_12_p4_32(pretrained=False, **kwargs):
model_kwargs = dict(
patch_size=16, # 16 because the pre-trained model has 16
embed_dim=384,
depth=12,
num_heads=8,
eta=1.0,
tokens_norm=True,
**kwargs)
model = xcit._create_xcit('xcit_small_12_p4_32', pretrained=pretrained, **model_kwargs)
assert isinstance(model, xcit.XCiT)
model = utils.adapt_model_patches(model, 4)
return model
@register_model
def xcit_medium_12_p4_32(pretrained=False, **kwargs):
model_kwargs = dict(patch_size=16,
embed_dim=512,
depth=12,
num_heads=8,
eta=1.0,
tokens_norm=True,
**kwargs)
model = xcit._create_xcit('xcit_medium_12_p4_32', pretrained=pretrained, **model_kwargs)
# TODO: make this a function
assert isinstance(model, xcit.XCiT)
model = utils.adapt_model_patches(model, 4)
return model
@register_model
def xcit_large_12_p4_32(pretrained=False, **kwargs):
model_kwargs = dict(patch_size=16,
embed_dim=768,
depth=12,
num_heads=16,
eta=1.0,
tokens_norm=True,
**kwargs)
model = xcit._create_xcit('xcit_large_12_p16_224', pretrained=pretrained, **model_kwargs)
assert isinstance(model, xcit.XCiT)
model = utils.adapt_model_patches(model, 4)
return model
@register_model
def xcit_small_12_p2_32(pretrained=False, **kwargs):
model_kwargs = dict(patch_size=16,
embed_dim=384,
depth=12,
num_heads=8,
eta=1.0,
tokens_norm=True,
**kwargs)
model = xcit._create_xcit('xcit_small_12_p2_32', pretrained=pretrained, **model_kwargs)
assert isinstance(model, xcit.XCiT)
model = utils.adapt_model_patches(model, 2)
return model
@register_model
def resnet152_gelu(pretrained=False, **kwargs):
"""Constructs a ResNet-152 model with GELU.
"""
model_args = dict(block=resnet.Bottleneck,
layers=[3, 8, 36, 3],
act_layer=lambda inplace: nn.GELU(),
**kwargs)
return resnet._create_resnet('resnet152', pretrained, **model_args)
@register_model
def resnet50_32(pretrained=False, **kwargs):
"""Constructs a ResNet-50 model.
"""
model_args = dict(block=resnet.Bottleneck, layers=[3, 4, 6, 3], **kwargs)
model = resnet._create_resnet('resnet50_32', pretrained, **model_args)
model.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False)
model.maxpool = nn.Identity()
return model
@register_model
def resnet50_gelu(pretrained=False, **kwargs):
"""Constructs a ResNet-50 model with GELU."""
model_args = dict(block=resnet.Bottleneck,
layers=[3, 4, 6, 3],
act_layer=lambda inplace: nn.GELU(),
**kwargs)
return resnet._create_resnet('resnet50_gelu', pretrained, **model_args)
@register_model
def resnet18_gelu(pretrained=False, **kwargs):
"""Constructs a ResNet-18 model wit GELU."""
model_args = dict(block=resnet.BasicBlock,
layers=[2, 2, 2, 2],
act_layer=lambda inplace: nn.GELU(),
**kwargs)
return resnet._create_resnet('resnet18_gelu', pretrained, **model_args)
@register_model
def resnext152_32x8d(pretrained=False, **kwargs):
"""Constructs a ResNeXt152-32x8d model. Added to compare to https://arxiv.org/abs/2006.14536"""
model_args = dict(block=resnet.Bottleneck,
layers=[3, 4, 36, 3],
cardinality=32,
base_width=8,
act_layer=nn.SiLU,
**kwargs)
return resnet._create_resnet('resnext152_32x8d', pretrained, **model_args)
@register_model
def poolformer_m12(pretrained=False, **kwargs):
""" PoolFormer-M12 model, Params: 12M """
layers = (2, 2, 6, 2)
embed_dims = (96, 192, 384, 768)
model = poolformer._create_poolformer('poolformer_m12',
pretrained=pretrained,
layers=layers,
embed_dims=embed_dims,
**kwargs)
return model