Models

Classes and methods which can be used to create and configure model architectures.

Base Model

class tensornet.models.BaseModel[source]

This is the parent class for all the models that are to be created using TensorNet.

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

This function defines the forward pass of the model.

Parameters

x (torch.Tensor) – Input.

Returns

Model output.

Return type

(torch.Tensor)

summary(input_size: Tuple[int])[source]

Generates model summary.

Parameters

input_size (tuple) – Size of input to the model.

create_learner(train_loader, optimizer, criterion, device='cpu', epochs=1, l1_factor=0.0, val_loader=None, callbacks=None, metrics=None, activate_loss_logits=False, record_train=True)[source]

Create Learner object.

Parameters
  • train_loader (torch.utils.data.DataLoader) – Training data loader.

  • optimizer (torch.optim) – Optimizer for the model.

  • criterion (torch.nn) – Loss Function.

  • device (str or torch.device) – Device where the data will be loaded.

  • epochs (int, optional) – Numbers of epochs to train the model. (default: 1)

  • l1_factor (float, optional) – L1 regularization factor. (default: 0)

  • val_loader (torch.utils.data.DataLoader, optional) – Validation data loader.

  • callbacks (list, optional) – List of callbacks to be used during training.

  • track (str, optional) – Can be set to either ‘epoch’ or ‘batch’ and will store the changes in loss and accuracy for each batch or the entire epoch respectively. (default: ‘epoch’)

  • metrics (list, optional) – List of names of the metrics for model evaluation.

set_learner(learner: tensornet.engine.learner.Learner)[source]

Assign a learner object to the model.

Parameters

learner (Learner) – Learner object.

fit(train_loader, optimizer, criterion, device='cpu', epochs=1, l1_factor=0.0, val_loader=None, callbacks=None, metrics=None, activate_loss_logits=False, record_train=True, start_epoch=1, verbose=True)[source]

Train the model.

Parameters
  • train_loader (torch.utils.data.DataLoader) – Training data loader.

  • optimizer (torch.optim) – Optimizer for the model.

  • criterion (torch.nn) – Loss Function.

  • device (str or torch.device) – Device where the data will be loaded.

  • epochs (int, optional) – Numbers of epochs to train the model. (default: 1)

  • l1_factor (float, optional) – L1 regularization factor. (default: 0)

  • val_loader (torch.utils.data.DataLoader, optional) – Validation data loader.

  • callbacks (list, optional) – List of callbacks to be used during training.

  • track (str, optional) – Can be set to either ‘epoch’ or ‘batch’ and will store the changes in loss and accuracy for each batch or the entire epoch respectively. (default: ‘epoch’)

  • metrics (list, optional) – List of names of the metrics for model evaluation.

  • record_train (bool, optional) – If False, metrics will be calculated only during validation. (default: True)

  • activate_loss_logits (bool, optional) – If True, the logits will first pass through the activate_logits function before going to the criterion. (default: False)

  • start_epoch (int, optional) – Starting epoch number to display during training. (default: 1)

  • verbose (bool, optional) – Print loss and metrics. (default: True)

rfit(start_epoch=1, epochs=None, verbose=True)[source]
evaluate(loader, verbose=True, log_message='Evaluation')[source]

Evaluate the model on a custom data loader.

Parameters
  • loader (torch.utils.data.DataLoader) – Data loader.

  • verbose (bool, optional) – Print loss and metrics. (default: True)

  • log_message (str) – Prefix for the logs which are printed at the end.

Returns

loss and metric values

save(filepath: str, **kwargs)[source]

Save the model.

Parameters
  • filepath (str) – File in which the model will be saved.

  • **kwargs – Additional parameters to save with the model.

load(filepath: str) → dict[source]

Load the model and return the additional parameters saved in in the checkpoint file.

Parameters

filepath (str) – File in which the model is be saved.

Returns

Parameters saved inside the checkpoint file.

Return type

(dict)

training: bool

ResNet

class tensornet.models.ResNet(block: Type[Union[tensornet.models.resnet.BasicBlock, tensornet.models.resnet.Bottleneck]], layers: List[int], num_classes: int = 1000, zero_init_residual: bool = False, groups: int = 1, width_per_group: int = 64, replace_stride_with_dilation: Optional[List[bool]] = None, norm_layer: Optional[Callable[[], torch.nn.modules.module.Module]] = None)[source]

Residual-Net (ResNet)

Note: This model inherits the BaseModel class.

Parameters
  • block (BasicBlock or Bottleneck) – Type of block to use for the model.

  • layers (list) – Number of blocks for each layer.

  • num_classes (int, optional) – Number of classes. (default: 1000)

  • zero_init_residual (bool, optional) – Make residual branch behave like an identity. (default: False)

  • groups (int, optional) – Number of groups per block. (default: 1)

  • width_per_group (int, optional) – Width for each group. (default: 64)

  • replace_stride_with_dilation (list , optional) – Replace stride with dilation for each layer.

  • norm_layer (nn.Module , optional) – Normalization Layer.

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

This function defines the forward pass of the model.

Parameters

x (torch.Tensor) – Input.

Returns

Model output.

Return type

(torch.Tensor)

training: bool
tensornet.models.resnet18(pretrained: bool = False, progress: bool = True, **kwargs: Any) → tensornet.models.resnet.ResNet[source]

ResNet-18 model from “Deep Residual Learning for Image Recognition”.

Parameters
  • pretrained (bool) – If True, returns a model pre-trained on ImageNet

  • progress (bool) – If True, displays a progress bar of the download to stderr

