Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimenting with tiny test models #2197

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions timm/models/byobnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,9 @@ def init_weights(self, zero_init_last: bool = False):
def forward(self, x):
shortcut = x
x = self.conv1_kxk(x)
x = self.conv2_kxk(x)
x = self.attn(x)
x = self.conv2_kxk(x)
x = self.attn_last(x)
x = self.drop_path(x)
if self.shortcut is not None:
x = x + self.shortcut(shortcut)
Expand Down Expand Up @@ -449,7 +450,6 @@ def __init__(
downsample, in_chs, out_chs,
stride=stride, dilation=dilation, apply_act=False, layers=layers,
)

self.conv1_kxk = layers.conv_norm_act(
in_chs, mid_chs, kernel_size,
stride=stride, dilation=dilation[0], groups=groups, drop_layer=drop_block,
Expand Down Expand Up @@ -1931,7 +1931,6 @@ def _init_weights(module, name='', zero_init_last=False):
aa_layer='avg',
head_type='attn_abs',
),

resnet101_clip=ByoModelCfg(
blocks=(
ByoBlockCfg(type='bottle', d=3, c=256, s=1, br=0.25),
Expand All @@ -1946,7 +1945,6 @@ def _init_weights(module, name='', zero_init_last=False):
aa_layer='avg',
head_type='attn_abs',
),

resnet50x4_clip=ByoModelCfg(
blocks=(
ByoBlockCfg(type='bottle', d=4, c=256, s=1, br=0.25),
Expand All @@ -1962,7 +1960,6 @@ def _init_weights(module, name='', zero_init_last=False):
aa_layer='avg',
head_type='attn_abs',
),

resnet50x16_clip=ByoModelCfg(
blocks=(
ByoBlockCfg(type='bottle', d=6, c=256, s=1, br=0.25),
Expand All @@ -1978,7 +1975,6 @@ def _init_weights(module, name='', zero_init_last=False):
aa_layer='avg',
head_type='attn_abs',
),

resnet50x64_clip=ByoModelCfg(
blocks=(
ByoBlockCfg(type='bottle', d=3, c=256, s=1, br=0.25),
Expand Down Expand Up @@ -2010,6 +2006,21 @@ def _init_weights(module, name='', zero_init_last=False):
head_hidden_size=1024,
head_type='mlp',
),

test_byobnet=ByoModelCfg(
blocks=(
ByoBlockCfg(type='edge', d=1, c=32, s=2, gs=0, br=0.5),
ByoBlockCfg(type='dark', d=1, c=64, s=2, gs=0, br=0.5),
ByoBlockCfg(type='basic', d=1, c=128, s=2, gs=32, br=0.25),
ByoBlockCfg(type='bottle', d=1, c=256, s=2, gs=64, br=0.25),
),
stem_chs=24,
downsample='avg',
stem_pool='',
act_layer='relu',
attn_layer='se',
attn_kwargs=dict(rd_ratio=0.25),
),
)
for k in ('resnet50_clip', 'resnet101_clip', 'resnet50x4_clip', 'resnet50x16_clip', 'resnet50x64_clip'):
model_cfgs[k + '_gap'] = replace(model_cfgs[k], head_type='classifier')
Expand Down Expand Up @@ -2340,6 +2351,11 @@ def _cfgr(url='', **kwargs):
'resnet50_mlp.untrained': _cfgr(
input_size=(3, 256, 256), pool_size=(8, 8),
),

'test_byobnet.untrained': _cfgr(
# hf_hub_id='timm/',
input_size=(3, 160, 160), crop_pct=0.875, pool_size=(5, 5),
),
})


Expand Down Expand Up @@ -2719,3 +2735,10 @@ def resnet50_mlp(pretrained=False, **kwargs) -> ByobNet:
"""
"""
return _create_byobnet('resnet50_mlp', pretrained=pretrained, **kwargs)


@register_model
def test_byobnet(pretrained=False, **kwargs) -> ByobNet:
""" Minimal test ResNet (BYOB based) model.
"""
return _create_byobnet('test_byobnet', pretrained=pretrained, **kwargs)
36 changes: 36 additions & 0 deletions timm/models/efficientnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,31 @@ def _arch_def(chs: List[int], group_size: int):
return model


def _gen_test_efficientnet(
variant, channel_multiplier=1.0, depth_multiplier=1.0, pretrained=False, **kwargs):
""" Minimal test EfficientNet generator.
"""
arch_def = [
['cn_r1_k3_s1_e1_c16_skip'],
['er_r1_k3_s2_e4_c24'],
['er_r1_k3_s2_e4_c32'],
['ir_r1_k3_s2_e4_c48_se0.25'],
['ir_r1_k3_s2_e4_c64_se0.25'],
]
round_chs_fn = partial(round_channels, multiplier=channel_multiplier, round_limit=0.)
model_kwargs = dict(
block_args=decode_arch_def(arch_def, depth_multiplier),
num_features=round_chs_fn(256),
stem_size=24,
round_chs_fn=round_chs_fn,
norm_layer=kwargs.pop('norm_layer', None) or partial(nn.BatchNorm2d, **resolve_bn_args(kwargs)),
act_layer=resolve_act_layer(kwargs, 'silu'),
**kwargs,
)
model = _create_effnet(variant, pretrained, **model_kwargs)
return model


def _cfg(url='', **kwargs):
return {
'url': url, 'num_classes': 1000, 'input_size': (3, 224, 224), 'pool_size': (7, 7),
Expand Down Expand Up @@ -1722,6 +1747,10 @@ def _cfg(url='', **kwargs):
#hf_hub_id='timm/',
input_size=(3, 224, 224), crop_pct=0.9),


"test_efficientnet.untrained": _cfg(
# hf_hub_id='timm/'
input_size=(3, 160, 160), pool_size=(5, 5)),
})


