-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathmodels.py
456 lines (380 loc) · 12.7 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
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
# Copyright 2024 Google LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import ml_collections
# The key of this dictionary refers to basename in the directory:
# https://console.cloud.google.com/storage/vit_models/
# Note that some names (e.g. "testing", but also some models only available in
# the AugReg paper) are not actually present in that directory.
MODEL_CONFIGS = {}
# The key of this dictionary refers to the first part (delimited by "-") of the
# filename of the checkpoint in:
# https://console.cloud.google.com/storage/vit_models/augreg/index.csv
AUGREG_CONFIGS = {}
def _register(get_config):
"""Adds reference to model config into MODEL_CONFIGS and AUGREG_CONFIGS."""
config = get_config().lock()
name = config.get('model_name')
MODEL_CONFIGS[name] = config
if 'Mixer' not in name and name not in ('testing', 'ViT-L_32', 'R50+ViT-B_16',
'ViT-H_14'):
# Note: we're using stricter filenames for AugReg checkpoints so they can be
# used both as filesystem filenames and URIs without escaping.
augreg_name = name.replace('ViT-', '').replace('+', '_')
AUGREG_CONFIGS[augreg_name] = config
return get_config
@_register
def get_testing_config():
"""Returns a simple config used for testing."""
config = ml_collections.ConfigDict()
# Only used for testing.
config.model_name = 'testing'
config.patches = ml_collections.ConfigDict({'size': (16, 16)})
config.hidden_size = 10
config.transformer = ml_collections.ConfigDict()
config.transformer.mlp_dim = 10
config.transformer.num_heads = 2
config.transformer.num_layers = 1
config.transformer.attention_dropout_rate = 0.0
config.transformer.dropout_rate = 0.1
config.classifier = 'token'
config.representation_size = None
return config
@_register
def get_testing_unpooled_config():
"""Returns a simple config used for testing unpooled version."""
config = get_testing_config()
# Only used for testing.
config.model_name = 'testing-unpooled'
config.classifier = 'unpooled'
return config
# ViT-X/16 & ViT-H/14
#####################
@_register
def get_ti16_config():
"""Returns the ViT-Ti/16 configuration."""
config = ml_collections.ConfigDict()
config.model_name = 'ViT-Ti_16'
config.patches = ml_collections.ConfigDict({'size': (16, 16)})
config.hidden_size = 192
config.transformer = ml_collections.ConfigDict()
config.transformer.mlp_dim = 768
config.transformer.num_heads = 3
config.transformer.num_layers = 12
config.transformer.attention_dropout_rate = 0.0
config.transformer.dropout_rate = 0.0
config.classifier = 'token'
config.representation_size = None
return config
@_register
def get_s16_config():
"""Returns the ViT-S/16 configuration."""
config = ml_collections.ConfigDict()
config.model_name = 'ViT-S_16'
config.patches = ml_collections.ConfigDict({'size': (16, 16)})
config.hidden_size = 384
config.transformer = ml_collections.ConfigDict()
config.transformer.mlp_dim = 1536
config.transformer.num_heads = 6
config.transformer.num_layers = 12
config.transformer.attention_dropout_rate = 0.0
config.transformer.dropout_rate = 0.0
config.classifier = 'token'
config.representation_size = None
return config
@_register
def get_b16_config():
"""Returns the ViT-B/16 configuration."""
config = ml_collections.ConfigDict()
config.model_name = 'ViT-B_16'
config.patches = ml_collections.ConfigDict({'size': (16, 16)})
config.hidden_size = 768
config.transformer = ml_collections.ConfigDict()
config.transformer.mlp_dim = 3072
config.transformer.num_heads = 12
config.transformer.num_layers = 12
config.transformer.attention_dropout_rate = 0.0
config.transformer.dropout_rate = 0.0
config.classifier = 'token'
config.representation_size = None
return config
@_register
def get_l16_config():
"""Returns the ViT-L/16 configuration."""
config = ml_collections.ConfigDict()
config.model_name = 'ViT-L_16'
config.patches = ml_collections.ConfigDict({'size': (16, 16)})
config.hidden_size = 1024
config.transformer = ml_collections.ConfigDict()
config.transformer.mlp_dim = 4096
config.transformer.num_heads = 16
config.transformer.num_layers = 24
config.transformer.attention_dropout_rate = 0.0
config.transformer.dropout_rate = 0.1
config.classifier = 'token'
config.representation_size = None
return config
@_register
def get_h14_config():
"""Returns the ViT-H/14 configuration."""
config = ml_collections.ConfigDict()
config.model_name = 'ViT-H_14'
config.patches = ml_collections.ConfigDict({'size': (14, 14)})
config.hidden_size = 1280
config.transformer = ml_collections.ConfigDict()
config.transformer.mlp_dim = 5120
config.transformer.num_heads = 16
config.transformer.num_layers = 32
config.transformer.attention_dropout_rate = 0.0
config.transformer.dropout_rate = 0.1
config.classifier = 'token'
config.representation_size = None
return config
@_register
def get_s16_gap_norep_config():
"""Returns ViT-S/16 with classifier=gap, representation=None."""
config = get_s16_config()
config.model_name = 'ViT-S_16-gap-norep'
config.classifier = 'gap'
config.representation_size = None
return config
@_register
def get_b16_gap_norep_config():
"""Returns ViT-B/16 with classifier=gap, representation=None."""
config = get_b16_config()
config.model_name = 'ViT-B_16-gap-norep'
config.classifier = 'gap'
config.representation_size = None
return config
# ViT-X/8
#########
@_register
def get_b8_config():
"""Returns the ViT-B/8 configuration."""
config = get_b16_config()
config.model_name = 'ViT-B_8'
config.patches.size = (8, 8)
return config
# ViT-X/32
##########
@_register
def get_s32_config():
"""Returns the ViT-S/32 configuration."""
config = get_s16_config()
config.model_name = 'ViT-S_32'
config.patches.size = (32, 32)
return config
@_register
def get_b32_config():
"""Returns the ViT-B/32 configuration."""
config = get_b16_config()
config.model_name = 'ViT-B_32'
config.patches.size = (32, 32)
return config
@_register
def get_l32_config():
"""Returns the ViT-L/32 configuration."""
config = get_l16_config()
config.transformer.dropout_rate = 0.0
config.model_name = 'ViT-L_32'
config.patches.size = (32, 32)
return config
@_register
def get_s32_gap_norep_config():
"""Returns ViT-S/32 with classifier=gap, representation=None."""
config = get_s32_config()
config.model_name = 'ViT-S_32-gap-norep'
config.classifier = 'gap'
config.representation_size = None
return config
@_register
def get_b32_gap_norep_config():
"""Returns ViT-B/32 with classifier=gap, representation=None."""
config = get_b32_config()
config.model_name = 'ViT-B_32-gap-norep'
config.classifier = 'gap'
config.representation_size = None
return config
# Hybrids R+ViT-X/16
####################
@_register
def get_r_ti16_config():
"""Returns the Resnet stem + ViT-Ti/16 configuration."""
config = get_ti16_config()
config.model_name = 'R+ViT-Ti_16'
config.patches.size = (8, 8)
config.resnet = ml_collections.ConfigDict()
# The resnet stem alone downscales 2x, making /16 with 8x8 patches.
config.resnet.num_layers = ()
config.resnet.width_factor = 1
return config
@_register
def get_r50_b16_config():
"""Returns the Resnet50 + ViT-B/16 configuration."""
config = get_b16_config()
config.transformer.dropout_rate = 0.1
config.model_name = 'R50+ViT-B_16'
config.patches.size = (1, 1)
config.resnet = ml_collections.ConfigDict()
# Note that the "real" Resnet50 has (3, 4, 6, 3) bottleneck blocks. Here
# we're using (3, 4, 9) configuration so we get a downscaling of 2^(1 + 3)=16
# which results in an effective patch size of /16.
config.resnet.num_layers = (3, 4, 9)
config.resnet.width_factor = 1
return config
# Hybrids R+ViT-X/32
####################
@_register
def get_r26_b32_config():
"""Returns the Resnet26 + ViT-B/32 configuration."""
config = get_b32_config()
config.model_name = 'R26+ViT-B_32'
config.patches.size = (1, 1)
config.resnet = ml_collections.ConfigDict()
# Using four bottleneck blocks results in a downscaling of 2^(1 + 4)=32 which
# results in an effective patch size of /32.
config.resnet.num_layers = (2, 2, 2, 2)
config.resnet.width_factor = 1
return config
@_register
def get_r26_s32_config():
"""Returns the Resnet26 + ViT-S/32 configuration."""
config = get_s16_config()
config.model_name = 'R26+ViT-S_32'
config.patches.size = (1, 1)
config.resnet = ml_collections.ConfigDict()
# Using four bottleneck blocks results in a downscaling of 2^(1 + 4)=32 which
# results in an effective patch size of /32.
config.resnet.num_layers = (2, 2, 2, 2)
config.resnet.width_factor = 1
return config
@_register
def get_r50_l32_config():
"""Returns the Resnet50 + ViT-L/32 configuration."""
config = get_l16_config()
config.model_name = 'R50+ViT-L_32'
config.patches.size = (1, 1)
config.resnet = ml_collections.ConfigDict()
# Using four bottleneck blocks results in a downscaling of 2^(1 + 4)=32 which
# results in an effective patch size of /32.
config.resnet.num_layers = (3, 4, 6, 3)
config.resnet.width_factor = 1
return config
# Mixers
########
@_register
def get_mixer_b16_config():
"""Returns Mixer-B/16 configuration."""
config = ml_collections.ConfigDict()
config.model_name = 'Mixer-B_16'
config.patches = ml_collections.ConfigDict({'size': (16, 16)})
config.hidden_dim = 768
config.num_blocks = 12
config.tokens_mlp_dim = 384
config.channels_mlp_dim = 3072
return config
@_register
def get_mixer_b32_config():
"""Returns Mixer-B/32 configuration."""
config = get_mixer_b16_config()
config.model_name = 'Mixer-B_32'
config.patches = ml_collections.ConfigDict({'size': (32, 32)})
return config
@_register
def get_mixer_l16_config():
"""Returns Mixer-L/16 configuration."""
config = ml_collections.ConfigDict()
config.model_name = 'Mixer-L_16'
config.patches = ml_collections.ConfigDict({'size': (16, 16)})
config.hidden_dim = 1024
config.num_blocks = 24
config.tokens_mlp_dim = 512
config.channels_mlp_dim = 4096
return config
# LiT
#####
@_register
def get_lit_b16b_config():
"""Returns a LiT model with ViT-Base and BERT-Base towers."""
config = ml_collections.ConfigDict()
config.model_name = 'LiT-B16B'
config.out_dim = (768, 768)
config.image = get_b16_config()
config.text_model = 'bert'
config.text = {}
config.text.config = 'base'
config.pp = {}
config.pp.tokenizer_name = 'bert'
config.pp.size = 224
config.pp.max_len = 16
return config
@_register
def get_lit_b16b_2_config():
"""Returns an improved LiT model with ViT-Base and BERT-Base towers."""
config = get_lit_b16b_config()
config.model_name = 'LiT-B16B_2'
config.out_dim = (None, 768)
return config
@_register
def get_lit_l16l_config():
"""Returns a LiT model with ViT-Large and BERT-Large towers."""
config = ml_collections.ConfigDict()
config.model_name = 'LiT-L16L'
config.out_dim = (None, 1024)
config.image = get_l16_config()
config.text_model = 'bert'
config.text = {}
config.text.config = 'large'
config.pp = {}
config.pp.tokenizer_name = 'bert'
config.pp.size = 224
config.pp.max_len = 16
return config
@_register
def get_lit_l16s_config():
"""Returns a LiT model with ViT-Large and small text towers."""
config = ml_collections.ConfigDict()
config.model_name = 'LiT-L16S'
config.out_dim = (None, 1024)
config.image = get_l16_config()
config.text_model = 'text_transformer'
config.text = {}
config.text.width = 384
config.text.num_layers = 12
config.text.mlp_dim = 1536
config.text.num_heads = 6
config.text.vocab_size = 16_000
config.pp = {}
config.pp.tokenizer_name = 'sentencepiece'
config.pp.size = 224
config.pp.max_len = 16
return config
@_register
def get_lit_l16ti_config():
"""Returns a LiT model with ViT-Large and tiny text towers."""
config = ml_collections.ConfigDict()
config.model_name = 'LiT-L16Ti'
config.out_dim = (None, 1024)
config.image = get_l16_config()
config.text_model = 'text_transformer'
config.text = {}
config.text.width = 192
config.text.num_layers = 12
config.text.mlp_dim = 768
config.text.num_heads = 3
config.text.vocab_size = 16_000
config.pp = {}
config.pp.tokenizer_name = 'sentencepiece'
config.pp.size = 224
config.pp.max_len = 16
return config