glasses.models.classification.densenet package¶
Module contents¶
- class glasses.models.classification.densenet.DenseBottleNeckBlock(in_features: int, out_features: int, activation: torch.nn.modules.module.Module = functools.partial(<class 'torch.nn.modules.activation.ReLU'>, inplace=True), expansion: int = 4, **kwargs)[source]¶
Bases:
glasses.models.classification.densenet.DenseNetBasicBlock
Bottleneck block composed by two preactivated layer of convolution. The expensive 3x3 conv is computed after a cheap 1x1 conv donwsample the input resulting in less parameters.
- Parameters
out_features (int) – Number of input features
out_features – Number of output features
conv (nn.Module, optional) – [description]. Defaults to nn.Conv2d.
activation (nn.Module, optional) – [description]. Defaults to ReLUInPlace.
expansion (int, optional) – [description]. Defaults to 4.
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- training: bool¶
- class glasses.models.classification.densenet.DenseNet(encoder: torch.nn.modules.module.Module = <class 'glasses.models.classification.densenet.DenseNetEncoder'>, head: torch.nn.modules.module.Module = <class 'glasses.models.classification.resnet.ResNetHead'>, *args, **kwargs)[source]¶
Bases:
glasses.models.classification.base.ClassificationModule
Implementation of DenseNet proposed in Densely Connected Convolutional Networks
Create a default models
DenseNet.densenet121() DenseNet.densenet161() DenseNet.densenet169() DenseNet.densenet201()
Examples
# change activation DenseNet.densenet121(activation = nn.SELU) # change number of classes (default is 1000 ) DenseNet.densenet121(n_classes=100) # pass a different block DenseNet.densenet121(block=...) # change the initial convolution model = DenseNet.densenet121() model.encoder.gate.conv1 = nn.Conv2d(3, 64, kernel_size=3) # store each feature x = torch.rand((1, 3, 224, 224)) model = DenseNet.densenet121() # 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, 128, 28, 28]), torch.Size([1, 256, 14, 14]), torch.Size([1, 512, 7, 7]), torch.Size([1, 1024, 7, 7])]
- 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 densenet121(*args, **kwargs) glasses.models.classification.densenet.DenseNet [source]¶
Creates a densenet121 model. Grow rate is set to 32
- Returns
A densenet121 model
- Return type
- classmethod densenet161(*args, **kwargs) glasses.models.classification.densenet.DenseNet [source]¶
Creates a densenet161 model. Grow rate is set to 48
- Returns
A densenet161 model
- Return type
- classmethod densenet169(*args, **kwargs) glasses.models.classification.densenet.DenseNet [source]¶
Creates a densenet169 model. Grow rate is set to 32
- Returns
A densenet169 model
- Return type
- classmethod densenet201(*args, **kwargs) glasses.models.classification.densenet.DenseNet [source]¶
Creates a densenet201 model. Grow rate is set to 32
- Returns
A densenet201 model
- Return type
- 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¶
- class glasses.models.classification.densenet.DenseNetBasicBlock(in_features: int, out_features: int, activation: torch.nn.modules.module.Module = functools.partial(<class 'torch.nn.modules.activation.ReLU'>, inplace=True), *args, **kwargs)[source]¶
Bases:
torch.nn.modules.module.Module
Basic DenseNet block composed by one 3x3 convs with residual connection. The residual connection is perfomed by concatenate the input and the output.
- Parameters
out_features (int) – Number of input features
out_features – Number of output features
conv (nn.Module, optional) – [description]. Defaults to nn.Conv2d.
activation (nn.Module, optional) – [description]. Defaults to ReLUInPlace.
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- 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¶
- class glasses.models.classification.densenet.DenseNetEncoder(in_channels: int = 3, start_features: int = 64, grow_rate: int = 32, depths: List[int] = [4, 4, 4, 4], 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.densenet.DenseBottleNeckBlock'>, *args, **kwargs)[source]¶
Bases:
glasses.models.classification.resnet.ResNetEncoder
DenseNet encoder composed by multiple DeseNetLayer with increasing features size. The .stem is the same used in ResNet
- Parameters
in_channels (int, optional) – [description]. Defaults to 3.
start_features (int, optional) – [description]. Defaults to 64.
grow_rate (int, optional) – [description]. Defaults to 32.
depths (List[int], optional) – [description]. Defaults to [4, 4, 4, 4].
activation (nn.Module, optional) – [description]. Defaults to ReLUInPlace.
block (nn.Module, optional) – [description]. Defaults to DenseNetBasicBlock.
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- training: bool¶
- class glasses.models.classification.densenet.DenseNetLayer(in_features: int, grow_rate: int = 32, n: int = 4, block: torch.nn.modules.module.Module = <class 'glasses.models.classification.densenet.DenseBottleNeckBlock'>, transition_block: torch.nn.modules.module.Module = <class 'glasses.models.classification.densenet.TransitionBlock'>, *args, **kwargs)[source]¶
Bases:
torch.nn.modules.container.Sequential
A DenseNet layer is composed by n blocks stacked together followed by a transition to downsample the output features.
- Parameters
out_features (int) – Number of input features
grow_rate (int, optional) – [description]. Defaults to 32.
n (int, optional) – [description]. Defaults to 4.
block (nn.Module, optional) – [description]. Defaults to DenseNetBasicBlock.
transition_block (nn.Module, optional) – A module applied after the block(s). Defaults to TransitionBlock.
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- class glasses.models.classification.densenet.TransitionBlock(in_features: int, factor: int = 2, activation: torch.nn.modules.module.Module = functools.partial(<class 'torch.nn.modules.activation.ReLU'>, inplace=True))[source]¶
Bases:
torch.nn.modules.container.Sequential
A transition block is used to downsample the output using 1x1 conv followed by 2x2 average pooling.
- Parameters
out_features (int) – Number of input features
factor (int, optional) – Reduction factor applied on the in_features. Defaults to 2
conv (nn.Module, optional) – [description]. Defaults to nn.Conv2d.
activation (nn.Module, optional) – [description]. Defaults to ReLUInPlace.
Initializes internal Module state, shared by both nn.Module and ScriptModule.