Spaces:
Running
on
Zero
Running
on
Zero
SunderAli17
commited on
Commit
•
81dfef5
1
Parent(s):
a859150
Create timm_model.py
Browse files- evaclip/timm_model.py +122 -0
evaclip/timm_model.py
ADDED
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
""" timm model adapter
|
2 |
+
Wraps timm (https://github.com/rwightman/pytorch-image-models) models for use as a vision tower in CLIP model.
|
3 |
+
"""
|
4 |
+
import logging
|
5 |
+
from collections import OrderedDict
|
6 |
+
|
7 |
+
import torch
|
8 |
+
import torch.nn as nn
|
9 |
+
|
10 |
+
try:
|
11 |
+
import timm
|
12 |
+
from timm.models.layers import Mlp, to_2tuple
|
13 |
+
try:
|
14 |
+
# old timm imports < 0.8.1
|
15 |
+
from timm.models.layers.attention_pool2d import RotAttentionPool2d
|
16 |
+
from timm.models.layers.attention_pool2d import AttentionPool2d as AbsAttentionPool2d
|
17 |
+
except ImportError:
|
18 |
+
# new timm imports >= 0.8.1
|
19 |
+
from timm.layers import RotAttentionPool2d
|
20 |
+
from timm.layers import AttentionPool2d as AbsAttentionPool2d
|
21 |
+
except ImportError:
|
22 |
+
timm = None
|
23 |
+
|
24 |
+
from .utils import freeze_batch_norm_2d
|
25 |
+
|
26 |
+
|
27 |
+
class TimmModel(nn.Module):
|
28 |
+
""" timm model adapter
|
29 |
+
# FIXME this adapter is a work in progress, may change in ways that break weight compat
|
30 |
+
"""
|
31 |
+
|
32 |
+
def __init__(
|
33 |
+
self,
|
34 |
+
model_name,
|
35 |
+
embed_dim,
|
36 |
+
image_size=224,
|
37 |
+
pool='avg',
|
38 |
+
proj='linear',
|
39 |
+
proj_bias=False,
|
40 |
+
drop=0.,
|
41 |
+
pretrained=False):
|
42 |
+
super().__init__()
|
43 |
+
if timm is None:
|
44 |
+
# raise RuntimeError("Please `pip install timm` to use timm models.")
|
45 |
+
return
|
46 |
+
|
47 |
+
self.image_size = to_2tuple(image_size)
|
48 |
+
self.trunk = timm.create_model(model_name, pretrained=pretrained)
|
49 |
+
feat_size = self.trunk.default_cfg.get('pool_size', None)
|
50 |
+
feature_ndim = 1 if not feat_size else 2
|
51 |
+
if pool in ('abs_attn', 'rot_attn'):
|
52 |
+
assert feature_ndim == 2
|
53 |
+
# if attn pooling used, remove both classifier and default pool
|
54 |
+
self.trunk.reset_classifier(0, global_pool='')
|
55 |
+
else:
|
56 |
+
# reset global pool if pool config set, otherwise leave as network default
|
57 |
+
reset_kwargs = dict(global_pool=pool) if pool else {}
|
58 |
+
self.trunk.reset_classifier(0, **reset_kwargs)
|
59 |
+
prev_chs = self.trunk.num_features
|
60 |
+
|
61 |
+
head_layers = OrderedDict()
|
62 |
+
if pool == 'abs_attn':
|
63 |
+
head_layers['pool'] = AbsAttentionPool2d(prev_chs, feat_size=feat_size, out_features=embed_dim)
|
64 |
+
prev_chs = embed_dim
|
65 |
+
elif pool == 'rot_attn':
|
66 |
+
head_layers['pool'] = RotAttentionPool2d(prev_chs, out_features=embed_dim)
|
67 |
+
prev_chs = embed_dim
|
68 |
+
else:
|
69 |
+
assert proj, 'projection layer needed if non-attention pooling is used.'
|
70 |
+
|
71 |
+
# NOTE attention pool ends with a projection layer, so proj should usually be set to '' if such pooling is used
|
72 |
+
if proj == 'linear':
|
73 |
+
head_layers['drop'] = nn.Dropout(drop)
|
74 |
+
head_layers['proj'] = nn.Linear(prev_chs, embed_dim, bias=proj_bias)
|
75 |
+
elif proj == 'mlp':
|
76 |
+
head_layers['mlp'] = Mlp(prev_chs, 2 * embed_dim, embed_dim, drop=drop, bias=(True, proj_bias))
|
77 |
+
|
78 |
+
self.head = nn.Sequential(head_layers)
|
79 |
+
|
80 |
+
def lock(self, unlocked_groups=0, freeze_bn_stats=False):
|
81 |
+
""" lock modules
|
82 |
+
Args:
|
83 |
+
unlocked_groups (int): leave last n layer groups unlocked (default: 0)
|
84 |
+
"""
|
85 |
+
if not unlocked_groups:
|
86 |
+
# lock full model
|
87 |
+
for param in self.trunk.parameters():
|
88 |
+
param.requires_grad = False
|
89 |
+
if freeze_bn_stats:
|
90 |
+
freeze_batch_norm_2d(self.trunk)
|
91 |
+
else:
|
92 |
+
# NOTE: partial freeze requires latest timm (master) branch and is subject to change
|
93 |
+
try:
|
94 |
+
# FIXME import here until API stable and in an official release
|
95 |
+
from timm.models.helpers import group_parameters, group_modules
|
96 |
+
except ImportError:
|
97 |
+
raise RuntimeError(
|
98 |
+
'Please install latest timm `pip install git+https://github.com/rwightman/pytorch-image-models`')
|
99 |
+
matcher = self.trunk.group_matcher()
|
100 |
+
gparams = group_parameters(self.trunk, matcher)
|
101 |
+
max_layer_id = max(gparams.keys())
|
102 |
+
max_layer_id = max_layer_id - unlocked_groups
|
103 |
+
for group_idx in range(max_layer_id + 1):
|
104 |
+
group = gparams[group_idx]
|
105 |
+
for param in group:
|
106 |
+
self.trunk.get_parameter(param).requires_grad = False
|
107 |
+
if freeze_bn_stats:
|
108 |
+
gmodules = group_modules(self.trunk, matcher, reverse=True)
|
109 |
+
gmodules = {k for k, v in gmodules.items() if v <= max_layer_id}
|
110 |
+
freeze_batch_norm_2d(self.trunk, gmodules)
|
111 |
+
|
112 |
+
@torch.jit.ignore
|
113 |
+
def set_grad_checkpointing(self, enable=True):
|
114 |
+
try:
|
115 |
+
self.trunk.set_grad_checkpointing(enable)
|
116 |
+
except Exception as e:
|
117 |
+
logging.warning('grad checkpointing not supported for this timm image tower, continuing without...')
|
118 |
+
|
119 |
+
def forward(self, x):
|
120 |
+
x = self.trunk(x)
|
121 |
+
x = self.head(x)
|
122 |
+
return x
|