-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRDNet.py
343 lines (287 loc) · 11.2 KB
/
RDNet.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
"""
RDNet
Copyright (c) 2024-present NAVER Cloud Corp.
Apache-2.0
"""
from functools import partial
from typing import List
import torch
import torch.nn as nn
from timm.data import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD
from timm.layers.squeeze_excite import EffectiveSEModule
from timm.models import register_model, build_model_with_cfg, named_apply, generate_default_cfgs
from timm.models.layers import DropPath
from timm.models.layers import LayerNorm2d
__all__ = ["RDNet"]
class RDNetClassifierHead(nn.Module):
def __init__(
self,
in_features: int,
num_classes: int,
drop_rate: float = 0.,
):
super().__init__()
self.in_features = in_features
self.num_features = in_features
self.norm = nn.LayerNorm(in_features)
self.drop = nn.Dropout(drop_rate)
self.fc = nn.Linear(self.num_features, num_classes) if num_classes > 0 else nn.Identity()
def reset(self, num_classes):
self.fc = nn.Linear(self.num_features, num_classes) if num_classes > 0 else nn.Identity()
def forward(self, x, pre_logits: bool = False):
x = x.mean([-2, -1])
x = self.norm(x)
x = self.drop(x)
if pre_logits:
return x
x = self.fc(x)
return x
class PatchifyStem(nn.Module):
def __init__(self, num_input_channels, num_init_features, patch_size=4):
super().__init__()
self.stem = nn.Sequential(
nn.Conv2d(num_input_channels, num_init_features, kernel_size=patch_size, stride=patch_size),
LayerNorm2d(num_init_features),
)
def forward(self, x):
return self.stem(x)
class Block(nn.Module):
"""D == Dw conv, N == Norm, F == Feed Forward, A == Activation"""
def __init__(self, in_chs, inter_chs, out_chs):
super().__init__()
self.layers = nn.Sequential(
nn.Conv2d(in_chs, in_chs, groups=in_chs, kernel_size=7, stride=1, padding=3),
LayerNorm2d(in_chs, eps=1e-6),
nn.Conv2d(in_chs, inter_chs, kernel_size=1, stride=1, padding=0),
nn.GELU(),
nn.Conv2d(inter_chs, out_chs, kernel_size=1, stride=1, padding=0),
)
def forward(self, x):
return self.layers(x)
class BlockESE(nn.Module):
"""D == Dw conv, N == Norm, F == Feed Forward, A == Activation"""
def __init__(self, in_chs, inter_chs, out_chs):
super().__init__()
self.layers = nn.Sequential(
nn.Conv2d(in_chs, in_chs, groups=in_chs, kernel_size=7, stride=1, padding=3),
LayerNorm2d(in_chs, eps=1e-6),
nn.Conv2d(in_chs, inter_chs, kernel_size=1, stride=1, padding=0),
nn.GELU(),
nn.Conv2d(inter_chs, out_chs, kernel_size=1, stride=1, padding=0),
EffectiveSEModule(out_chs),
)
def forward(self, x):
return self.layers(x)
class DenseBlock(nn.Module):
def __init__(
self,
num_input_features,
growth_rate,
bottleneck_width_ratio,
drop_path_rate,
drop_rate=0.0,
rand_gather_step_prob=0.0,
block_idx=0,
block_type="Block",
ls_init_value=1e-6,
**kwargs,
):
super().__init__()
self.drop_rate = drop_rate
self.drop_path_rate = drop_path_rate
self.rand_gather_step_prob = rand_gather_step_prob
self.block_idx = block_idx
self.growth_rate = growth_rate
self.gamma = nn.Parameter(ls_init_value * torch.ones(growth_rate)) if ls_init_value > 0 else None
growth_rate = int(growth_rate)
inter_chs = int(num_input_features * bottleneck_width_ratio / 8) * 8
if self.drop_path_rate > 0:
self.drop_path = DropPath(drop_path_rate)
self.layers = eval(block_type)(
in_chs=num_input_features,
inter_chs=inter_chs,
out_chs=growth_rate,
)
def forward(self, x):
if isinstance(x, List):
x = torch.cat(x, 1)
x = self.layers(x)
if self.gamma is not None:
x = x.mul(self.gamma.reshape(1, -1, 1, 1))
if self.drop_path_rate > 0 and self.training:
x = self.drop_path(x)
return x
class DenseStage(nn.Sequential):
def __init__(self, num_block, num_input_features, drop_path_rates, growth_rate, **kwargs):
super().__init__()
for i in range(num_block):
layer = DenseBlock(
num_input_features=num_input_features,
growth_rate=growth_rate,
drop_path_rate=drop_path_rates[i],
block_idx=i,
**kwargs,
)
num_input_features += growth_rate
self.add_module(f"dense_block{i}", layer)
self.num_out_features = num_input_features
def forward(self, init_feature):
features = [init_feature]
for module in self:
new_feature = module(features)
features.append(new_feature)
return torch.cat(features, 1)
class RDNet(nn.Module):
def __init__(
self,
num_init_features=64,
growth_rates=(64, 104, 128, 128, 128, 128, 224),
num_blocks_list=(3, 3, 3, 3, 3, 3, 3),
bottleneck_width_ratio=4,
zero_head=False,
in_chans=3, # timm option [--in-chans]
num_classes=1000, # timm option [--num-classes]
drop_rate=0.0, # timm option [--drop: dropout ratio]
drop_path_rate=0.0, # timm option [--drop-path: drop-path ratio]
checkpoint_path=None, # timm option [--initial-checkpoint]
transition_compression_ratio=0.5,
ls_init_value=1e-6,
is_downsample_block=(None, True, True, False, False, False, True),
block_type="Block",
head_init_scale: float = 1.,
**kwargs,
):
super().__init__()
assert len(growth_rates) == len(num_blocks_list) == len(is_downsample_block)
self.num_classes = num_classes
if isinstance(block_type, str):
block_type = [block_type] * len(growth_rates)
# stem
self.stem = PatchifyStem(in_chans, num_init_features, patch_size=4)
# features
self.feature_info = []
self.num_stages = len(growth_rates)
curr_stride = 4 # stem_stride
num_features = num_init_features
dp_rates = [x.tolist() for x in torch.linspace(0, drop_path_rate, sum(num_blocks_list)).split(num_blocks_list)]
dense_stages = []
for i in range(self.num_stages):
dense_stage_layers = []
if i != 0:
compressed_num_features = int(num_features * transition_compression_ratio / 8) * 8
k_size = stride = 1
if is_downsample_block[i]:
curr_stride *= 2
k_size = stride = 2
dense_stage_layers.append(LayerNorm2d(num_features))
dense_stage_layers.append(
nn.Conv2d(num_features, compressed_num_features, kernel_size=k_size, stride=stride, padding=0)
)
num_features = compressed_num_features
stage = DenseStage(
num_block=num_blocks_list[i],
num_input_features=num_features,
growth_rate=growth_rates[i],
bottleneck_width_ratio=bottleneck_width_ratio,
drop_rate=drop_rate,
drop_path_rates=dp_rates[i],
ls_init_value=ls_init_value,
block_type=block_type[i],
)
dense_stage_layers.append(stage)
num_features += num_blocks_list[i] * growth_rates[i]
if i + 1 == self.num_stages or (i + 1 != self.num_stages and is_downsample_block[i + 1]):
self.feature_info += [
dict(
num_chs=num_features,
reduction=curr_stride,
module=f'dense_stages.{i}',
growth_rate=growth_rates[i],
)
]
dense_stages.append(nn.Sequential(*dense_stage_layers))
self.dense_stages = nn.Sequential(*dense_stages)
# classifier
self.head = RDNetClassifierHead(num_features, num_classes, drop_rate=drop_rate)
# initialize weights
named_apply(partial(_init_weights, head_init_scale=head_init_scale), self)
if zero_head:
nn.init.zeros_(self.head[-1].weight.data)
if self.head[-1].bias is not None:
nn.init.zeros_(self.head[-1].bias.data)
if checkpoint_path is not None:
self.load_state_dict(torch.load(checkpoint_path, map_location="cpu"))
@torch.jit.ignore
def get_classifier(self):
return self.head.fc
def reset_classifier(self, num_classes=0, global_pool=None):
assert global_pool is None
self.head.reset(num_classes)
def forward_head(self, x, pre_logits: bool = False):
return self.head(x, pre_logits=True) if pre_logits else self.head(x)
def forward_features(self, x):
x = self.stem(x)
x = self.dense_stages(x)
return x
def forward(self, x):
x = self.forward_features(x)
x = self.head(x)
return x
def group_matcher(self, coarse=False):
assert not coarse
return dict(
stem=r'^stem',
blocks=r'^dense_stages\.(\d+)',
)
def _init_weights(module, name=None, head_init_scale=1.0):
if isinstance(module, nn.Conv2d):
nn.init.kaiming_normal_(module.weight)
elif isinstance(module, nn.BatchNorm2d):
nn.init.constant_(module.weight, 1)
nn.init.constant_(module.bias, 0)
elif isinstance(module, nn.Linear):
nn.init.constant_(module.bias, 0)
if name and 'head.' in name:
module.weight.data.mul_(head_init_scale)
module.bias.data.mul_(head_init_scale)
def _create_rdnet(variant, pretrained=False, **kwargs):
if kwargs.get("pretrained_cfg", "") == "fcmae":
# NOTE fcmae pretrained weights have no classifier or final norm-layer (`head.norm`)
# This is workaround loading with num_classes=0 w/o removing norm-layer.
kwargs.setdefault("pretrained_strict", False)
model = build_model_with_cfg(
RDNet, variant, pretrained, feature_cfg=dict(out_indices=(0, 1, 2, 3), flatten_sequential=True), **kwargs
)
return model
def _cfg(url='', **kwargs):
return {
"url": url,
"num_classes": 1000,
"input_size": (3, 224, 224),
"crop_pct": 0.9,
"interpolation": "bicubic",
"mean": IMAGENET_DEFAULT_MEAN,
"std": IMAGENET_DEFAULT_STD,
"first_conv": "stem.stem.0",
"classifier": "head.fc",
**kwargs,
}
default_cfgs = generate_default_cfgs({
'rdnet_tiny.nv_in1k': _cfg(
hf_hub_id='naver-ai/rdnet_tiny.nv_in1k',
),
'rdnet_small.nv_in1k': _cfg(
hf_hub_id='naver-ai/rdnet_small.nv_in1k',
),
'rdnet_base.nv_in1k': _cfg(
hf_hub_id='naver-ai/rdnet_base.nv_in1k',
),
'rdnet_large.nv_in1k': _cfg(
hf_hub_id='naver-ai/rdnet_large.nv_in1k',
),
'rdnet_large.nv_in1k_ft_in1k_384': _cfg(
hf_hub_id='naver-ai/rdnet_large.nv_in1k_ft_in1k_384',
input_size=(3, 384, 384),
crop_pct=1.0,
),
})