This repository has been archived by the owner on Dec 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathmodels.py
64 lines (58 loc) · 2.01 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
# Copyright (c) 2015-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the CC-by-NC license found in the
# LICENSE file in the root directory of this source tree.
#
import torch
import torch.nn as nn
from functools import partial
from convit import VisionTransformer
from timm.models.efficientnet import EfficientNet
from timm.models.vision_transformer import _cfg
from timm.models.registry import register_model
@register_model
def convit_tiny(pretrained=False, **kwargs):
num_heads = 4
kwargs['embed_dim'] *= num_heads
model = VisionTransformer(
num_heads=num_heads,
norm_layer=partial(nn.LayerNorm, eps=1e-6), **kwargs)
model.default_cfg = _cfg()
if pretrained:
checkpoint = torch.hub.load_state_dict_from_url(
url="https://dl.fbaipublicfiles.com/convit/convit_tiny.pth",
map_location="cpu", check_hash=True
)
model.load_state_dict(checkpoint)
return model
@register_model
def convit_small(pretrained=False, **kwargs):
num_heads = 9
kwargs['embed_dim'] *= num_heads
model = VisionTransformer(
num_heads=num_heads,
norm_layer=partial(nn.LayerNorm, eps=1e-6), **kwargs)
model.default_cfg = _cfg()
if pretrained:
checkpoint = torch.hub.load_state_dict_from_url(
url="https://dl.fbaipublicfiles.com/convit/convit_small.pth",
map_location="cpu", check_hash=True
)
model.load_state_dict(checkpoint)
return model
@register_model
def convit_base(pretrained=False, **kwargs):
num_heads = 16
kwargs['embed_dim'] *= num_heads
model = VisionTransformer(
num_heads=num_heads,
norm_layer=partial(nn.LayerNorm, eps=1e-6), **kwargs)
model.default_cfg = _cfg()
if pretrained:
checkpoint = torch.hub.load_state_dict_from_url(
url="https://dl.fbaipublicfiles.com/convit/convit_base.pth",
map_location="cpu", check_hash=True
)
model.load_state_dict(checkpoint)
return model