| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279 |
- """ EfficientViT (by MIT Song Han's Lab)
- Paper: `Efficientvit: Enhanced linear attention for high-resolution low-computation visual recognition`
- - https://arxiv.org/abs/2205.14756
- Adapted from official impl at https://github.com/mit-han-lab/efficientvit
- """
- __all__ = ['EfficientVit', 'EfficientVitLarge']
- from typing import List, Optional, Tuple, Type, Union
- from functools import partial
- import torch
- import torch.nn as nn
- import torch.nn.functional as F
- from timm.data import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD
- from timm.layers import SelectAdaptivePool2d, create_conv2d, GELUTanh
- from ._builder import build_model_with_cfg
- from ._features import feature_take_indices
- from ._features_fx import register_notrace_module
- from ._manipulate import checkpoint_seq
- from ._registry import register_model, generate_default_cfgs
- def val2list(x: list or tuple or any, repeat_time=1):
- if isinstance(x, (list, tuple)):
- return list(x)
- return [x for _ in range(repeat_time)]
- def val2tuple(x: list or tuple or any, min_len: int = 1, idx_repeat: int = -1):
- # repeat elements if necessary
- x = val2list(x)
- if len(x) > 0:
- x[idx_repeat:idx_repeat] = [x[idx_repeat] for _ in range(min_len - len(x))]
- return tuple(x)
- def get_same_padding(kernel_size: int or tuple[int, ...]) -> int or tuple[int, ...]:
- if isinstance(kernel_size, tuple):
- return tuple([get_same_padding(ks) for ks in kernel_size])
- else:
- assert kernel_size % 2 > 0, "kernel size should be odd number"
- return kernel_size // 2
- class ConvNormAct(nn.Module):
- def __init__(
- self,
- in_channels: int,
- out_channels: int,
- kernel_size: Union[int, Tuple[int, int]] = 3,
- stride: int = 1,
- dilation: int = 1,
- groups: int = 1,
- bias: bool = False,
- dropout: float = 0.,
- norm_layer: Optional[Type[nn.Module]] = nn.BatchNorm2d,
- act_layer: Optional[Type[nn.Module]] = nn.ReLU,
- device=None,
- dtype=None,
- ):
- dd = {'device': device, 'dtype': dtype}
- super().__init__()
- self.dropout = nn.Dropout(dropout, inplace=False)
- self.conv = create_conv2d(
- in_channels,
- out_channels,
- kernel_size=kernel_size,
- stride=stride,
- dilation=dilation,
- groups=groups,
- bias=bias,
- **dd,
- )
- self.norm = norm_layer(num_features=out_channels, **dd) if norm_layer else nn.Identity()
- self.act = act_layer(inplace=True) if act_layer is not None else nn.Identity()
- def forward(self, x):
- x = self.dropout(x)
- x = self.conv(x)
- x = self.norm(x)
- x = self.act(x)
- return x
- class DSConv(nn.Module):
- def __init__(
- self,
- in_channels: int,
- out_channels: int,
- kernel_size: int = 3,
- stride: int = 1,
- use_bias: Union[bool, Tuple[bool, bool]] = False,
- norm_layer: Union[Type[nn.Module], Tuple[Optional[Type[nn.Module]], ...]] = nn.BatchNorm2d,
- act_layer: Union[Type[nn.Module], Tuple[Optional[Type[nn.Module]], ...]] = (nn.ReLU6, None),
- device=None,
- dtype=None,
- ):
- dd = {'device': device, 'dtype': dtype}
- super().__init__()
- use_bias = val2tuple(use_bias, 2)
- norm_layer = val2tuple(norm_layer, 2)
- act_layer = val2tuple(act_layer, 2)
- self.depth_conv = ConvNormAct(
- in_channels,
- in_channels,
- kernel_size,
- stride,
- groups=in_channels,
- norm_layer=norm_layer[0],
- act_layer=act_layer[0],
- bias=use_bias[0],
- **dd,
- )
- self.point_conv = ConvNormAct(
- in_channels,
- out_channels,
- 1,
- norm_layer=norm_layer[1],
- act_layer=act_layer[1],
- bias=use_bias[1],
- **dd,
- )
- def forward(self, x):
- x = self.depth_conv(x)
- x = self.point_conv(x)
- return x
- class ConvBlock(nn.Module):
- def __init__(
- self,
- in_channels: int,
- out_channels: int,
- kernel_size: int = 3,
- stride: int = 1,
- mid_channels: Optional[int] = None,
- expand_ratio: float = 1,
- use_bias: Union[bool, Tuple[bool, bool]] = False,
- norm_layer: Union[Type[nn.Module], Tuple[Optional[Type[nn.Module]], ...]] = nn.BatchNorm2d,
- act_layer: Union[Type[nn.Module], Tuple[Optional[Type[nn.Module]], ...]] = (nn.ReLU6, None),
- device=None,
- dtype=None,
- ):
- dd = {'device': device, 'dtype': dtype}
- super().__init__()
- use_bias = val2tuple(use_bias, 2)
- norm_layer = val2tuple(norm_layer, 2)
- act_layer = val2tuple(act_layer, 2)
- mid_channels = mid_channels or round(in_channels * expand_ratio)
- self.conv1 = ConvNormAct(
- in_channels,
- mid_channels,
- kernel_size,
- stride,
- norm_layer=norm_layer[0],
- act_layer=act_layer[0],
- bias=use_bias[0],
- **dd,
- )
- self.conv2 = ConvNormAct(
- mid_channels,
- out_channels,
- kernel_size,
- 1,
- norm_layer=norm_layer[1],
- act_layer=act_layer[1],
- bias=use_bias[1],
- **dd,
- )
- def forward(self, x):
- x = self.conv1(x)
- x = self.conv2(x)
- return x
- class MBConv(nn.Module):
- def __init__(
- self,
- in_channels: int,
- out_channels: int,
- kernel_size: int = 3,
- stride: int = 1,
- mid_channels: Optional[int] = None,
- expand_ratio: float = 6,
- use_bias: Union[bool, Tuple[bool, ...]] = False,
- norm_layer: Union[Type[nn.Module], Tuple[Optional[Type[nn.Module]], ...]] = nn.BatchNorm2d,
- act_layer: Union[Type[nn.Module], Tuple[Optional[Type[nn.Module]], ...]] = (nn.ReLU6, nn.ReLU6, None),
- device=None,
- dtype=None,
- ):
- dd = {'device': device, 'dtype': dtype}
- super().__init__()
- use_bias = val2tuple(use_bias, 3)
- norm_layer = val2tuple(norm_layer, 3)
- act_layer = val2tuple(act_layer, 3)
- mid_channels = mid_channels or round(in_channels * expand_ratio)
- self.inverted_conv = ConvNormAct(
- in_channels,
- mid_channels,
- 1,
- stride=1,
- norm_layer=norm_layer[0],
- act_layer=act_layer[0],
- bias=use_bias[0],
- **dd,
- )
- self.depth_conv = ConvNormAct(
- mid_channels,
- mid_channels,
- kernel_size,
- stride=stride,
- groups=mid_channels,
- norm_layer=norm_layer[1],
- act_layer=act_layer[1],
- bias=use_bias[1],
- **dd,
- )
- self.point_conv = ConvNormAct(
- mid_channels,
- out_channels,
- 1,
- norm_layer=norm_layer[2],
- act_layer=act_layer[2],
- bias=use_bias[2],
- **dd,
- )
- def forward(self, x):
- x = self.inverted_conv(x)
- x = self.depth_conv(x)
- x = self.point_conv(x)
- return x
- class FusedMBConv(nn.Module):
- def __init__(
- self,
- in_channels: int,
- out_channels: int,
- kernel_size: int = 3,
- stride: int = 1,
- mid_channels: Optional[int] = None,
- expand_ratio: float = 6,
- groups: int = 1,
- use_bias: Union[bool, Tuple[bool, ...]] = False,
- norm_layer: Union[Type[nn.Module], Tuple[Optional[Type[nn.Module]], ...]] = nn.BatchNorm2d,
- act_layer: Union[Type[nn.Module], Tuple[Optional[Type[nn.Module]], ...]] = (nn.ReLU6, None),
- device=None,
- dtype=None,
- ):
- dd = {'device': device, 'dtype': dtype}
- super().__init__()
- use_bias = val2tuple(use_bias, 2)
- norm_layer = val2tuple(norm_layer, 2)
- act_layer = val2tuple(act_layer, 2)
- mid_channels = mid_channels or round(in_channels * expand_ratio)
- self.spatial_conv = ConvNormAct(
- in_channels,
- mid_channels,
- kernel_size,
- stride=stride,
- groups=groups,
- norm_layer=norm_layer[0],
- act_layer=act_layer[0],
- bias=use_bias[0],
- **dd,
- )
- self.point_conv = ConvNormAct(
- mid_channels,
- out_channels,
- 1,
- norm_layer=norm_layer[1],
- act_layer=act_layer[1],
- bias=use_bias[1],
- **dd,
- )
- def forward(self, x):
- x = self.spatial_conv(x)
- x = self.point_conv(x)
- return x
- class LiteMLA(nn.Module):
- """Lightweight multi-scale linear attention"""
- def __init__(
- self,
- in_channels: int,
- out_channels: int,
- heads: Optional[int] = None,
- heads_ratio: float = 1.0,
- dim: int = 8,
- use_bias: Union[bool, Tuple[bool, ...]] = False,
- norm_layer: Union[Type[nn.Module], Tuple[Optional[Type[nn.Module]], ...]] = (None, nn.BatchNorm2d),
- act_layer: Union[Type[nn.Module], Tuple[Optional[Type[nn.Module]], ...]] = (None, None),
- kernel_func: Type[nn.Module] = nn.ReLU,
- scales: Tuple[int, ...] = (5,),
- eps: float = 1e-5,
- device=None,
- dtype=None,
- ):
- dd = {'device': device, 'dtype': dtype}
- super().__init__()
- self.eps = eps
- heads = heads or int(in_channels // dim * heads_ratio)
- total_dim = heads * dim
- use_bias = val2tuple(use_bias, 2)
- norm_layer = val2tuple(norm_layer, 2)
- act_layer = val2tuple(act_layer, 2)
- self.dim = dim
- self.qkv = ConvNormAct(
- in_channels,
- 3 * total_dim,
- 1,
- bias=use_bias[0],
- norm_layer=norm_layer[0],
- act_layer=act_layer[0],
- **dd,
- )
- self.aggreg = nn.ModuleList([
- nn.Sequential(
- nn.Conv2d(
- 3 * total_dim,
- 3 * total_dim,
- scale,
- padding=get_same_padding(scale),
- groups=3 * total_dim,
- bias=use_bias[0],
- **dd,
- ),
- nn.Conv2d(3 * total_dim, 3 * total_dim, 1, groups=3 * heads, bias=use_bias[0], **dd),
- )
- for scale in scales
- ])
- self.kernel_func = kernel_func(inplace=False)
- self.proj = ConvNormAct(
- total_dim * (1 + len(scales)),
- out_channels,
- 1,
- bias=use_bias[1],
- norm_layer=norm_layer[1],
- act_layer=act_layer[1],
- **dd,
- )
- def _attn(self, q, k, v):
- dtype = v.dtype
- q, k, v = q.float(), k.float(), v.float()
- kv = k.transpose(-1, -2) @ v
- out = q @ kv
- out = out[..., :-1] / (out[..., -1:] + self.eps)
- return out.to(dtype)
- def forward(self, x):
- B, _, H, W = x.shape
- # generate multi-scale q, k, v
- qkv = self.qkv(x)
- multi_scale_qkv = [qkv]
- for op in self.aggreg:
- multi_scale_qkv.append(op(qkv))
- multi_scale_qkv = torch.cat(multi_scale_qkv, dim=1)
- multi_scale_qkv = multi_scale_qkv.reshape(B, -1, 3 * self.dim, H * W).transpose(-1, -2)
- q, k, v = multi_scale_qkv.chunk(3, dim=-1)
- # lightweight global attention
- q = self.kernel_func(q)
- k = self.kernel_func(k)
- v = F.pad(v, (0, 1), mode="constant", value=1.)
- if not torch.jit.is_scripting():
- with torch.autocast(device_type=v.device.type, enabled=False):
- out = self._attn(q, k, v)
- else:
- out = self._attn(q, k, v)
- # final projection
- out = out.transpose(-1, -2).reshape(B, -1, H, W)
- out = self.proj(out)
- return out
- register_notrace_module(LiteMLA)
- class EfficientVitBlock(nn.Module):
- def __init__(
- self,
- in_channels: int,
- heads_ratio: float = 1.0,
- head_dim: int = 32,
- expand_ratio: float = 4,
- norm_layer: Type[nn.Module] = nn.BatchNorm2d,
- act_layer: Type[nn.Module] = nn.Hardswish,
- device=None,
- dtype=None,
- ):
- dd = {'device': device, 'dtype': dtype}
- super().__init__()
- self.context_module = ResidualBlock(
- LiteMLA(
- in_channels=in_channels,
- out_channels=in_channels,
- heads_ratio=heads_ratio,
- dim=head_dim,
- norm_layer=(None, norm_layer),
- **dd,
- ),
- nn.Identity(),
- )
- self.local_module = ResidualBlock(
- MBConv(
- in_channels=in_channels,
- out_channels=in_channels,
- expand_ratio=expand_ratio,
- use_bias=(True, True, False),
- norm_layer=(None, None, norm_layer),
- act_layer=(act_layer, act_layer, None),
- **dd,
- ),
- nn.Identity(),
- )
- def forward(self, x):
- x = self.context_module(x)
- x = self.local_module(x)
- return x
- class ResidualBlock(nn.Module):
- def __init__(
- self,
- main: Optional[nn.Module],
- shortcut: Optional[nn.Module] = None,
- pre_norm: Optional[nn.Module] = None,
- ):
- super().__init__()
- self.pre_norm = pre_norm if pre_norm is not None else nn.Identity()
- self.main = main
- self.shortcut = shortcut
- def forward(self, x):
- res = self.main(self.pre_norm(x))
- if self.shortcut is not None:
- res = res + self.shortcut(x)
- return res
- def build_local_block(
- in_channels: int,
- out_channels: int,
- stride: int,
- expand_ratio: float,
- norm_layer: str,
- act_layer: str,
- fewer_norm: bool = False,
- block_type: str = "default",
- device=None,
- dtype=None,
- ):
- dd = {'device': device, 'dtype': dtype}
- assert block_type in ["default", "large", "fused"]
- if expand_ratio == 1:
- if block_type == "default":
- block = DSConv(
- in_channels=in_channels,
- out_channels=out_channels,
- stride=stride,
- use_bias=(True, False) if fewer_norm else False,
- norm_layer=(None, norm_layer) if fewer_norm else norm_layer,
- act_layer=(act_layer, None),
- **dd,
- )
- else:
- block = ConvBlock(
- in_channels=in_channels,
- out_channels=out_channels,
- stride=stride,
- use_bias=(True, False) if fewer_norm else False,
- norm_layer=(None, norm_layer) if fewer_norm else norm_layer,
- act_layer=(act_layer, None),
- **dd,
- )
- else:
- if block_type == "default":
- block = MBConv(
- in_channels=in_channels,
- out_channels=out_channels,
- stride=stride,
- expand_ratio=expand_ratio,
- use_bias=(True, True, False) if fewer_norm else False,
- norm_layer=(None, None, norm_layer) if fewer_norm else norm_layer,
- act_layer=(act_layer, act_layer, None),
- **dd,
- )
- else:
- block = FusedMBConv(
- in_channels=in_channels,
- out_channels=out_channels,
- stride=stride,
- expand_ratio=expand_ratio,
- use_bias=(True, False) if fewer_norm else False,
- norm_layer=(None, norm_layer) if fewer_norm else norm_layer,
- act_layer=(act_layer, None),
- **dd,
- )
- return block
- class Stem(nn.Sequential):
- def __init__(
- self,
- in_chs: int,
- out_chs: int,
- depth: int,
- norm_layer: Type[nn.Module],
- act_layer: Type[nn.Module],
- block_type: str = 'default',
- device=None,
- dtype=None,
- ):
- super().__init__()
- dd = {'device': device, 'dtype': dtype}
- self.stride = 2
- self.add_module(
- 'in_conv',
- ConvNormAct(
- in_chs,
- out_chs,
- kernel_size=3,
- stride=2,
- norm_layer=norm_layer,
- act_layer=act_layer,
- **dd,
- )
- )
- stem_block = 0
- for _ in range(depth):
- self.add_module(f'res{stem_block}', ResidualBlock(
- build_local_block(
- in_channels=out_chs,
- out_channels=out_chs,
- stride=1,
- expand_ratio=1,
- norm_layer=norm_layer,
- act_layer=act_layer,
- block_type=block_type,
- **dd,
- ),
- nn.Identity(),
- ))
- stem_block += 1
- class EfficientVitStage(nn.Module):
- def __init__(
- self,
- in_chs: int,
- out_chs: int,
- depth: int,
- norm_layer: Type[nn.Module],
- act_layer: Type[nn.Module],
- expand_ratio: float,
- head_dim: int,
- vit_stage: bool = False,
- device=None,
- dtype=None,
- ):
- dd = {'device': device, 'dtype': dtype}
- super().__init__()
- blocks = [ResidualBlock(
- build_local_block(
- in_channels=in_chs,
- out_channels=out_chs,
- stride=2,
- expand_ratio=expand_ratio,
- norm_layer=norm_layer,
- act_layer=act_layer,
- fewer_norm=vit_stage,
- **dd,
- ),
- None,
- )]
- in_chs = out_chs
- if vit_stage:
- # for stage 3, 4
- for _ in range(depth):
- blocks.append(
- EfficientVitBlock(
- in_channels=in_chs,
- head_dim=head_dim,
- expand_ratio=expand_ratio,
- norm_layer=norm_layer,
- act_layer=act_layer,
- **dd,
- )
- )
- else:
- # for stage 1, 2
- for i in range(1, depth):
- blocks.append(ResidualBlock(
- build_local_block(
- in_channels=in_chs,
- out_channels=out_chs,
- stride=1,
- expand_ratio=expand_ratio,
- norm_layer=norm_layer,
- act_layer=act_layer,
- **dd,
- ),
- nn.Identity(),
- ))
- self.blocks = nn.Sequential(*blocks)
- def forward(self, x):
- return self.blocks(x)
- class EfficientVitLargeStage(nn.Module):
- def __init__(
- self,
- in_chs: int,
- out_chs: int,
- depth: int,
- norm_layer: Type[nn.Module],
- act_layer: Type[nn.Module],
- head_dim: int,
- vit_stage: bool = False,
- fewer_norm: bool = False,
- device=None,
- dtype=None,
- ):
- dd = {'device': device, 'dtype': dtype}
- super().__init__()
- blocks = [ResidualBlock(
- build_local_block(
- in_channels=in_chs,
- out_channels=out_chs,
- stride=2,
- expand_ratio=24 if vit_stage else 16,
- norm_layer=norm_layer,
- act_layer=act_layer,
- fewer_norm=vit_stage or fewer_norm,
- block_type='default' if fewer_norm else 'fused',
- **dd,
- ),
- None,
- )]
- in_chs = out_chs
- if vit_stage:
- # for stage 4
- for _ in range(depth):
- blocks.append(
- EfficientVitBlock(
- in_channels=in_chs,
- head_dim=head_dim,
- expand_ratio=6,
- norm_layer=norm_layer,
- act_layer=act_layer,
- **dd,
- )
- )
- else:
- # for stage 1, 2, 3
- for i in range(depth):
- blocks.append(ResidualBlock(
- build_local_block(
- in_channels=in_chs,
- out_channels=out_chs,
- stride=1,
- expand_ratio=4,
- norm_layer=norm_layer,
- act_layer=act_layer,
- fewer_norm=fewer_norm,
- block_type='default' if fewer_norm else 'fused',
- **dd,
- ),
- nn.Identity(),
- ))
- self.blocks = nn.Sequential(*blocks)
- def forward(self, x):
- return self.blocks(x)
- class ClassifierHead(nn.Module):
- def __init__(
- self,
- in_channels: int,
- widths: List[int],
- num_classes: int = 1000,
- dropout: float = 0.,
- norm_layer: Type[nn.Module] = nn.BatchNorm2d,
- act_layer: Optional[Type[nn.Module]] = nn.Hardswish,
- pool_type: str = 'avg',
- norm_eps: float = 1e-5,
- device=None,
- dtype=None,
- ):
- dd = {'device': device, 'dtype': dtype}
- super().__init__()
- self.widths = widths
- self.num_features = widths[-1]
- assert pool_type, 'Cannot disable pooling'
- self.in_conv = ConvNormAct(in_channels, widths[0], 1, norm_layer=norm_layer, act_layer=act_layer, **dd)
- self.global_pool = SelectAdaptivePool2d(pool_type=pool_type, flatten=True)
- self.classifier = nn.Sequential(
- nn.Linear(widths[0], widths[1], bias=False, **dd),
- nn.LayerNorm(widths[1], eps=norm_eps, **dd),
- act_layer(inplace=True) if act_layer is not None else nn.Identity(),
- nn.Dropout(dropout, inplace=False),
- nn.Linear(widths[1], num_classes, bias=True, **dd) if num_classes > 0 else nn.Identity(),
- )
- def reset(self, num_classes: int, pool_type: Optional[str] = None):
- if pool_type is not None:
- assert pool_type, 'Cannot disable pooling'
- self.global_pool = SelectAdaptivePool2d(pool_type=pool_type, flatten=True,)
- if num_classes > 0:
- self.classifier[-1] = nn.Linear(self.num_features, num_classes, bias=True)
- else:
- self.classifier[-1] = nn.Identity()
- def forward(self, x, pre_logits: bool = False):
- x = self.in_conv(x)
- x = self.global_pool(x)
- if pre_logits:
- # cannot slice or iterate with torchscript so, this
- x = self.classifier[0](x)
- x = self.classifier[1](x)
- x = self.classifier[2](x)
- x = self.classifier[3](x)
- else:
- x = self.classifier(x)
- return x
- class EfficientVit(nn.Module):
- def __init__(
- self,
- in_chans: int = 3,
- widths: Tuple[int, ...] = (),
- depths: Tuple[int, ...] = (),
- head_dim: int = 32,
- expand_ratio: float = 4,
- norm_layer: Type[nn.Module] = nn.BatchNorm2d,
- act_layer: Type[nn.Module] = nn.Hardswish,
- global_pool: str = 'avg',
- head_widths: Tuple[int, ...] = (),
- drop_rate: float = 0.0,
- num_classes: int = 1000,
- device=None,
- dtype=None,
- ):
- dd = {'device': device, 'dtype': dtype}
- super().__init__()
- self.grad_checkpointing = False
- self.global_pool = global_pool
- self.num_classes = num_classes
- # input stem
- self.stem = Stem(in_chans, widths[0], depths[0], norm_layer, act_layer, **dd)
- stride = self.stem.stride
- # stages
- self.feature_info = []
- self.stages = nn.Sequential()
- in_channels = widths[0]
- for i, (w, d) in enumerate(zip(widths[1:], depths[1:])):
- self.stages.append(EfficientVitStage(
- in_channels,
- w,
- depth=d,
- norm_layer=norm_layer,
- act_layer=act_layer,
- expand_ratio=expand_ratio,
- head_dim=head_dim,
- vit_stage=i >= 2,
- **dd,
- ))
- stride *= 2
- in_channels = w
- self.feature_info += [dict(num_chs=in_channels, reduction=stride, module=f'stages.{i}')]
- self.num_features = in_channels
- self.head = ClassifierHead(
- self.num_features,
- widths=head_widths,
- num_classes=num_classes,
- dropout=drop_rate,
- pool_type=self.global_pool,
- **dd,
- )
- self.head_hidden_size = self.head.num_features
- @torch.jit.ignore
- def group_matcher(self, coarse=False):
- matcher = dict(
- stem=r'^stem',
- blocks=r'^stages\.(\d+)' if coarse else [
- (r'^stages\.(\d+).downsample', (0,)),
- (r'^stages\.(\d+)\.\w+\.(\d+)', None),
- ]
- )
- return matcher
- @torch.jit.ignore
- def set_grad_checkpointing(self, enable=True):
- self.grad_checkpointing = enable
- @torch.jit.ignore
- def get_classifier(self) -> nn.Module:
- return self.head.classifier[-1]
- def reset_classifier(self, num_classes: int, global_pool: Optional[str] = None):
- self.num_classes = num_classes
- self.head.reset(num_classes, global_pool)
- def forward_intermediates(
- self,
- x: torch.Tensor,
- indices: Optional[Union[int, List[int]]] = None,
- norm: bool = False,
- stop_early: bool = False,
- output_fmt: str = 'NCHW',
- intermediates_only: bool = False,
- ) -> Union[List[torch.Tensor], Tuple[torch.Tensor, List[torch.Tensor]]]:
- """ Forward features that returns intermediates.
- Args:
- x: Input image tensor
- indices: Take last n blocks if int, all if None, select matching indices if sequence
- norm: Apply norm layer to compatible intermediates
- stop_early: Stop iterating over blocks when last desired intermediate hit
- output_fmt: Shape of intermediate feature outputs
- intermediates_only: Only return intermediate features
- Returns:
- """
- assert output_fmt in ('NCHW',), 'Output shape must be NCHW.'
- intermediates = []
- take_indices, max_index = feature_take_indices(len(self.stages), indices)
- # forward pass
- x = self.stem(x)
- if torch.jit.is_scripting() or not stop_early: # can't slice blocks in torchscript
- stages = self.stages
- else:
- stages = self.stages[:max_index + 1]
- for feat_idx, stage in enumerate(stages):
- if self.grad_checkpointing and not torch.jit.is_scripting():
- x = checkpoint_seq(stages, x)
- else:
- x = stage(x)
- if feat_idx in take_indices:
- intermediates.append(x)
- if intermediates_only:
- return intermediates
- return x, intermediates
- def prune_intermediate_layers(
- self,
- indices: Union[int, List[int]] = 1,
- prune_norm: bool = False,
- prune_head: bool = True,
- ):
- """ Prune layers not required for specified intermediates.
- """
- take_indices, max_index = feature_take_indices(len(self.stages), indices)
- self.stages = self.stages[:max_index + 1] # truncate blocks w/ stem as idx 0
- if prune_head:
- self.reset_classifier(0, '')
- return take_indices
- def forward_features(self, x):
- x = self.stem(x)
- if self.grad_checkpointing and not torch.jit.is_scripting():
- x = checkpoint_seq(self.stages, x)
- else:
- x = self.stages(x)
- return x
- def forward_head(self, x, pre_logits: bool = False):
- return self.head(x, pre_logits=pre_logits) if pre_logits else self.head(x)
- def forward(self, x):
- x = self.forward_features(x)
- x = self.forward_head(x)
- return x
- class EfficientVitLarge(nn.Module):
- def __init__(
- self,
- in_chans: int = 3,
- widths: Tuple[int, ...] = (),
- depths: Tuple[int, ...] = (),
- head_dim: int = 32,
- norm_layer: Type[nn.Module] = nn.BatchNorm2d,
- act_layer: Type[nn.Module] = GELUTanh,
- global_pool: str = 'avg',
- head_widths: Tuple[int, ...] = (),
- drop_rate: float = 0.0,
- num_classes: int = 1000,
- norm_eps: float = 1e-7,
- device=None,
- dtype=None,
- ):
- dd = {'device': device, 'dtype': dtype}
- super().__init__()
- self.grad_checkpointing = False
- self.global_pool = global_pool
- self.num_classes = num_classes
- self.norm_eps = norm_eps
- norm_layer = partial(norm_layer, eps=self.norm_eps)
- # input stem
- self.stem = Stem(in_chans, widths[0], depths[0], norm_layer, act_layer, block_type='large', **dd)
- stride = self.stem.stride
- # stages
- self.feature_info = []
- self.stages = nn.Sequential()
- in_channels = widths[0]
- for i, (w, d) in enumerate(zip(widths[1:], depths[1:])):
- self.stages.append(EfficientVitLargeStage(
- in_channels,
- w,
- depth=d,
- norm_layer=norm_layer,
- act_layer=act_layer,
- head_dim=head_dim,
- vit_stage=i >= 3,
- fewer_norm=i >= 2,
- **dd,
- ))
- stride *= 2
- in_channels = w
- self.feature_info += [dict(num_chs=in_channels, reduction=stride, module=f'stages.{i}')]
- self.num_features = in_channels
- self.head = ClassifierHead(
- self.num_features,
- widths=head_widths,
- num_classes=num_classes,
- dropout=drop_rate,
- pool_type=self.global_pool,
- act_layer=act_layer,
- norm_eps=self.norm_eps,
- **dd,
- )
- self.head_hidden_size = self.head.num_features
- @torch.jit.ignore
- def group_matcher(self, coarse=False):
- matcher = dict(
- stem=r'^stem',
- blocks=r'^stages\.(\d+)' if coarse else [
- (r'^stages\.(\d+).downsample', (0,)),
- (r'^stages\.(\d+)\.\w+\.(\d+)', None),
- ]
- )
- return matcher
- @torch.jit.ignore
- def set_grad_checkpointing(self, enable=True):
- self.grad_checkpointing = enable
- @torch.jit.ignore
- def get_classifier(self) -> nn.Module:
- return self.head.classifier[-1]
- def reset_classifier(self, num_classes: int, global_pool: Optional[str] = None):
- self.num_classes = num_classes
- self.head.reset(num_classes, global_pool)
- def forward_intermediates(
- self,
- x: torch.Tensor,
- indices: Optional[Union[int, List[int]]] = None,
- norm: bool = False,
- stop_early: bool = False,
- output_fmt: str = 'NCHW',
- intermediates_only: bool = False,
- ) -> Union[List[torch.Tensor], Tuple[torch.Tensor, List[torch.Tensor]]]:
- """ Forward features that returns intermediates.
- Args:
- x: Input image tensor
- indices: Take last n blocks if int, all if None, select matching indices if sequence
- norm: Apply norm layer to compatible intermediates
- stop_early: Stop iterating over blocks when last desired intermediate hit
- output_fmt: Shape of intermediate feature outputs
- intermediates_only: Only return intermediate features
- Returns:
- """
- assert output_fmt in ('NCHW',), 'Output shape must be NCHW.'
- intermediates = []
- take_indices, max_index = feature_take_indices(len(self.stages), indices)
- # forward pass
- x = self.stem(x)
- if torch.jit.is_scripting() or not stop_early: # can't slice blocks in torchscript
- stages = self.stages
- else:
- stages = self.stages[:max_index + 1]
- for feat_idx, stage in enumerate(stages):
- if self.grad_checkpointing and not torch.jit.is_scripting():
- x = checkpoint_seq(stages, x)
- else:
- x = stage(x)
- if feat_idx in take_indices:
- intermediates.append(x)
- if intermediates_only:
- return intermediates
- return x, intermediates
- def prune_intermediate_layers(
- self,
- indices: Union[int, List[int]] = 1,
- prune_norm: bool = False,
- prune_head: bool = True,
- ):
- """ Prune layers not required for specified intermediates.
- """
- take_indices, max_index = feature_take_indices(len(self.stages), indices)
- self.stages = self.stages[:max_index + 1] # truncate blocks w/ stem as idx 0
- if prune_head:
- self.reset_classifier(0, '')
- return take_indices
- def forward_features(self, x):
- x = self.stem(x)
- if self.grad_checkpointing and not torch.jit.is_scripting():
- x = checkpoint_seq(self.stages, x)
- else:
- x = self.stages(x)
- return x
- def forward_head(self, x, pre_logits: bool = False):
- return self.head(x, pre_logits=pre_logits) if pre_logits else self.head(x)
- def forward(self, x):
- x = self.forward_features(x)
- x = self.forward_head(x)
- return x
- def _cfg(url='', **kwargs):
- return {
- 'url': url,
- 'num_classes': 1000,
- 'mean': IMAGENET_DEFAULT_MEAN,
- 'std': IMAGENET_DEFAULT_STD,
- 'first_conv': 'stem.in_conv.conv',
- 'classifier': 'head.classifier.4',
- 'crop_pct': 0.95,
- 'license': 'apache-2.0',
- 'input_size': (3, 224, 224),
- 'pool_size': (7, 7),
- **kwargs,
- }
- default_cfgs = generate_default_cfgs({
- 'efficientvit_b0.r224_in1k': _cfg(
- hf_hub_id='timm/',
- ),
- 'efficientvit_b1.r224_in1k': _cfg(
- hf_hub_id='timm/',
- ),
- 'efficientvit_b1.r256_in1k': _cfg(
- hf_hub_id='timm/',
- input_size=(3, 256, 256), pool_size=(8, 8), crop_pct=1.0,
- ),
- 'efficientvit_b1.r288_in1k': _cfg(
- hf_hub_id='timm/',
- input_size=(3, 288, 288), pool_size=(9, 9), crop_pct=1.0,
- ),
- 'efficientvit_b2.r224_in1k': _cfg(
- hf_hub_id='timm/',
- ),
- 'efficientvit_b2.r256_in1k': _cfg(
- hf_hub_id='timm/',
- input_size=(3, 256, 256), pool_size=(8, 8), crop_pct=1.0,
- ),
- 'efficientvit_b2.r288_in1k': _cfg(
- hf_hub_id='timm/',
- input_size=(3, 288, 288), pool_size=(9, 9), crop_pct=1.0,
- ),
- 'efficientvit_b3.r224_in1k': _cfg(
- hf_hub_id='timm/',
- ),
- 'efficientvit_b3.r256_in1k': _cfg(
- hf_hub_id='timm/',
- input_size=(3, 256, 256), pool_size=(8, 8), crop_pct=1.0,
- ),
- 'efficientvit_b3.r288_in1k': _cfg(
- hf_hub_id='timm/',
- input_size=(3, 288, 288), pool_size=(9, 9), crop_pct=1.0,
- ),
- 'efficientvit_l1.r224_in1k': _cfg(
- hf_hub_id='timm/',
- crop_pct=1.0,
- ),
- 'efficientvit_l2.r224_in1k': _cfg(
- hf_hub_id='timm/',
- crop_pct=1.0,
- ),
- 'efficientvit_l2.r256_in1k': _cfg(
- hf_hub_id='timm/',
- input_size=(3, 256, 256), pool_size=(8, 8), crop_pct=1.0,
- ),
- 'efficientvit_l2.r288_in1k': _cfg(
- hf_hub_id='timm/',
- input_size=(3, 288, 288), pool_size=(9, 9), crop_pct=1.0,
- ),
- 'efficientvit_l2.r384_in1k': _cfg(
- hf_hub_id='timm/',
- input_size=(3, 384, 384), pool_size=(12, 12), crop_pct=1.0,
- ),
- 'efficientvit_l3.r224_in1k': _cfg(
- hf_hub_id='timm/',
- crop_pct=1.0,
- ),
- 'efficientvit_l3.r256_in1k': _cfg(
- hf_hub_id='timm/',
- input_size=(3, 256, 256), pool_size=(8, 8), crop_pct=1.0,
- ),
- 'efficientvit_l3.r320_in1k': _cfg(
- hf_hub_id='timm/',
- input_size=(3, 320, 320), pool_size=(10, 10), crop_pct=1.0,
- ),
- 'efficientvit_l3.r384_in1k': _cfg(
- hf_hub_id='timm/',
- input_size=(3, 384, 384), pool_size=(12, 12), crop_pct=1.0,
- ),
- # 'efficientvit_l0_sam.sam': _cfg(
- # # hf_hub_id='timm/',
- # input_size=(3, 512, 512), crop_pct=1.0,
- # num_classes=0,
- # ),
- # 'efficientvit_l1_sam.sam': _cfg(
- # # hf_hub_id='timm/',
- # input_size=(3, 512, 512), crop_pct=1.0,
- # num_classes=0,
- # ),
- # 'efficientvit_l2_sam.sam': _cfg(
- # # hf_hub_id='timm/',f
- # input_size=(3, 512, 512), crop_pct=1.0,
- # num_classes=0,
- # ),
- })
- def _create_efficientvit(variant, pretrained=False, **kwargs):
- out_indices = kwargs.pop('out_indices', (0, 1, 2, 3))
- model = build_model_with_cfg(
- EfficientVit,
- variant,
- pretrained,
- feature_cfg=dict(flatten_sequential=True, out_indices=out_indices),
- **kwargs
- )
- return model
- def _create_efficientvit_large(variant, pretrained=False, **kwargs):
- out_indices = kwargs.pop('out_indices', (0, 1, 2, 3))
- model = build_model_with_cfg(
- EfficientVitLarge,
- variant,
- pretrained,
- feature_cfg=dict(flatten_sequential=True, out_indices=out_indices),
- **kwargs
- )
- return model
- @register_model
- def efficientvit_b0(pretrained=False, **kwargs):
- model_args = dict(
- widths=(8, 16, 32, 64, 128), depths=(1, 2, 2, 2, 2), head_dim=16, head_widths=(1024, 1280))
- return _create_efficientvit('efficientvit_b0', pretrained=pretrained, **dict(model_args, **kwargs))
- @register_model
- def efficientvit_b1(pretrained=False, **kwargs):
- model_args = dict(
- widths=(16, 32, 64, 128, 256), depths=(1, 2, 3, 3, 4), head_dim=16, head_widths=(1536, 1600))
- return _create_efficientvit('efficientvit_b1', pretrained=pretrained, **dict(model_args, **kwargs))
- @register_model
- def efficientvit_b2(pretrained=False, **kwargs):
- model_args = dict(
- widths=(24, 48, 96, 192, 384), depths=(1, 3, 4, 4, 6), head_dim=32, head_widths=(2304, 2560))
- return _create_efficientvit('efficientvit_b2', pretrained=pretrained, **dict(model_args, **kwargs))
- @register_model
- def efficientvit_b3(pretrained=False, **kwargs):
- model_args = dict(
- widths=(32, 64, 128, 256, 512), depths=(1, 4, 6, 6, 9), head_dim=32, head_widths=(2304, 2560))
- return _create_efficientvit('efficientvit_b3', pretrained=pretrained, **dict(model_args, **kwargs))
- @register_model
- def efficientvit_l1(pretrained=False, **kwargs):
- model_args = dict(
- widths=(32, 64, 128, 256, 512), depths=(1, 1, 1, 6, 6), head_dim=32, head_widths=(3072, 3200))
- return _create_efficientvit_large('efficientvit_l1', pretrained=pretrained, **dict(model_args, **kwargs))
- @register_model
- def efficientvit_l2(pretrained=False, **kwargs):
- model_args = dict(
- widths=(32, 64, 128, 256, 512), depths=(1, 2, 2, 8, 8), head_dim=32, head_widths=(3072, 3200))
- return _create_efficientvit_large('efficientvit_l2', pretrained=pretrained, **dict(model_args, **kwargs))
- @register_model
- def efficientvit_l3(pretrained=False, **kwargs):
- model_args = dict(
- widths=(64, 128, 256, 512, 1024), depths=(1, 2, 2, 8, 8), head_dim=32, head_widths=(6144, 6400))
- return _create_efficientvit_large('efficientvit_l3', pretrained=pretrained, **dict(model_args, **kwargs))
- # FIXME will wait for v2 SAM models which are pending
- # @register_model
- # def efficientvit_l0_sam(pretrained=False, **kwargs):
- # # only backbone for segment-anything-model weights
- # model_args = dict(
- # widths=(32, 64, 128, 256, 512), depths=(1, 1, 1, 4, 4), head_dim=32, num_classes=0, norm_eps=1e-6)
- # return _create_efficientvit_large('efficientvit_l0_sam', pretrained=pretrained, **dict(model_args, **kwargs))
- #
- #
- # @register_model
- # def efficientvit_l1_sam(pretrained=False, **kwargs):
- # # only backbone for segment-anything-model weights
- # model_args = dict(
- # widths=(32, 64, 128, 256, 512), depths=(1, 1, 1, 6, 6), head_dim=32, num_classes=0, norm_eps=1e-6)
- # return _create_efficientvit_large('efficientvit_l1_sam', pretrained=pretrained, **dict(model_args, **kwargs))
- #
- #
- # @register_model
- # def efficientvit_l2_sam(pretrained=False, **kwargs):
- # # only backbone for segment-anything-model weights
- # model_args = dict(
- # widths=(32, 64, 128, 256, 512), depths=(1, 2, 2, 8, 8), head_dim=32, num_classes=0, norm_eps=1e-6)
- # return _create_efficientvit_large('efficientvit_l2_sam', pretrained=pretrained, **dict(model_args, **kwargs))
|