Expand Down Expand Up @@ -2697,6 +2726,13 @@ def mobilenet_edgetpu_v2_l(pretrained=False, **kwargs) -> EfficientNet:
return model


@register_model
def test_efficientnet(pretrained=False, **kwargs) -> EfficientNet:
model = _gen_test_efficientnet('test_efficientnet', pretrained=pretrained, **kwargs)
return model



register_model_deprecations(__name__, {
'tf_efficientnet_b0_ap': 'tf_efficientnet_b0.ap_in1k',
'tf_efficientnet_b1_ap': 'tf_efficientnet_b1.ap_in1k',
Expand Down
18 changes: 15 additions & 3 deletions timm/models/vision_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1959,13 +1959,16 @@ def _cfg(url: str = '', **kwargs) -> Dict[str, Any]:
hf_hub_id='timm/',
num_classes=11821,
input_size=(3, 256, 256), crop_pct=0.95),
'vit_base_patch16_reg4_gap_256': _cfg(
'vit_base_patch16_reg4_gap_256.untrained': _cfg(
input_size=(3, 256, 256)),

'vit_so150m_patch16_reg4_gap_256': _cfg(
'vit_so150m_patch16_reg4_gap_256.untrained': _cfg(
input_size=(3, 256, 256)),
'vit_so150m_patch16_reg4_map_256': _cfg(
'vit_so150m_patch16_reg4_map_256.untrained': _cfg(
input_size=(3, 256, 256)),

'test_vit.untrained': _cfg(
input_size=(3, 160, 160), crop_pct=0.875),
}

_quick_gelu_cfgs = [
Expand Down Expand Up @@ -3136,6 +3139,15 @@ def vit_so150m_patch16_reg4_gap_256(pretrained: bool = False, **kwargs) -> Visio
return model


@register_model
def test_vit(pretrained: bool = False, **kwargs) -> VisionTransformer:
""" ViT Test
"""
model_args = dict(patch_size=16, embed_dim=64, depth=6, num_heads=2, mlp_ratio=3)
model = _create_vision_transformer('test_vit', pretrained=pretrained, **dict(model_args, **kwargs))
return model


register_model_deprecations(__name__, {
'vit_tiny_patch16_224_in21k': 'vit_tiny_patch16_224.augreg_in21k',
'vit_small_patch32_224_in21k': 'vit_small_patch32_224.augreg_in21k',
Expand Down
Loading