mirror of
https://github.com/huggingface/pytorch-image-models.git
synced 2025-06-03 15:01:08 +08:00
Update metaformers.py
This commit is contained in:
parent
1b3318bf19
commit
4cfecf8acb
@ -647,6 +647,9 @@ class MetaFormer(nn.Module):
|
|||||||
if not isinstance(res_scale_init_values, (list, tuple)):
|
if not isinstance(res_scale_init_values, (list, tuple)):
|
||||||
res_scale_init_values = [res_scale_init_values] * num_stage
|
res_scale_init_values = [res_scale_init_values] * num_stage
|
||||||
|
|
||||||
|
self.grad_checkpointing = False
|
||||||
|
self.feature_info = []
|
||||||
|
|
||||||
stages = nn.ModuleList() # each stage consists of multiple metaformer blocks
|
stages = nn.ModuleList() # each stage consists of multiple metaformer blocks
|
||||||
cur = 0
|
cur = 0
|
||||||
for i in range(num_stage):
|
for i in range(num_stage):
|
||||||
@ -665,6 +668,7 @@ class MetaFormer(nn.Module):
|
|||||||
)
|
)
|
||||||
stages.append(stage)
|
stages.append(stage)
|
||||||
cur += depths[i]
|
cur += depths[i]
|
||||||
|
self.feature_info += [dict(num_chs=dims[stage_id], reduction=2, module=f'stages.{stage_id}')]
|
||||||
|
|
||||||
self.stages = nn.Sequential(*stages)
|
self.stages = nn.Sequential(*stages)
|
||||||
self.norm = output_norm(dims[-1])
|
self.norm = output_norm(dims[-1])
|
||||||
@ -688,16 +692,25 @@ class MetaFormer(nn.Module):
|
|||||||
def no_weight_decay(self):
|
def no_weight_decay(self):
|
||||||
return {'norm'}
|
return {'norm'}
|
||||||
|
|
||||||
def forward_features(self, x):
|
def forward_head(self, x, pre_logits: bool = False):
|
||||||
x = self.stages(x)
|
if pre_logits:
|
||||||
|
return x
|
||||||
|
|
||||||
x = x.mean([1,2]) # TODO use adaptive pool instead of mean
|
x = x.mean([1,2]) # TODO use adaptive pool instead of mean
|
||||||
x = self.norm(x)
|
x = self.norm(x)
|
||||||
# (B, H, W, C) -> (B, C)
|
# (B, H, W, C) -> (B, C)
|
||||||
|
x = self.head(x)
|
||||||
|
return x
|
||||||
|
|
||||||
|
def forward_features(self, x):
|
||||||
|
x = self.stages(x)
|
||||||
|
|
||||||
|
|
||||||
return x
|
return x
|
||||||
|
|
||||||
def forward(self, x):
|
def forward(self, x):
|
||||||
x = self.forward_features(x)
|
x = self.forward_features(x)
|
||||||
x = self.head(x)
|
x = forward_head(x)
|
||||||
return x
|
return x
|
||||||
|
|
||||||
def checkpoint_filter_fn(state_dict, model):
|
def checkpoint_filter_fn(state_dict, model):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user