Convolutional Layers#

Classes#

class Conv(net_out, shapes, *, filters=None, layer=None, factor=None, groups=1, kernel=3, stride=1, padding=0, dropout=0, activation='ELU', norm=None, padding_mode=None, **kwargs)[source]#

Bases: BaseSingleLayer

Convolutional layer constructor.

Supports 1D, 2D, and 3D convolution.

Attributes:
groupint

Layer group, if 0 it will always be used, else it will only be used if its group matches the Networks

descriptionstr

Description of the layer

layersSequential

Layers to loop through in the forward pass

Parameters:
net_outlist[int]

Shape of the network’s output, required only if layer contains factor

shapesShapes

Shape of the outputs from each layer

filtersint | NoneType, Optional

Number of convolutional filters, will be used if provided, else factor will be used

layerint | NoneType, Optional

If factor is not None, which layer for factor to be relative to, if None, network output will be used

factorfloat | NoneType, Optional

Number of convolutional filters equal to the output channels, or if layer is provided, the layer’s channels, multiplied by factor, won’t be used if filters is provided

groupsint, Optional

Number of input channel groups, each with its own convolutional filter(s), input and output channels must both be divisible by the number of groups, default = 1

kernelint | list[int], Optional

Size of the kernel, default = 3

strideint | list[int], Optional

Stride of the kernel, default = 1

paddingint | {‘same’} | list[int], Optional

Input padding, can an int, list of ints or ‘same’ where ‘same’ preserves the input shape, default = 0

dropoutfloat, Optional

Probability of dropout, default = 0

activationstr | NoneType, Optional

Which activation function to use from PyTorch, default = ‘ELU’

norm{NoneType, ‘batch’, ‘layer’}

If batch or layer normalisation should be used

padding_mode{NoneType, ‘zeros’, ‘reflect’, ‘replicate’, ‘circular’}

Padding mode to use from PyTorch, if None zero padding is used if version is >3.9.4 else replication padding is used

**kwargs

Leftover parameters to pass to base layer for checking

class ConvDepth(net_out, shapes, *, filters=None, layer=None, factor=None, kernel=3, stride=1, padding=0, dropout=0, activation='ELU', norm=None, padding_mode=None, **kwargs)[source]#

Bases: Conv

Constructs a depthwise convolutional layer.

Supports 1D, 2D, and 3D convolution.

Attributes:
groupint

Layer group, if 0 it will always be used, else it will only be used if its group matches the Networks

descriptionstr

Description of the layer

layersSequential

Layers to loop through in the forward pass

Parameters:
net_outlist[int]

Shape of the network’s output, required only if layer contains factor

shapesShapes

Shape of the outputs from each layer

filtersint | NoneType, Optional

Number of convolutional filters, will be used if provided, else factor will be used

layerint | NoneType, Optional

If factor is not None, which layer for factor to be relative to, if None, network output will be used

factorfloat | NoneType, Optional

Number of convolutional filters equal to the output channels multiplied by factor, won’t be used if filters is provided

kernelint | list[int], Optional

Size of the kernel, default = 3

strideint | list[int], Optional

Stride of the kernel, default = 1

paddingint | {‘same’} | list[int], Optional

Input padding, can an int, list of ints or ‘same’ where ‘same’ preserves the input shape, default = 0

dropoutfloat, Optional

Probability of dropout, default = 0

activationstr | NoneType, Optional

Which activation function to use, default = ‘ELU’

norm{NoneType, ‘batch’, ‘layer’}

If batch or layer normalisation should be used

padding_mode{NoneType, ‘zeros’, ‘reflect’, ‘replicate’, ‘circular’}

Padding mode to use from PyTorch, if None zero padding is used if version is >3.9.4 else replication padding is used

**kwargs

Leftover parameters to pass to base layer for checking

class ConvDepthDownscale(net_out, shapes, *, dropout=0, activation='ELU', norm=None, padding_mode=None, **kwargs)[source]#

Bases: Conv

Constructs depth downscaler using convolution with kernel size of 1.

Attributes:
groupint

Layer group, if 0 it will always be used, else it will only be used if its group matches the Networks

descriptionstr

Description of the layer

layersSequential

Layers to loop through in the forward pass

Parameters:
net_outlist[int]

Shape of the network’s output, required only if layer contains factor

shapes: Shapes

Shape of the outputs from each layer

dropoutfloat, Optional

Probability of dropout, default = 0

activationstr | NoneType, Optional

Which activation function to use, default = ‘ELU’

norm{NoneType, ‘batch’, ‘layer’}

If batch or layer normalisation should be used

padding_mode{NoneType, ‘zeros’, ‘reflect’, ‘replicate’, ‘circular’}

Padding mode to use from PyTorch, if None zero padding is used if version is >3.9.4 else replication padding is used

**kwargs

Leftover parameters to pass to base layer for checking

class ConvDownscale(net_out, shapes, *, filters=None, layer=None, factor=None, scale=2, dropout=0, activation='ELU', norm=None, **kwargs)[source]#

Bases: Conv

Constructs a strided convolutional layer for downscaling.

The scale factor is equal to the stride and kernel size.

Attributes:
groupint

Layer group, if 0 it will always be used, else it will only be used if its group matches the Networks

descriptionstr

Description of the layer

layersSequential

Layers to loop through in the forward pass

Parameters:
net_outlist[int]

Shape of the network’s output, required only if layer contains factor

shapes: Shapes

Shape of the outputs from each layer

filtersint | NoneType, Optional

Number of convolutional filters, will be used if provided, else factor will be used

layerint | NoneType, Optional

If factor is not None, which layer for factor to be relative to, if None, network output will be used

factorfloat | NoneType, Optional

Number of convolutional filters equal to the output channels multiplied by factor, won’t be used if filters is provided

scaleint, Optional

Stride and size of the kernel, which acts as the downscaling factor, default = 2

dropoutfloat, Optional

Probability of dropout, default = 0

activationstr | NoneType, Optional

Which activation function to use, default = ‘ELU’

norm{NoneType, ‘batch’, ‘layer’}

If batch or layer normalisation should be used

**kwargs

Leftover parameters to pass to base layer for checking

class ConvTranspose(net_out, shapes, *, filters=None, layer=None, factor=None, kernel=3, stride=1, out_padding=0, dilation=1, padding=0, dropout=0, padding_mode='zeros', activation='ELU', norm=None, **kwargs)[source]#

Bases: BaseSingleLayer

Constructs a transpose convolutional layer with fractional stride for input upscaling.

Supports 1D, 2D, and 3D transposed convolution.

Attributes:
groupint

Layer group, if 0 it will always be used, else it will only be used if its group matches the Networks

descriptionstr

Description of the layer

layersSequential

Layers to loop through in the forward pass

Parameters:
net_outlist[int]

Shape of the network’s output, required only if layer contains factor

shapes: Shapes

Shape of the outputs from each layer

filtersint | NoneType, Optional

Number of convolutional filters, will be used if provided, else factor will be used

layerint | NoneType, Optional

If factor is not None, which layer for factor to be relative to, if None, network output will be used

factorfloat | NoneType, Optional

Number of convolutional filters equal to the output channels multiplied by factor, won’t be used if filters is provided

kernelint | list[int], Optional

Size of the kernel, default = 3

strideint | list[int], Optional

Stride of the kernel, default = 1

out_paddingint | list[int], Optional

Padding applied to the output, default = 0

dilationint | list[int], Optional

Spacing between kernel points, default = 1

paddingint | {‘same’} | list[int], Optional

Inverse of convolutional padding which removes rows from each dimension in the output, default = 0

dropoutfloat, Optional

Probability of dropout, default = 0

padding_mode{‘zeros’, ‘reflect’, ‘replicate’, ‘circular’}

Padding mode to use from PyTorch

activationstr | NoneType, Optional

Which activation function to use, default = ‘ELU’

norm{NoneType, ‘batch’, ‘layer’}

If batch or layer normalisation should be used

**kwargs

Leftover parameters to pass to base layer for checking

forward(x, *_, **__)[source]#

Forward pass of the transposed convolutional layer

Parameters:
xTensor

Input tensor with shape (N,…) and type float, where N is the batch size

Returns:
Tensor

Output tensor with shape (N,…) and type float

class ConvTransposeUpscale(net_out, shapes, *, filters=None, layer=None, factor=None, scale=2, out_padding=0, dropout=0, activation='ELU', norm=None, **kwargs)[source]#

Bases: ConvTranspose

Constructs an upscaler using a transposed convolutional layer.

Supports 1D, 2D, and 3D transposed convolutional upscaling.

Attributes:
groupint

Layer group, if 0 it will always be used, else it will only be used if its group matches the Networks

descriptionstr

Description of the layer

layersSequential

Layers to loop through in the forward pass

Parameters:
net_outlist[int]

Shape of the network’s output, required only if layer contains factor

shapes: Shapes

Shape of the outputs from each layer

filtersint | NoneType, Optional

Number of convolutional filters, will be used if provided, else factor will be used

layerint | NoneType, Optional

If factor is not None, which layer for factor to be relative to, if None, network output will be used

factorfloat | NoneType, Optional

Number of convolutional filters equal to the output channels multiplied by factor, won’t be used if filters is provided

scaleint | list[int], Optional

Stride and size of the kernel, which acts as the upscaling factor, default = 2

out_paddingint | list[int], Optional

Padding applied to the output, default = 0

dropoutfloat, Optional

Probability of dropout, default = 0

activationstr | NoneType, Optional

Which activation function to use, default = ‘ELU’

norm{NoneType, ‘batch’, ‘layer’}

If batch or layer normalisation should be used

**kwargs

Leftover parameters to pass to base layer for checking

class ConvUpscale(net_out, shapes, *, filters=None, layer=None, factor=None, scale=2, kernel=3, dropout=0, activation='ELU', norm=None, padding_mode=None, **kwargs)[source]#

Bases: Conv

Constructs an upscaler using a convolutional layer and pixel shuffling.

Supports 1D, 2D, and 3D convolutional upscaling.

See Real-Time Single Image and Video Super-Resolution Using an Efficient Sub-Pixel Convolutional Neural Network by Shi et al. (2016) for details.

Attributes:
groupint

Layer group, if 0 it will always be used, else it will only be used if its group matches the Networks

descriptionstr

Description of the layer

layersSequential

Layers to loop through in the forward pass

Parameters:
net_outlist[int]

Shape of the network’s output, required only if layer contains factor

shapes: Shapes

Shape of the outputs from each layer

filtersint | NoneType, Optional

Number of convolutional filters, will be used if provided, else factor will be used

layerint | NoneType, Optional

If factor is not None, which layer for factor to be relative to, if None, network output will be used

factorfloat | NoneType, Optional

Number of convolutional filters equal to the output channels multiplied by factor, won’t be used if filters is provided

scaleint, Optional

Factor to upscale the input by, default = 2

kernelint | list[int], Optional

Size of the kernel, default = 3

dropoutfloat, Optional

Probability of dropout, default = 0

activationstr | NoneType, Optional

Which activation function to use, default = ‘ELU’

norm{NoneType, ‘batch’, ‘layer’}

If batch or layer normalisation should be used

padding_mode{NoneType, ‘zeros’, ‘reflect’, ‘replicate’, ‘circular’}

Padding mode to use from PyTorch, if None zero padding is used if version is >3.9.4 else replicate padding is used

**kwargs

Leftover parameters to pass to base layer for checking

class PixelShuffle(scale, *, shapes=None, **kwargs)[source]#

Bases: BaseLayer

Used for upscaling by scale factor \(r\) for an input \((N,C\times r^n,D_1,...,D_n)\) to an output \((N,C,D_1\times r,...,D_n\times r)\).

Equivalent to torch.nn.PixelShuffle, but for nD.

Attributes:
groupint

Layer group, if 0 it will always be used, else it will only be used if its group matches the Networks

descriptionstr

Description of the layer

Parameters:
scaleint

Upscaling factor

shapes: Shapes | None, Optional

Shape of the outputs from each layer

**kwargs

Leftover parameters to pass to base layer for checking

extra_repr()[source]#

Displays layer parameters when printing the network

Returns:
str

Layer parameters

forward(x, *_, **__)[source]#

Forward pass of pixel shuffle

Parameters:
xTensor

Input tensor with shape \((N,C\times r^n,D_1,...,D_n)\) and type float, where N is the batch size, C is the number of channels, r is the upscaling factor and \(D_n\) is the length of dimension n

Returns:
Tensor

Output tensor with shape \((N,C,D_1\times r,...,D_n\times r)\) and type float