| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
- """PyTorch SelecSLS Net example for ImageNet Classification
- License: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/legalcode)
- Author: Dushyant Mehta (@mehtadushy)
- SelecSLS (core) Network Architecture as proposed in "XNect: Real-time Multi-person 3D
- Human Pose Estimation with a Single RGB Camera, Mehta et al."
- https://arxiv.org/abs/1907.00837
- Based on ResNet implementation in https://github.com/rwightman/pytorch-image-models
- and SelecSLS Net implementation in https://github.com/mehtadushy/SelecSLS-Pytorch
- """
- from typing import List, Type
- import torch
- import torch.nn as nn
- from timm.data import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD
- from timm.layers import create_classifier
- from ._builder import build_model_with_cfg
- from ._registry import register_model, generate_default_cfgs
- __all__ = ['SelecSls'] # model_registry will add each entrypoint fn to this
- class SequentialList(nn.Sequential):
- def __init__(self, *args):
- super().__init__(*args)
- def forward(self, x) -> List[torch.Tensor]:
- for module in self:
- x = module(x)
- return x
- class SelectSeq(nn.Module):
- def __init__(self, mode='index', index=0):
- super().__init__()
- self.mode = mode
- self.index = index
- def forward(self, x) -> torch.Tensor:
- if self.mode == 'index':
- return x[self.index]
- else:
- return torch.cat(x, dim=1)
- def conv_bn(in_chs, out_chs, k=3, stride=1, padding=None, dilation=1, device=None, dtype=None):
- dd = {'device': device, 'dtype': dtype}
- if padding is None:
- padding = ((stride - 1) + dilation * (k - 1)) // 2
- return nn.Sequential(
- nn.Conv2d(in_chs, out_chs, k, stride, padding=padding, dilation=dilation, bias=False, **dd),
- nn.BatchNorm2d(out_chs, **dd),
- nn.ReLU(inplace=True)
- )
- class SelecSlsBlock(nn.Module):
- def __init__(
- self,
- in_chs: int,
- skip_chs: int,
- mid_chs: int,
- out_chs: int,
- is_first: bool,
- stride: int,
- dilation: int = 1,
- device=None,
- dtype=None,
- ):
- dd = {'device': device, 'dtype': dtype}
- super().__init__()
- self.stride = stride
- self.is_first = is_first
- assert stride in [1, 2]
- # Process input with 4 conv blocks with the same number of input and output channels
- self.conv1 = conv_bn(in_chs, mid_chs, 3, stride, dilation=dilation, **dd)
- self.conv2 = conv_bn(mid_chs, mid_chs, 1, **dd)
- self.conv3 = conv_bn(mid_chs, mid_chs // 2, 3, **dd)
- self.conv4 = conv_bn(mid_chs // 2, mid_chs, 1, **dd)
- self.conv5 = conv_bn(mid_chs, mid_chs // 2, 3, **dd)
- self.conv6 = conv_bn(2 * mid_chs + (0 if is_first else skip_chs), out_chs, 1, **dd)
- def forward(self, x: List[torch.Tensor]) -> List[torch.Tensor]:
- if not isinstance(x, list):
- x = [x]
- assert len(x) in [1, 2]
- d1 = self.conv1(x[0])
- d2 = self.conv3(self.conv2(d1))
- d3 = self.conv5(self.conv4(d2))
- if self.is_first:
- out = self.conv6(torch.cat([d1, d2, d3], 1))
- return [out, out]
- else:
- return [self.conv6(torch.cat([d1, d2, d3, x[1]], 1)), x[1]]
- class SelecSls(nn.Module):
- """SelecSls42 / SelecSls60 / SelecSls84
- Parameters
- ----------
- cfg : network config dictionary specifying block type, feature, and head args
- num_classes : int, default 1000
- Number of classification classes.
- in_chans : int, default 3
- Number of input (color) channels.
- drop_rate : float, default 0.
- Dropout probability before classifier, for training
- global_pool : str, default 'avg'
- Global pooling type. One of 'avg', 'max', 'avgmax', 'catavgmax'
- """
- def __init__(
- self,
- cfg,
- num_classes: int = 1000,
- in_chans: int = 3,
- drop_rate: float = 0.0,
- global_pool: str = 'avg',
- device=None,
- dtype=None,
- ):
- self.num_classes = num_classes
- self.in_chans = in_chans
- super().__init__()
- dd = {'device': device, 'dtype': dtype}
- self.stem = conv_bn(in_chans, 32, stride=2, **dd)
- self.features = SequentialList(*[cfg['block'](*block_args, **dd) for block_args in cfg['features']])
- self.from_seq = SelectSeq() # from List[tensor] -> Tensor in module compatible way
- self.head = nn.Sequential(*[conv_bn(*conv_args, **dd) for conv_args in cfg['head']])
- self.num_features = self.head_hidden_size = cfg['num_features']
- self.feature_info = cfg['feature_info']
- self.global_pool, self.head_drop, self.fc = create_classifier(
- self.num_features,
- self.num_classes,
- pool_type=global_pool,
- drop_rate=drop_rate,
- **dd,
- )
- for n, m in self.named_modules():
- if isinstance(m, nn.Conv2d):
- nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
- @torch.jit.ignore
- def group_matcher(self, coarse=False):
- return dict(
- stem=r'^stem',
- blocks=r'^features\.(\d+)',
- blocks_head=r'^head'
- )
- @torch.jit.ignore
- def set_grad_checkpointing(self, enable=True):
- assert not enable, 'gradient checkpointing not supported'
- @torch.jit.ignore
- def get_classifier(self) -> nn.Module:
- return self.fc
- def reset_classifier(self, num_classes: int, global_pool: str = 'avg'):
- self.num_classes = num_classes
- self.global_pool, self.fc = create_classifier(
- self.num_features,
- self.num_classes,
- pool_type=global_pool,
- device=self.fc.weight.device if hasattr(self.fc, 'weight') else None,
- dtype=self.fc.weight.dtype if hasattr(self.fc, 'weight') else None,
- )
- def forward_features(self, x):
- x = self.stem(x)
- x = self.features(x)
- x = self.head(self.from_seq(x))
- return x
- def forward_head(self, x, pre_logits: bool = False):
- x = self.global_pool(x)
- x = self.head_drop(x)
- return x if pre_logits else self.fc(x)
- def forward(self, x):
- x = self.forward_features(x)
- x = self.forward_head(x)
- return x
- def _create_selecsls(variant, pretrained, **kwargs):
- cfg = {}
- feature_info = [dict(num_chs=32, reduction=2, module='stem.2')]
- if variant.startswith('selecsls42'):
- cfg['block'] = SelecSlsBlock
- # Define configuration of the network after the initial neck
- cfg['features'] = [
- # in_chs, skip_chs, mid_chs, out_chs, is_first, stride
- (32, 0, 64, 64, True, 2),
- (64, 64, 64, 128, False, 1),
- (128, 0, 144, 144, True, 2),
- (144, 144, 144, 288, False, 1),
- (288, 0, 304, 304, True, 2),
- (304, 304, 304, 480, False, 1),
- ]
- feature_info.extend([
- dict(num_chs=128, reduction=4, module='features.1'),
- dict(num_chs=288, reduction=8, module='features.3'),
- dict(num_chs=480, reduction=16, module='features.5'),
- ])
- # Head can be replaced with alternative configurations depending on the problem
- feature_info.append(dict(num_chs=1024, reduction=32, module='head.1'))
- if variant == 'selecsls42b':
- cfg['head'] = [
- (480, 960, 3, 2),
- (960, 1024, 3, 1),
- (1024, 1280, 3, 2),
- (1280, 1024, 1, 1),
- ]
- feature_info.append(dict(num_chs=1024, reduction=64, module='head.3'))
- cfg['num_features'] = 1024
- else:
- cfg['head'] = [
- (480, 960, 3, 2),
- (960, 1024, 3, 1),
- (1024, 1024, 3, 2),
- (1024, 1280, 1, 1),
- ]
- feature_info.append(dict(num_chs=1280, reduction=64, module='head.3'))
- cfg['num_features'] = 1280
- elif variant.startswith('selecsls60'):
- cfg['block'] = SelecSlsBlock
- # Define configuration of the network after the initial neck
- cfg['features'] = [
- # in_chs, skip_chs, mid_chs, out_chs, is_first, stride
- (32, 0, 64, 64, True, 2),
- (64, 64, 64, 128, False, 1),
- (128, 0, 128, 128, True, 2),
- (128, 128, 128, 128, False, 1),
- (128, 128, 128, 288, False, 1),
- (288, 0, 288, 288, True, 2),
- (288, 288, 288, 288, False, 1),
- (288, 288, 288, 288, False, 1),
- (288, 288, 288, 416, False, 1),
- ]
- feature_info.extend([
- dict(num_chs=128, reduction=4, module='features.1'),
- dict(num_chs=288, reduction=8, module='features.4'),
- dict(num_chs=416, reduction=16, module='features.8'),
- ])
- # Head can be replaced with alternative configurations depending on the problem
- feature_info.append(dict(num_chs=1024, reduction=32, module='head.1'))
- if variant == 'selecsls60b':
- cfg['head'] = [
- (416, 756, 3, 2),
- (756, 1024, 3, 1),
- (1024, 1280, 3, 2),
- (1280, 1024, 1, 1),
- ]
- feature_info.append(dict(num_chs=1024, reduction=64, module='head.3'))
- cfg['num_features'] = 1024
- else:
- cfg['head'] = [
- (416, 756, 3, 2),
- (756, 1024, 3, 1),
- (1024, 1024, 3, 2),
- (1024, 1280, 1, 1),
- ]
- feature_info.append(dict(num_chs=1280, reduction=64, module='head.3'))
- cfg['num_features'] = 1280
- elif variant == 'selecsls84':
- cfg['block'] = SelecSlsBlock
- # Define configuration of the network after the initial neck
- cfg['features'] = [
- # in_chs, skip_chs, mid_chs, out_chs, is_first, stride
- (32, 0, 64, 64, True, 2),
- (64, 64, 64, 144, False, 1),
- (144, 0, 144, 144, True, 2),
- (144, 144, 144, 144, False, 1),
- (144, 144, 144, 144, False, 1),
- (144, 144, 144, 144, False, 1),
- (144, 144, 144, 304, False, 1),
- (304, 0, 304, 304, True, 2),
- (304, 304, 304, 304, False, 1),
- (304, 304, 304, 304, False, 1),
- (304, 304, 304, 304, False, 1),
- (304, 304, 304, 304, False, 1),
- (304, 304, 304, 512, False, 1),
- ]
- feature_info.extend([
- dict(num_chs=144, reduction=4, module='features.1'),
- dict(num_chs=304, reduction=8, module='features.6'),
- dict(num_chs=512, reduction=16, module='features.12'),
- ])
- # Head can be replaced with alternative configurations depending on the problem
- cfg['head'] = [
- (512, 960, 3, 2),
- (960, 1024, 3, 1),
- (1024, 1024, 3, 2),
- (1024, 1280, 3, 1),
- ]
- cfg['num_features'] = 1280
- feature_info.extend([
- dict(num_chs=1024, reduction=32, module='head.1'),
- dict(num_chs=1280, reduction=64, module='head.3')
- ])
- else:
- raise ValueError('Invalid net configuration ' + variant + ' !!!')
- cfg['feature_info'] = feature_info
- # this model can do 6 feature levels by default, unlike most others, leave as 0-4 to avoid surprises?
- return build_model_with_cfg(
- SelecSls,
- variant,
- pretrained,
- model_cfg=cfg,
- feature_cfg=dict(out_indices=(0, 1, 2, 3, 4), flatten_sequential=True),
- **kwargs,
- )
- def _cfg(url='', **kwargs):
- return {
- 'url': url,
- 'num_classes': 1000, 'input_size': (3, 224, 224), 'pool_size': (4, 4),
- 'crop_pct': 0.875, 'interpolation': 'bilinear',
- 'mean': IMAGENET_DEFAULT_MEAN, 'std': IMAGENET_DEFAULT_STD,
- 'first_conv': 'stem.0', 'classifier': 'fc',
- 'license': 'cc-by-4.0',
- **kwargs
- }
- default_cfgs = generate_default_cfgs({
- 'selecsls42.untrained': _cfg(
- interpolation='bicubic'),
- 'selecsls42b.in1k': _cfg(
- hf_hub_id='timm/',
- interpolation='bicubic'),
- 'selecsls60.in1k': _cfg(
- hf_hub_id='timm/',
- interpolation='bicubic'),
- 'selecsls60b.in1k': _cfg(
- hf_hub_id='timm/',
- interpolation='bicubic'),
- 'selecsls84.untrained': _cfg(
- interpolation='bicubic'),
- })
- @register_model
- def selecsls42(pretrained=False, **kwargs) -> SelecSls:
- """Constructs a SelecSls42 model.
- """
- return _create_selecsls('selecsls42', pretrained, **kwargs)
- @register_model
- def selecsls42b(pretrained=False, **kwargs) -> SelecSls:
- """Constructs a SelecSls42_B model.
- """
- return _create_selecsls('selecsls42b', pretrained, **kwargs)
- @register_model
- def selecsls60(pretrained=False, **kwargs) -> SelecSls:
- """Constructs a SelecSls60 model.
- """
- return _create_selecsls('selecsls60', pretrained, **kwargs)
- @register_model
- def selecsls60b(pretrained=False, **kwargs) -> SelecSls:
- """Constructs a SelecSls60_B model.
- """
- return _create_selecsls('selecsls60b', pretrained, **kwargs)
- @register_model
- def selecsls84(pretrained=False, **kwargs) -> SelecSls:
- """Constructs a SelecSls84 model.
- """
- return _create_selecsls('selecsls84', pretrained, **kwargs)
|