tensornet.models.resnet34(pretrained: bool = False, progress: bool = True, **kwargs: Any) → tensornet.models.resnet.ResNet[source]

ResNet-34 model from “Deep Residual Learning for Image Recognition”.

Parameters
  • pretrained (bool) – If True, returns a model pre-trained on ImageNet

  • progress (bool) – If True, displays a progress bar of the download to stderr

tensornet.models.resnet50(pretrained: bool = False, progress: bool = True, **kwargs: Any) → tensornet.models.resnet.ResNet[source]

ResNet-50 model from “Deep Residual Learning for Image Recognition”.

Parameters
  • pretrained (bool) – If True, returns a model pre-trained on ImageNet

  • progress (bool) – If True, displays a progress bar of the download to stderr

tensornet.models.resnet101(pretrained: bool = False, progress: bool = True, **kwargs: Any) → tensornet.models.resnet.ResNet[source]

ResNet-101 model from “Deep Residual Learning for Image Recognition”.

Parameters
  • pretrained (bool) – If True, returns a model pre-trained on ImageNet

  • progress (bool) – If True, displays a progress bar of the download to stderr

tensornet.models.resnet152(pretrained: bool = False, progress: bool = True, **kwargs: Any) → tensornet.models.resnet.ResNet[source]

ResNet-152 model from “Deep Residual Learning for Image Recognition”.

Parameters
  • pretrained (bool) – If True, returns a model pre-trained on ImageNet

  • progress (bool) – If True, displays a progress bar of the download to stderr

tensornet.models.resnext50_32x4d(pretrained: bool = False, progress: bool = True, **kwargs: Any) → tensornet.models.resnet.ResNet[source]

ResNeXt-50 32x4d model from “Aggregated Residual Transformation for Deep Neural Networks”.

Parameters
  • pretrained (bool) – If True, returns a model pre-trained on ImageNet

  • progress (bool) – If True, displays a progress bar of the download to stderr

tensornet.models.resnext101_32x8d(pretrained: bool = False, progress: bool = True, **kwargs: Any) → tensornet.models.resnet.ResNet[source]

ResNeXt-101 32x8d model from “Aggregated Residual Transformation for Deep Neural Networks”.

Parameters
  • pretrained (bool) – If True, returns a model pre-trained on ImageNet

  • progress (bool) – If True, displays a progress bar of the download to stderr

tensornet.models.wide_resnet50_2(pretrained: bool = False, progress: bool = True, **kwargs: Any) → tensornet.models.resnet.ResNet[source]

Wide ResNet-50-2 model from “Wide Residual Networks”. The model is the same as ResNet except for the bottleneck number of channels which is twice larger in every block. The number of channels in outer 1x1 convolutions is the same, e.g. last block in ResNet-50 has 2048-512-2048 channels, and in Wide ResNet-50-2 has 2048-1024-2048.

Parameters
  • pretrained (bool) – If True, returns a model pre-trained on ImageNet

  • progress (bool) – If True, displays a progress bar of the download to stderr

tensornet.models.wide_resnet101_2(pretrained: bool = False, progress: bool = True, **kwargs: Any) → tensornet.models.resnet.ResNet[source]

Wide ResNet-101-2 model from “Wide Residual Networks”. The model is the same as ResNet except for the bottleneck number of channels which is twice larger in every block. The number of channels in outer 1x1 convolutions is the same, e.g. last block in ResNet-50 has 2048-512-2048 channels, and in Wide ResNet-50-2 has 2048-1024-2048.

Parameters
  • pretrained (bool) – If True, returns a model pre-trained on ImageNet

  • progress (bool) – If True, displays a progress bar of the download to stderr

MobilenetV2

class tensornet.models.MobileNetV2(num_classes=1000, width_mult=1.0, inverted_residual_setting=None, round_nearest=8, block=None, norm_layer=None)[source]

MobileNet V2

Note: This model inherits the BaseModel class.

Parameters
  • num_classes (int, optional) – Number of classes. (default: 1000)

  • width_mult (float, optional) – Width multiplier - adjusts number of channels in each layer by this amount. (default: 1.0)

  • inverted_residual_setting (optional) – Network structure.

  • round_nearest (int, optional) – Round the number of channels in each layer to be a multiple of this number. Set to 1 to turn off rounding. (default: 8)

  • block (optional) – Module specifying inverted residual building block for mobilenet.

  • norm_layer (optional) – Module specifying the normalization layer to use.

forward(x)[source]

This function defines the forward pass of the model.

Parameters

x (torch.Tensor) – Input.

Returns

Model output.

Return type

(torch.Tensor)

training: bool
tensornet.models.mobilenet_v2(pretrained=False, progress=True, **kwargs)[source]

Constructs a MobileNetV2 architecture from “MobileNetV2: Inverted Residuals and Linear Bottlenecks”.

Parameters
  • pretrained (bool, optional) – If True, returns a model pre-trained on ImageNet. (default=False)

  • progress (bool, optional) – If True, displays a progress bar of the download to stderr. (default=True)

Depth Estimation and Segmentation ResNet

class tensornet.models.DSResNet[source]

A U-Net Inspired model for Monocular Depth Estimation and Image Segmentation.

For information check the Depth-Estimation-Segmentation repository.

Note: This model inherits the BaseModel class.

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

This function defines the forward pass of the model.

Parameters

x (torch.Tensor) – Input.

Returns

Model output.

Return type

(torch.Tensor)

training: bool