glasses.models.classification.resnest package

Module contents

class glasses.models.classification.resnest.ResNeSt(encoder: torch.nn.modules.module.Module = <class 'glasses.models.classification.resnest.ResNeStEncoder'>, *args, **kwargs)[source]

Bases: glasses.models.classification.resnet.ResNet

Implementation of ResNeSt proposed in “ResNeSt: Split-Attention Networks”.

This model beats EfficientNet both in speed and accuracy.

ResNeSt.resnest14d()
ResNeSt.resnest26d()
ResNeSt.resnest50d()
ResNeSt.resnest50d_1s4x24d()
ResNeSt.resnest50d_4s2x40d()
# 'e' models have a bigger start_features (128), resulting in a 64 stem width
ResNeSt.resnest101e()
ResNeSt.resnest200e()
ResNeSt.resnest269e()
# create a ResNeSt50_2s4s40d
ResNeSt.resnet50d(radix=2, groups=4, base_width=80)

Examples

# change activation
ResNeSt.resnest50d(activation = nn.SELU)
# change number of classes (default is 1000 )
ResNeSt.resnest50d(n_classes=100)
# pass a different block
ResNeSt.resnest50d(block=SENetBasicBlock)
# store each feature
x = torch.rand((1, 3, 224, 224))
model = ResNeSt.resnest50d()
# first call .features, this will activate the forward hooks and tells the model you'll like to get the features
model.encoder.features
model(torch.randn((1,3,224,224)))
# get the features from the encoder
features = model.encoder.features
print([x.shape for x in features])
#[torch.Size([1, 64, 112, 112]), torch.Size([1, 64, 56, 56]), torch.Size([1, 128, 28, 28]), torch.Size([1, 256, 14, 14])]
Parameters
  • in_channels (int, optional) – Number of channels in the input Image (3 for RGB and 1 for Gray). Defaults to 3.

  • n_classes (int, optional) – Number of classes. Defaults to 1000.

Initializes internal Module state, shared by both nn.Module and ScriptModule.

classmethod resnest101e(*args, **kwargs) glasses.models.classification.resnest.ResNeSt[source]
classmethod resnest14d(*args, **kwargs) glasses.models.classification.resnest.ResNeSt[source]
classmethod resnest200e(*args, **kwargs) glasses.models.classification.resnest.ResNeSt[source]
classmethod resnest269e(*args, **kwargs) glasses.models.classification.resnest.ResNeSt[source]
classmethod resnest26d(*args, **kwargs) glasses.models.classification.resnest.ResNeSt[source]
classmethod resnest50d(*args, **kwargs) glasses.models.classification.resnest.ResNeSt[source]
classmethod resnest50d_1s4x24d(*args, **kwargs) glasses.models.classification.resnest.ResNeSt[source]
classmethod resnest50d_4s2x40d(*args, **kwargs) glasses.models.classification.resnest.ResNeSt[source]
classmethod resnest50d_fast(*args, **kwargs) glasses.models.classification.resnest.ResNeSt[source]
training: bool
class glasses.models.classification.resnest.ResNeStBottleneckBlock(in_features: int, out_features: int, stride: int = 1, radix: int = 2, groups: int = 1, fast: bool = True, reduction: int = 4, activation: torch.nn.modules.module.Module = functools.partial(<class 'torch.nn.modules.activation.ReLU'>, inplace=True), drop_block_p: float = 0, **kwargs)[source]

Bases: glasses.models.classification.resnetxt.ResNetXtBottleNeckBlock

Implementation of ResNeSt Bottleneck Block proposed in proposed in “ResNeSt: Split-Attention Networks”. It subclasses ResNetXtBottleNeckBlock to use the inner features calculation based on the reduction and groups widths.

It uses SplitAttention after the 3x3 conv and an AvgPool2d layer instead of a strided 3x3 convolution to downsample the input.

DropBlock is added after every convolution operation.

https://github.com/FrancescoSaverioZuppichini/glasses/blob/develop/docs/_static/images/resnet/ResNeStBlock.jpg?raw=true
Parameters
  • in_features (int) – [description]

  • out_features (int) – [description]

  • reduction (int, optional) – Reduction used in the bottleneck. Defaults to 4.

  • stride (int, optional) – Stride that was originally applied to the 3x3 conv. Defaults to 1.

  • radix (int, optional) – Number of subgroups. Defaults to 2.

  • groups (int, optional) – Number of groups. Defaults to 1.

  • fast (bool, optional) – If True, the pooling is applied before the 3x3 conv, this improves performance. Defaults to True.

  • activation (nn.Module, optional) – [description]. Defaults to ReLUInPlace.

training: bool
class glasses.models.classification.resnest.ResNeStEncoder(in_channels: int = 3, start_features: int = 64, widths: List[int] = [64, 128, 256, 512], depths: List[int] = [2, 2, 2, 2], stem: torch.nn.modules.module.Module = functools.partial(<class 'glasses.models.classification.resnet.ResNetStem3x3'>, widths=[32, 32]), activation: torch.nn.modules.module.Module = functools.partial(<class 'torch.nn.modules.activation.ReLU'>, inplace=True), block: torch.nn.modules.module.Module = <class 'glasses.models.classification.resnest.ResNeStBottleneckBlock'>, downsample_first: bool = False, drop_block_p: float = 0.2, **kwargs)[source]

Bases: glasses.models.classification.resnet.ResNetEncoder

Initializes internal Module state, shared by both nn.Module and ScriptModule.

training: bool
class glasses.models.classification.resnest.SplitAtt(in_features: int, features: int, radix: int, groups: int)[source]

Bases: torch.nn.modules.module.Module

Implementation of Split Attention proposed in “ResNeSt: Split-Attention Networks”

Grouped convolution have been proved to be impirically better (ResNetXt). The main idea is to apply an attention group-wise.

Einops is used to improve the readibility of this module

Parameters
  • in_features (int) – number of input features

  • features (int) – attention’s features

  • radix (int) – number of subgroups (radix) in the groups

  • groups (int) – number of groups, each group contains radix subgroups

forward(x: torch.Tensor) torch.Tensor[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool