unimodals package

Subpackages

Submodules

unimodals.MVAE module

Implements various encoders and decoders for MVAE.

class unimodals.MVAE.DeLeNet(in_channels, arg_channels, additional_layers, latent)

Bases: Module

Implements an image deconvolution decoder for MVAE.

__init__(in_channels, arg_channels, additional_layers, latent)

Instantiate DeLeNet Module.

Parameters:
  • in_channels (int) – Number of input channels

  • arg_channels (int) – Number of arg channels

  • additional_layers (int) – Number of additional layers.

  • latent (int) – Latent dimension size

forward(x)

Apply DeLeNet to Layer Input.

Parameters:

x (torch.Tensor) – Layer Input

Returns:

Layer Output

Return type:

torch.Tensor

training: bool
class unimodals.MVAE.LeNetEncoder(in_channels, arg_channels, additional_layers, latent, twooutput=True)

Bases: Module

Implements a LeNet Encoder for MVAE.

__init__(in_channels, arg_channels, additional_layers, latent, twooutput=True)

Instantiate LeNetEncoder Module

Parameters:
  • in_channels (int) – Input Dimensions

  • arg_channels (int) – Arg channels dimension size

  • additional_layers (int) – Number of additional layers

  • latent (int) – Latent dimension size

  • twooutput (bool, optional) – Whether to output twice the size of the latent. Defaults to True.

forward(x)

Apply LeNetEncoder to Layer Input.

Parameters:

x (torch.Tensor) – Layer Input

Returns:

Layer Output

Return type:

torch.Tensor

training: bool
class unimodals.MVAE.MLPEncoder(indim, hiddim, outdim)

Bases: Module

Implements MLP Encoder for MVAE.

__init__(indim, hiddim, outdim)

Initialzies MLPEncoder Object.

Parameters:
  • indim (int) – Input Dimension

  • hiddim (int) – Hidden Dimension

  • outdim (int) – Output Dimension

forward(x)

Apply MLPEncoder to Input.

Parameters:

x (torch.Tensor) – Layer Input

Returns:

Layer Output

Return type:

torch.Tensor

training: bool
class unimodals.MVAE.TSDecoder(indim, outdim, finaldim, timestep)

Bases: Module

Implements a time-series decoder for MVAE.

__init__(indim, outdim, finaldim, timestep)

Instantiate TSDecoder Module.

Parameters:
  • indim (int) – Input dimension

  • outdim (int) – (unused) Output dimension

  • finaldim (int) – Hidden dimension

  • timestep (int) – Number of timesteps

forward(x)

Apply TSDecoder to layer input.

Parameters:

x (torch.Tensor) – Layer Input

Returns:

Layer Output

Return type:

torch.Tensor

training: bool
class unimodals.MVAE.TSEncoder(indim, outdim, finaldim, timestep, returnvar=True, batch_first=False)

Bases: Module

Implements a time series encoder for MVAE.

__init__(indim, outdim, finaldim, timestep, returnvar=True, batch_first=False)

Instantiate TSEncoder Module.

Parameters:
  • indim (int) – Input Dimension of GRU

  • outdim (int) – Output dimension of GRU

  • finaldim (int) – Output dimension of TSEncoder

  • timestep (float) – Number of timestamps

  • returnvar (bool, optional) – Whether to return the output split with the first encoded portion and the next or not. Defaults to True.

  • batch_first (bool, optional) – Whether the batching dimension is the first dimension of the input or not. Defaults to False.

forward(x)

Apply TS Encoder to Layer Input.

Parameters:

x (torch.Tensor) – Layer Input

Returns:

Layer Output

Return type:

torch.Tensor

training: bool

unimodals.common_models module

Implements common unimodal encoders.

class unimodals.common_models.Constant(out_dim)

Bases: Module

Implements a module that returns a constant no matter the input.

__init__(out_dim)

Initialize Constant Module.

Parameters:

out_dim (int) – Output Dimension.

forward(x)

Apply Constant to Layer Input.

Parameters:

x (torch.Tensor) – Layer Input

Returns:

Layer Output

Return type:

torch.Tensor

training: bool
class unimodals.common_models.DAN(indim, hiddim, dropout=False, dropoutp=0.25, nlayers=3, has_padding=False)

Bases: Module

Deep Averaging Network: https://people.cs.umass.edu/~miyyer/pubs/2015_acl_dan.pdf Deep Sets: https://arxiv.org/abs/1703.06114

__init__(indim, hiddim, dropout=False, dropoutp=0.25, nlayers=3, has_padding=False)

Initialize DAN Object.

Parameters:
  • indim (int) – Input Dimension

  • hiddim (int) – Hidden Dimension

  • dropout (bool, optional) – Whether to apply dropout to layer output. Defaults to False.

  • dropoutp (float, optional) – Dropout probability. Defaults to 0.25.

  • nlayers (int, optional) – Number of layers. Defaults to 3.

  • has_padding (bool, optional) – Whether the input has padding. Defaults to False.

forward(x)

Apply DAN to input.

Parameters:

x (torch.Tensor) – Layer Input

Returns:

Layer Output

Return type:

torch.Tensor

training: bool
class unimodals.common_models.GRU(indim, hiddim, dropout=False, dropoutp=0.1, flatten=False, has_padding=False, last_only=False, batch_first=True)

Bases: Module

Implements Gated Recurrent Unit (GRU).

__init__(indim, hiddim, dropout=False, dropoutp=0.1, flatten=False, has_padding=False, last_only=False, batch_first=True)

Initialize GRU Module.

Parameters:
  • indim (int) – Input dimension

  • hiddim (int) – Hidden dimension

  • dropout (bool, optional) – Whether to apply dropout layer or not. Defaults to False.

  • dropoutp (float, optional) – Dropout probability. Defaults to 0.1.

  • flatten (bool, optional) – Whether to flatten output before returning. Defaults to False.

  • has_padding (bool, optional) – Whether the input has padding or not. Defaults to False.

  • last_only (bool, optional) – Whether to return only the last output of the GRU. Defaults to False.

  • batch_first (bool, optional) – Whether to batch before applying or not. Defaults to True.

forward(x)

Apply GRU to input.

Parameters:

x (torch.Tensor) – Layer Input

Returns:

Layer Output

Return type:

torch.Tensor

training: bool
class unimodals.common_models.GRUWithLinear(indim, hiddim, outdim, dropout=False, dropoutp=0.1, flatten=False, has_padding=False, output_each_layer=False, batch_first=False)

Bases: Module

Implements a GRU with Linear Post-Processing.

__init__(indim, hiddim, outdim, dropout=False, dropoutp=0.1, flatten=False, has_padding=False, output_each_layer=False, batch_first=False)

Initialize GRUWithLinear Module.

Parameters:
  • indim (int) – Input Dimension

  • hiddim (int) – Hidden Dimension

  • outdim (int) – Output Dimension

  • dropout (bool, optional) – Whether to apply dropout or not. Defaults to False.

  • dropoutp (float, optional) – Dropout probability. Defaults to 0.1.

  • flatten (bool, optional) – Whether to flatten output before returning. Defaults to False.

  • has_padding (bool, optional) – Whether input has padding. Defaults to False.

  • output_each_layer (bool, optional) – Whether to return the output of every intermediate layer. Defaults to False.

  • batch_first (bool, optional) – Whether to apply batching before GRU. Defaults to False.

forward(x)

Apply GRUWithLinear to Input.

Parameters:

x (torch.Tensor) – Layer Input

Returns:

Layer Output

Return type:

torch.Tensor

training: bool
class unimodals.common_models.GlobalPooling2D

Bases: Module

Implements 2D Global Pooling.

__init__()

Initializes GlobalPooling2D Module.

forward(x)

Apply 2D Global Pooling to Layer Input.

Parameters:

x (torch.Tensor) – Layer Input

Returns:

Layer Output

Return type:

torch.Tensor

training: bool
class unimodals.common_models.Identity

Bases: Module

Identity Module.

__init__()

Initialize Identity Module.

forward(x)

Apply Identity to Input.

Parameters:

x (torch.Tensor) – Layer Input

Returns:

Layer Output

Return type:

torch.Tensor

training: bool
class unimodals.common_models.LSTM(indim, hiddim, linear_layer_outdim=None, dropout=False, dropoutp=0.1, flatten=False, has_padding=False)

Bases: Module

Extends nn.LSTM with dropout and other features.

__init__(indim, hiddim, linear_layer_outdim=None, dropout=False, dropoutp=0.1, flatten=False, has_padding=False)

Initialize LSTM Object.

Parameters:
  • indim (int) – Input Dimension

  • hiddim (int) – Hidden Layer Dimension

  • linear_layer_outdim (int, optional) – Linear Layer Output Dimension. Defaults to None.

  • dropout (bool, optional) – Whether to apply dropout to layer output. Defaults to False.

  • dropoutp (float, optional) – Dropout probability. Defaults to 0.1.

  • flatten (bool, optional) – Whether to flatten out. Defaults to False.

  • has_padding (bool, optional) – Whether input has padding. Defaults to False.

forward(x)

Apply LSTM to layer input.

Parameters:

x (torch.Tensor) – Layer Input

Returns:

Layer Output

Return type:

torch.Tensor

training: bool
class unimodals.common_models.LeNet(in_channels, args_channels, additional_layers, output_each_layer=False, linear=None, squeeze_output=True)

Bases: Module

Implements LeNet.

Adapted from centralnet code https://github.com/slyviacassell/_MFAS/blob/master/models/central/avmnist.py.

__init__(in_channels, args_channels, additional_layers, output_each_layer=False, linear=None, squeeze_output=True)

Initialize LeNet.

Parameters:
  • in_channels (int) – Input channel number.

  • args_channels (int) – Output channel number for block.

  • additional_layers (int) – Number of additional blocks for LeNet.

  • output_each_layer (bool, optional) – Whether to return the output of all layers. Defaults to False.

  • linear (tuple, optional) – Tuple of (input_dim, output_dim) for optional linear layer post-processing. Defaults to None.

  • squeeze_output (bool, optional) – Whether to squeeze output before returning. Defaults to True.

forward(x)

Apply LeNet to layer input.

Parameters:

x (torch.Tensor) – Layer Input

Returns:

Layer Output

Return type:

torch.Tensor

training: bool
class unimodals.common_models.Linear(indim, outdim, xavier_init=False)

Bases: Module

Linear Layer with Xavier Initialization, and 0 Bias.

__init__(indim, outdim, xavier_init=False)

Initialize Linear Layer w/ Xavier Init.

Parameters:
  • indim (int) – Input Dimension

  • outdim (int) – Output Dimension

  • xavier_init (bool, optional) – Whether to apply Xavier Initialization to Layer. Defaults to False.

forward(x)

Apply Linear Layer to Input.

Parameters:

x (torch.Tensor) – Input Tensor

Returns:

Output Tensor

Return type:

torch.Tensor

training: bool
class unimodals.common_models.MLP(indim, hiddim, outdim, dropout=False, dropoutp=0.1, output_each_layer=False)

Bases: Module

Two layered perceptron.

__init__(indim, hiddim, outdim, dropout=False, dropoutp=0.1, output_each_layer=False)

Initialize two-layered perceptron.

Parameters:
  • indim (int) – Input dimension

  • hiddim (int) – Hidden layer dimension

  • outdim (int) – Output layer dimension

  • dropout (bool, optional) – Whether to apply dropout or not. Defaults to False.

  • dropoutp (float, optional) – Dropout probability. Defaults to 0.1.

  • output_each_layer (bool, optional) – Whether to return outputs of each layer as a list. Defaults to False.

forward(x)

Apply MLP to Input.

Parameters:

x (torch.Tensor) – Layer Input

Returns:

Layer Output

Return type:

torch.Tensor

training: bool
class unimodals.common_models.MaxOut_MLP(num_outputs, first_hidden=64, number_input_feats=300, second_hidden=None, linear_layer=True)

Bases: Module

Implements Maxout w/ MLP.

__init__(num_outputs, first_hidden=64, number_input_feats=300, second_hidden=None, linear_layer=True)

Instantiate MaxOut_MLP Module.

Parameters:
  • num_outputs (int) – Output dimension

  • first_hidden (int, optional) – First hidden layer dimension. Defaults to 64.

  • number_input_feats (int, optional) – Input dimension. Defaults to 300.

  • second_hidden (_type_, optional) – Second hidden layer dimension. Defaults to None.

  • linear_layer (bool, optional) – Whether to include an output hidden layer or not. Defaults to True.

forward(x)

Apply module to layer input

Parameters:

x (torch.Tensor) – Layer Input

Returns:

Layer Output

Return type:

torch.Tensor

training: bool
class unimodals.common_models.Maxout(d, m, k)

Bases: Module

Implements Maxout module.

__init__(d, m, k)

Initialize Maxout object.

Parameters:
  • d (int) – (Unused)

  • m (int) – Number of features remeaining after Maxout.

  • k (int) – Pool Size

forward(inputs)

Apply Maxout to inputs.

Parameters:

inputs (torch.Tensor) – Layer Input

Returns:

Layer Output

Return type:

torch.Tensor

training: bool
class unimodals.common_models.ResNetLSTMEnc(hiddim, dropout=False, dropoutp=0.1)

Bases: Module

Implements an encoder which applies as ResNet first, and then an LSTM.

__init__(hiddim, dropout=False, dropoutp=0.1)

Instantiates ResNetLSTMEnc Module

Parameters:
  • hiddim (int) – Hidden dimension size of LSTM.

  • dropout (bool, optional) – Whether to apply dropout or not.. Defaults to False.

  • dropoutp (float, optional) – Dropout probability. Defaults to 0.1.

forward(x)

Apply ResNetLSTMEnc Module to Input

Parameters:

x (torch.Tensor) – Layer Input

Returns:

Layer Output

Return type:

torch.Tensor

training: bool
class unimodals.common_models.Reshape(shape)

Bases: Module

Custom reshape module for easier Sequential usage.

__init__(shape)

Initialize Reshape Module.

Parameters:

shape (tuple) – Tuple to reshape input to

forward(x)

Apply Reshape Module to Input.

Parameters:

x (torch.Tensor) – Layer Input

Returns:

Layer Output

Return type:

torch.Tensor

training: bool
class unimodals.common_models.Sequential(*args, **kwargs)

Bases: Sequential

Custom Sequential module for easier usage.

__init__(*args, **kwargs)

Initialize Sequential Layer.

forward(*args, **kwargs)

Apply args to Sequential Layer.

class unimodals.common_models.Squeeze(dim=None)

Bases: Module

Custom squeeze module for easier Sequential usage.

__init__(dim=None)

Initialize Squeeze Module.

Parameters:

dim (int, optional) – Dimension to Squeeze on. Defaults to None.

forward(x)

Apply Squeeze Layer to Input.

Parameters:

x (torch.Tensor) – Layer Input

Returns:

Layer Output

Return type:

torch.Tensor

training: bool
class unimodals.common_models.Transformer(n_features, dim)

Bases: Module

Extends nn.Transformer.

__init__(n_features, dim)

Initialize Transformer object.

Parameters:
  • n_features (int) – Number of features in the input.

  • dim (int) – Dimension which to embed upon / Hidden dimension size.

forward(x)

Apply Transformer to Input.

Parameters:

x (torch.Tensor) – Layer Input

Returns:

Layer Output

Return type:

torch.Tensor

training: bool
class unimodals.common_models.Transpose(dim0, dim1)

Bases: Module

Custom transpose module for easier Sequential usage.

__init__(dim0, dim1)

Initialize Transpose Module.

Parameters:
  • dim0 (int) – Dimension 1 of Torch.Transpose

  • dim1 (int) – Dimension 2 of Torch.Transpose

forward(x)

Apply Transpose Module to Input.

Parameters:

x (torch.Tensor) – Layer Input

Returns:

Layer Output

Return type:

torch.Tensor

training: bool
class unimodals.common_models.TwoLayersLSTM(indim, hiddim, dropout=False, dropoutp=0.1, flatten=False, has_padding=False, LayNorm=True, isBidirectional=True)

Bases: Module

Implements and Extends nn.LSTM for 2-layer LSTMs.

__init__(indim, hiddim, dropout=False, dropoutp=0.1, flatten=False, has_padding=False, LayNorm=True, isBidirectional=True)

Initialize TwoLayersLSTM Object.

Parameters:
  • indim (int) – Input dimension

  • hiddim (int) – Hidden layer dimension

  • dropout (bool, optional) – Whether to apply dropout to layer output. Defaults to False.

  • dropoutp (float, optional) – Dropout probability. Defaults to 0.1.

  • flatten (bool, optional) – Whether to flatten layer output before returning. Defaults to False.

  • has_padding (bool, optional) – Whether input has padding or not. Defaults to False.

  • isBidirectional (bool, optional) – Whether internal LSTMs are bidirectional. Defaults to True.

forward(x)

Apply TwoLayersLSTM to input.

Parameters:

x (torch.Tensor) – Layer Input

Returns:

Layer Output

Return type:

torch.Tensor

training: bool
class unimodals.common_models.VGG(num_outputs)

Bases: Module

Extends tmodels.vgg19 module with Global Pooling, BatchNorm, and a Linear Output.

__init__(num_outputs)

Initialize VGG Object.

Parameters:

num_outputs (int) – Output Dimension

forward(x)

Apply VGG Module to Input.

Parameters:

x (torch.Tensor) – Input Tensor

Returns:

Output Tensor

Return type:

torch.Tensor

training: bool
class unimodals.common_models.VGG11Pruned(hiddim, dropout=True, prune_factor=0.25, dropoutp=0.2)

Bases: Module

Extends VGG11 and prunes layers to make it even smaller.

Slimmer version of vgg11 model with fewer layers in classifier.

__init__(hiddim, dropout=True, prune_factor=0.25, dropoutp=0.2)

Initialize VGG11Pruned Object.

Parameters:
  • hiddim (int) – Hidden Layer Dimension

  • dropout (bool, optional) – Whether to apply dropout after ReLU. Defaults to True.

  • prune_factor (float, optional) – Percentage of channels to prune. Defaults to 0.25.

  • dropoutp (float, optional) – Dropout probability. Defaults to 0.2.

forward(x)

Apply VGG11Pruned to layer input.

Parameters:

x (torch.Tensor) – Layer Input

Returns:

Layer Output

Return type:

torch.Tensor

training: bool
class unimodals.common_models.VGG11Slim(hiddim, dropout=True, dropoutp=0.2, pretrained=True, freeze_features=True)

Bases: Module

Extends VGG11 with a fewer layers in the classifier.

Slimmer version of vgg11 model with fewer layers in classifier.

__init__(hiddim, dropout=True, dropoutp=0.2, pretrained=True, freeze_features=True)

Initialize VGG11Slim Object.

Parameters:
  • hiddim (int) – Hidden dimension size

  • dropout (bool, optional) – Whether to apply dropout to output of ReLU. Defaults to True.

  • dropoutp (float, optional) – Dropout probability. Defaults to 0.2.

  • pretrained (bool, optional) – Whether to instantiate VGG11 from Pretrained. Defaults to True.

  • freeze_features (bool, optional) – Whether to keep VGG11 features frozen. Defaults to True.

forward(x)

Apply VGG11Slim to Layer Input.

Parameters:

x (torch.Tensor) – Layer Input

Returns:

Layer Output

Return type:

torch.Tensor

training: bool
class unimodals.common_models.VGG16(hiddim, pretrained=True)

Bases: Module

Extends VGG16 for encoding.

__init__(hiddim, pretrained=True)

Initialize VGG16 Object.

Parameters:
  • hiddim (int) – Size of post-processing layer

  • pretrained (bool, optional) – Whether to instantiate VGG16 from pretrained. Defaults to True.

forward(x)

Apply VGG16 to Input.

Parameters:

x (torch.Tensor) – Layer Input

Returns:

Layer Output

Return type:

torch.Tensor

training: bool
class unimodals.common_models.VGG16Pruned(hiddim, dropout=True, prune_factor=0.25, dropoutp=0.2)

Bases: Module

Extends VGG16 and prunes layers to make it even smaller.

Slimmer version of vgg16 model with fewer layers in classifier.

__init__(hiddim, dropout=True, prune_factor=0.25, dropoutp=0.2)

Initialize VGG16Pruned Object.

Parameters:
  • hiddim (int) – Hidden Layer Dimension

  • dropout (bool, optional) – Whether to apply dropout after ReLU. Defaults to True.

  • prune_factor (float, optional) – Percentage of channels to prune. Defaults to 0.25.

  • dropoutp (float, optional) – Dropout probability. Defaults to 0.2.

forward(x)

Apply VGG16Pruned to layer input.

Parameters:

x (torch.Tensor) – Layer Input

Returns:

Layer Output

Return type:

torch.Tensor

training: bool
class unimodals.common_models.VGG16Slim(hiddim, dropout=True, dropoutp=0.2, pretrained=True)

Bases: Module

Extends VGG16 with a fewer layers in the classifier.

Slimmer version of vgg16 model with fewer layers in classifier.

__init__(hiddim, dropout=True, dropoutp=0.2, pretrained=True)

Initialize VGG16Slim object.

Parameters:
  • hiddim (int) – Hidden dimension size

  • dropout (bool, optional) – Whether to apply dropout to ReLU output. Defaults to True.

  • dropoutp (float, optional) – Dropout probability. Defaults to 0.2.

  • pretrained (bool, optional) – Whether to initialize VGG16 from pretrained. Defaults to True.

forward(x)

Apply VGG16Slim to model input.

Parameters:

x (torch.Tensor) – Layer Input

Returns:

Layer Output

Return type:

torch.Tensor

training: bool

unimodals.res3d module

Implements 3dResnet.

Copied from https://github.com/kenshohara/3D-ResNets-PyTorch/blob/master/models/resnet2p1d.py

class unimodals.res3d.BasicBlock(in_planes, planes, stride=1, downsample=None)

Bases: Module

Implements basic block of a resnet.

__init__(in_planes, planes, stride=1, downsample=None)

Instantiate BasicBlock Module.

Parameters:
  • in_planes (int) – Number of input channels

  • planes (int) – Number of output channels

  • stride (int, optional) – Convolution Stride. Defaults to 1.

  • downsample (nn.Module, optional) – Optional Downsampling Layer. Defaults to None.

expansion = 1
forward(x)

Apply Block to Layer Input.

Parameters:

x (torch.Tensor) – Layer Input

Returns:

Layer Output

Return type:

torch.Tensor

training: bool
class unimodals.res3d.Bottleneck(in_planes, planes, stride=1, downsample=None)

Bases: Module

Implements bottleneck block of a resnet.

__init__(in_planes, planes, stride=1, downsample=None)

Instantiate Bottleneck Module.

Parameters:
  • in_planes (int) – Number of input channels

  • planes (int) – Number of output channels

  • stride (int, optional) – Convolution Stride. Defaults to 1.

  • downsample (nn.Module, optional) – Optional Downsampling Layer. Defaults to None.

expansion = 4
forward(x)

Apply Bottleneck to Layer Input.

Parameters:

x (torch.Tensor) – Layer Input

Returns:

Layer Output

Return type:

torch.Tensor

training: bool
class unimodals.res3d.ResNet(block, layers, block_inplanes, n_input_channels=3, conv1_t_size=7, conv1_t_stride=1, no_max_pool=False, shortcut_type='B', widen_factor=1.0, n_classes=400)

Bases: Module

Implements a ResNet from scratch.

__init__(block, layers, block_inplanes, n_input_channels=3, conv1_t_size=7, conv1_t_stride=1, no_max_pool=False, shortcut_type='B', widen_factor=1.0, n_classes=400)

Instantiate 3DResNet

Parameters:
  • block (nn.Module) – Block definition

  • layers (list[int]) – List of number of blocks for 3d resnet

  • block_inplanes (list[int]) – In-channel count per block

  • n_input_channels (int, optional) – Number of input channels. Defaults to 3.

  • conv1_t_size (int, optional) – Convolution input kernel size. Defaults to 7.

  • conv1_t_stride (int, optional) – Convolution input stride size. Defaults to 1.

  • no_max_pool (bool, optional) – Whether to not apply max pooling or not. Defaults to False.

  • shortcut_type (str, optional) – Whether to apply downsampling or not. Defaults to ‘B’.

  • widen_factor (float, optional) – Widen factor. Defaults to 1.0.

  • n_classes (int, optional) – Number of classes in output. Defaults to 400.

forward(x)

Apply ResNet3D to Layer Input

Parameters:

x (torch.Tensor) – Layer Input

Returns:

Layer Output

Return type:

torch.Tensor

training: bool
unimodals.res3d.generate_model(model_depth, **kwargs)

Generate model given different standard model depths.

Module contents