imaginaire.layers package

Submodules

imaginaire.layers.activation_norm module

class imaginaire.layers.activation_norm.AdaptiveNorm(num_features, cond_dims, weight_norm_type='', projection=True, projection_bias=True, separate_projection=False, input_dim=2, activation_norm_type='instance', activation_norm_params=None, apply_noise=False, add_bias=True, input_scale=1.0, init_gain=1.0)[source]

Bases: torch.nn.modules.module.Module

Adaptive normalization layer. The layer first normalizes the input, then performs an affine transformation using parameters computed from the conditional inputs.

Parameters
  • num_features (int) – Number of channels in the input tensor.

  • cond_dims (int) – Number of channels in the conditional inputs.

  • weight_norm_type (str) – Type of weight normalization. 'none', 'spectral', 'weight', or 'weight_demod'.

  • projection (bool) – If True, project the conditional input to gamma and beta using a fully connected layer, otherwise directly use the conditional input as gamma and beta.

  • projection_bias (bool) – projection layer.

  • separate_projection (bool) – If True, we will use two different layers for gamma and beta. Otherwise, we will use one layer. It matters only if you apply any weight norms to this layer.

  • input_dim (int) – Number of dimensions of the input tensor.

  • activation_norm_type (str) – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

forward(x, y, noise=None, **_kwargs)[source]

Adaptive Normalization forward.

Parameters
  • x (N x C1 x * tensor) – Input tensor.

  • y (N x C2 tensor) – Conditional information.

Returns

Output tensor.

Return type

out (N x C1 x * tensor)

training = None
class imaginaire.layers.activation_norm.DualAdaptiveNorm(num_features, cond_dims, projection_bias=True, weight_norm_type='', activation_norm_type='instance', activation_norm_params=None, apply_noise=False, bias_only=False, init_gain=1.0, fc_scale=None, is_spatial=None)[source]

Bases: torch.nn.modules.module.Module

forward(x, *cond_inputs, **_kwargs)[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 = None
class imaginaire.layers.activation_norm.HyperSpatiallyAdaptiveNorm(num_features, cond_dims, num_filters=0, kernel_size=3, weight_norm_type='', activation_norm_type='sync_batch', is_hyper=True)[source]

Bases: torch.nn.modules.module.Module

Spatially Adaptive Normalization (SPADE) initialization.

Parameters
  • num_features (int) – Number of channels in the input tensor.

  • cond_dims (int or list of int) – List of numbers of channels in the conditional input.

  • num_filters (int) – Number of filters in SPADE.

  • kernel_size (int) – Kernel size of the convolutional filters in the SPADE layer.

  • weight_norm_type (str) – Type of weight normalization. 'none', 'spectral', or 'weight'.

  • activation_norm_type (str) – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group'.

  • is_hyper (bool) – Whether to use hyper SPADE.

forward(x, *cond_inputs, norm_weights=(None, None), **_kwargs)[source]

Spatially Adaptive Normalization (SPADE) forward.

Parameters
  • x (4D tensor) – Input tensor.

  • cond_inputs (list of tensors) – Conditional maps for SPADE.

  • norm_weights (5D tensor or list of tensors) – conv weights or

  • biases] ([weights,) –

Returns

Output tensor.

Return type

output (4D tensor)

training = None
class imaginaire.layers.activation_norm.LayerNorm2d(num_features, eps=1e-05, channel_only=False, affine=True)[source]

Bases: torch.nn.modules.module.Module

Layer Normalization as introduced in https://arxiv.org/abs/1607.06450. This is the usual way to apply layer normalization in CNNs. Note that unlike the pytorch implementation which applies per-element scale and bias, here it applies per-channel scale and bias, similar to batch/instance normalization.

Parameters
  • num_features (int) – Number of channels in the input tensor.

  • eps (float, optional, default=1e-5) – a value added to the denominator for numerical stability.

  • affine (bool, optional, default=False) – If True, performs affine transformation after normalization.

forward(x)[source]
Parameters

x (tensor) – Input tensor.

training = None
class imaginaire.layers.activation_norm.PixelLayerNorm(*args, **kwargs)[source]

Bases: torch.nn.modules.module.Module

forward(x)[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 = None
class imaginaire.layers.activation_norm.PixelNorm(learned_scale=False, eps=1e-05, **_kwargs)[source]

Bases: imaginaire.layers.activation_norm.ScaleNorm

training = None
class imaginaire.layers.activation_norm.ScaleNorm(dim=-1, learned_scale=True, eps=1e-05)[source]

Bases: torch.nn.modules.module.Module

Scale normalization: “Transformers without Tears: Improving the Normalization of Self-Attention” Modified from: https://github.com/tnq177/transformers_without_tears

extra_repr()[source]

Set the extra representation of the module

To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.

forward(x)[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 = None
class imaginaire.layers.activation_norm.SpatiallyAdaptiveNorm(num_features, cond_dims, num_filters=128, kernel_size=3, weight_norm_type='', separate_projection=False, activation_norm_type='sync_batch', activation_norm_params=None, bias_only=False, partial=False, interpolation='nearest')[source]

Bases: torch.nn.modules.module.Module

Spatially Adaptive Normalization (SPADE) initialization.

Parameters
  • num_features (int) – Number of channels in the input tensor.

  • cond_dims (int or list of int) – List of numbers of channels in the input.

  • num_filters (int) – Number of filters in SPADE.

  • kernel_size (int) – Kernel size of the convolutional filters in the SPADE layer.

  • weight_norm_type (str) – Type of weight normalization. 'none', 'spectral', or 'weight'.

  • separate_projection (bool) – If True, we will use two different layers for gamma and beta. Otherwise, we will use one layer. It matters only if you apply any weight norms to this layer.

  • activation_norm_type (str) – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

forward(x, *cond_inputs, **_kwargs)[source]

Spatially Adaptive Normalization (SPADE) forward.

Parameters
  • x (N x C1 x H x W tensor) – Input tensor.

  • cond_inputs (list of tensors) – Conditional maps for SPADE.

Returns

Output tensor.

Return type

output (4D tensor)

training = None
class imaginaire.layers.activation_norm.SplitMeanStd(num_features, eps=1e-05, **kwargs)[source]

Bases: torch.nn.modules.module.Module

forward(x)[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 = None
imaginaire.layers.activation_norm.get_activation_norm_layer(num_features, norm_type, input_dim, **norm_params)[source]

Return an activation normalization layer.

Parameters
  • num_features (int) – Number of feature channels.

  • norm_type (str) – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • input_dim (int) – Number of input dimensions.

  • norm_params – Arbitrary keyword arguments that will be used to initialize the activation normalization.

imaginaire.layers.conv module

class imaginaire.layers.conv.Conv1dBlock(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, nonlinearity='none', inplace_nonlinearity=False, apply_noise=False, blur=False, order='CNA', clamp=None, output_scale=None, init_gain=1.0, **_kwargs)[source]

Bases: imaginaire.layers.conv._BaseConvBlock

A Wrapper class that wraps torch.nn.Conv1d with normalization and nonlinearity.

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • out_channels (int) – Number of channels in the output tensor.

  • kernel_size (int or tuple) – Size of the convolving kernel.

  • stride (int or float or tuple, optional, default=1) – Stride of the convolution.

  • padding (int or tuple, optional, default=0) – Zero-padding added to both sides of the input.

  • dilation (int or tuple, optional, default=1) – Spacing between kernel elements.

  • groups (int, optional, default=1) – Number of blocked connections from input channels to output channels.

  • bias (bool, optional, default=True) – If True, adds a learnable bias to the output.

  • padding_mode (string, optional, default='zeros') – Type of padding: 'zeros', 'reflect', 'replicate' or 'circular'.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layer.

  • apply_noise (bool, optional, default=False) – If True, adds Gaussian noise with learnable magnitude to the convolution output.

  • order (str, optional, default='CNA') – Order of operations. 'C': convolution, 'N': normalization, 'A': nonlinear activation. For example, a block initialized with order='CNA' will do convolution first, then normalization, then nonlinearity.

training = None
class imaginaire.layers.conv.Conv2dBlock(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, nonlinearity='none', inplace_nonlinearity=False, apply_noise=False, blur=False, order='CNA', clamp=None, blur_kernel=(1, 3, 3, 1), output_scale=None, init_gain=1.0)[source]

Bases: imaginaire.layers.conv._BaseConvBlock

A Wrapper class that wraps torch.nn.Conv2d with normalization and nonlinearity.

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • out_channels (int) – Number of channels in the output tensor.

  • kernel_size (int or tuple) – Size of the convolving kernel.

  • stride (int or float or tuple, optional, default=1) – Stride of the convolution.

  • padding (int or tuple, optional, default=0) – Zero-padding added to both sides of the input.

  • dilation (int or tuple, optional, default=1) – Spacing between kernel elements.

  • groups (int, optional, default=1) – Number of blocked connections from input channels to output channels.

  • bias (bool, optional, default=True) – If True, adds a learnable bias to the output.

  • padding_mode (string, optional, default='zeros') – Type of padding: 'zeros', 'reflect', 'replicate' or 'circular'.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layer.

  • apply_noise (bool, optional, default=False) – If True, adds Gaussian noise with learnable magnitude to the convolution output.

  • order (str, optional, default='CNA') – Order of operations. 'C': convolution, 'N': normalization, 'A': nonlinear activation. For example, a block initialized with order='CNA' will do convolution first, then normalization, then nonlinearity.

training = None
class imaginaire.layers.conv.Conv3dBlock(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, nonlinearity='none', inplace_nonlinearity=False, apply_noise=False, blur=False, order='CNA', clamp=None, blur_kernel=(1, 3, 3, 1), output_scale=None, init_gain=1.0)[source]

Bases: imaginaire.layers.conv._BaseConvBlock

A Wrapper class that wraps torch.nn.Conv3d with normalization and nonlinearity.

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • out_channels (int) – Number of channels in the output tensor.

  • kernel_size (int or tuple) – Size of the convolving kernel.

  • stride (int or float or tuple, optional, default=1) – Stride of the convolution.

  • padding (int or tuple, optional, default=0) – Zero-padding added to both sides of the input.

  • dilation (int or tuple, optional, default=1) – Spacing between kernel elements.

  • groups (int, optional, default=1) – Number of blocked connections from input channels to output channels.

  • bias (bool, optional, default=True) – If True, adds a learnable bias to the output.

  • padding_mode (string, optional, default='zeros') – Type of padding: 'zeros', 'reflect', 'replicate' or 'circular'.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layer.

  • apply_noise (bool, optional, default=False) – If True, adds Gaussian noise with learnable magnitude to the convolution output.

  • order (str, optional, default='CNA') – Order of operations. 'C': convolution, 'N': normalization, 'A': nonlinear activation. For example, a block initialized with order='CNA' will do convolution first, then normalization, then nonlinearity.

training = None
class imaginaire.layers.conv.Embedding2d(in_channels, out_channels)[source]

Bases: torch.nn.modules.sparse.Embedding

embedding_dim = None
forward(x)[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.

max_norm = None
norm_type = None
num_embeddings = None
padding_idx = None
scale_grad_by_freq = None
sparse = None
weight = None
class imaginaire.layers.conv.Embedding2dBlock(in_features, out_features, bias=True, weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, nonlinearity='none', inplace_nonlinearity=False, apply_noise=False, order='CNA', clamp=None, output_scale=None, init_gain=1.0, **_kwargs)[source]

Bases: imaginaire.layers.conv._BaseConvBlock

training = None
class imaginaire.layers.conv.EmbeddingBlock(in_features, out_features, bias=True, weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, nonlinearity='none', inplace_nonlinearity=False, apply_noise=False, order='CNA', clamp=None, output_scale=None, init_gain=1.0, **_kwargs)[source]

Bases: imaginaire.layers.conv._BaseConvBlock

training = None
class imaginaire.layers.conv.HyperConv2d(in_channels=0, out_channels=0, kernel_size=3, stride=1, padding=1, dilation=1, groups=1, bias=True, padding_mode='zeros')[source]

Bases: torch.nn.modules.module.Module

Hyper Conv2d initialization.

Parameters
  • in_channels (int) – Dummy parameter.

  • out_channels (int) – Dummy parameter.

  • kernel_size (int or tuple) – Dummy parameter.

  • stride (int or float or tuple, optional, default=1) – Stride of the convolution. Default: 1

  • padding (int or tuple, optional, default=0) – Zero-padding added to both sides of the input.

  • padding_mode (string, optional, default='zeros') – 'zeros', 'reflect', 'replicate' or 'circular'.

  • dilation (int or tuple, optional, default=1) – Spacing between kernel elements.

  • groups (int, optional, default=1) – Number of blocked connections from input channels to output channels.

  • bias (bool, optional, default=True) – If True, adds a learnable bias to the output.

forward(x, *args, conv_weights=(None, None), **kwargs)[source]

Hyper Conv2d forward. Convolve x using the provided weight and bias.

Parameters
  • x (N x C x H x W tensor) – Input tensor.

  • conv_weights (N x C2 x C1 x k x k tensor or list of tensors) – Convolution weights or [weight, bias].

Returns

Output tensor.

Return type

y (N x C2 x H x W tensor)

training = None
class imaginaire.layers.conv.HyperConv2dBlock(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, is_hyper_conv=False, is_hyper_norm=False, nonlinearity='none', inplace_nonlinearity=False, apply_noise=False, blur=False, order='CNA', clamp=None)[source]

Bases: imaginaire.layers.conv._BaseHyperConvBlock

A Wrapper class that wraps HyperConv2d with normalization and nonlinearity.

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • out_channels (int) – Number of channels in the output tensor.

  • kernel_size (int or tuple) – Size of the convolving kernel.

  • stride (int or float or tuple, optional, default=1) – Stride of the convolution.

  • padding (int or tuple, optional, default=0) – Zero-padding added to both sides of the input.

  • dilation (int or tuple, optional, default=1) – Spacing between kernel elements.

  • groups (int, optional, default=1) – Number of blocked connections from input channels to output channels.

  • bias (bool, optional, default=True) – If True, adds a learnable bias to the output.

  • padding_mode (string, optional, default='zeros') – Type of padding: 'zeros', 'reflect', 'replicate' or 'circular'.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • is_hyper_conv (bool, optional, default=False) – If True, use HyperConv2d, otherwise use torch.nn.Conv2d.

  • is_hyper_norm (bool, optional, default=False) – If True, use hyper normalizations.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layer.

  • apply_noise (bool, optional, default=False) – If True, adds Gaussian noise with learnable magnitude to the convolution output.

  • order (str, optional, default='CNA') – Order of operations. 'C': convolution, 'N': normalization, 'A': nonlinear activation. For example, a block initialized with order='CNA' will do convolution first, then normalization, then nonlinearity.

training = None
class imaginaire.layers.conv.LinearBlock(in_features, out_features, bias=True, weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, nonlinearity='none', inplace_nonlinearity=False, apply_noise=False, order='CNA', clamp=None, blur_kernel=(1, 3, 3, 1), output_scale=None, init_gain=1.0, **_kwargs)[source]

Bases: imaginaire.layers.conv._BaseConvBlock

A Wrapper class that wraps torch.nn.Linear with normalization and nonlinearity.

Parameters
  • in_features (int) – Number of channels in the input tensor.

  • out_features (int) – Number of channels in the output tensor.

  • bias (bool, optional, default=True) – If True, adds a learnable bias to the output.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layer.

  • apply_noise (bool, optional, default=False) – If True, add Gaussian noise with learnable magnitude after the fully-connected layer.

  • order (str, optional, default='CNA') – Order of operations. 'C': fully-connected, 'N': normalization, 'A': nonlinear activation. For example, a block initialized with order='CNA' will do convolution first, then normalization, then nonlinearity.

training = None
class imaginaire.layers.conv.ModulatedConv2d(in_channels, out_channels, kernel_size, stride, padding, dilation, groups, bias, padding_mode, demodulate=True, eps=1e-08)[source]

Bases: torch.nn.modules.module.Module

extra_repr()[source]

Set the extra representation of the module

To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.

forward(x, style, **_kwargs)[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 = None
class imaginaire.layers.conv.ModulatedConv2dBlock(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, nonlinearity='none', inplace_nonlinearity=False, apply_noise=True, blur=True, order='CNA', demodulate=True, eps=True, style_dim=None, clamp=None, blur_kernel=(1, 3, 3, 1), output_scale=None, init_gain=1.0)[source]

Bases: imaginaire.layers.conv._BaseConvBlock

forward(x, *cond_inputs, **kw_cond_inputs)[source]

Args: x (tensor): Input tensor. cond_inputs (list of tensors) : Conditional input tensors. kw_cond_inputs (dict) : Keyword conditional inputs.

training = None
class imaginaire.layers.conv.MultiOutConv2dBlock(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, nonlinearity='none', inplace_nonlinearity=False, apply_noise=False, blur=False, order='CNA', clamp=None)[source]

Bases: imaginaire.layers.conv._MultiOutBaseConvBlock

A Wrapper class that wraps torch.nn.Conv2d with normalization and nonlinearity. It can return multiple outputs, if some layers in the block return more than one output.

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • out_channels (int) – Number of channels in the output tensor.

  • kernel_size (int or tuple) – Size of the convolving kernel.

  • stride (int or float or tuple, optional, default=1) – Stride of the convolution.

  • padding (int or tuple, optional, default=0) – Zero-padding added to both sides of the input.

  • dilation (int or tuple, optional, default=1) – Spacing between kernel elements.

  • groups (int, optional, default=1) – Number of blocked connections from input channels to output channels.

  • bias (bool, optional, default=True) – If True, adds a learnable bias to the output.

  • padding_mode (string, optional, default='zeros') – Type of padding: 'zeros', 'reflect', 'replicate' or 'circular'.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layer.

  • apply_noise (bool, optional, default=False) – If True, adds Gaussian noise with learnable magnitude to the convolution output.

  • order (str, optional, default='CNA') – Order of operations. 'C': convolution, 'N': normalization, 'A': nonlinear activation. For example, a block initialized with order='CNA' will do convolution first, then normalization, then nonlinearity.

training = None
class imaginaire.layers.conv.PartialConv2d(*args, multi_channel=False, return_mask=True, **kwargs)[source]

Bases: torch.nn.modules.conv.Conv2d

Partial 2D convolution in “Image inpainting for irregular holes using partial convolutions.” Liu et al., ECCV 2018

bias = None
dilation = None
forward(x, mask_in=None)[source]
Parameters
  • x (tensor) – Input tensor.

  • mask_in (tensor, optional, default=``None``) – it masks the valid input region.

groups = None
kernel_size = None
out_channels = None
output_padding = None
padding = None
padding_mode = None
stride = None
transposed = None
weight = None
class imaginaire.layers.conv.PartialConv2dBlock(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, nonlinearity='none', inplace_nonlinearity=False, multi_channel=False, return_mask=True, apply_noise=False, order='CNA', clamp=None)[source]

Bases: imaginaire.layers.conv._BasePartialConvBlock

A Wrapper class that wraps PartialConv2d with normalization and nonlinearity.

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • out_channels (int) – Number of channels in the output tensor.

  • kernel_size (int or tuple) – Size of the convolving kernel.

  • stride (int or float or tuple, optional, default=1) – Stride of the convolution.

  • padding (int or tuple, optional, default=0) – Zero-padding added to both sides of the input.

  • dilation (int or tuple, optional, default=1) – Spacing between kernel elements.

  • groups (int, optional, default=1) – Number of blocked connections from input channels to output channels.

  • bias (bool, optional, default=True) – If True, adds a learnable bias to the output.

  • padding_mode (string, optional, default='zeros') – Type of padding: 'zeros', 'reflect', 'replicate' or 'circular'.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layer.

  • apply_noise (bool, optional, default=False) – If True, adds Gaussian noise with learnable magnitude to the convolution output.

  • order (str, optional, default='CNA') – Order of operations. 'C': convolution, 'N': normalization, 'A': nonlinear activation. For example, a block initialized with order='CNA' will do convolution first, then normalization, then nonlinearity.

  • multi_channel (bool, optional, default=False) – If True, use different masks for different channels.

  • return_mask (bool, optional, default=True) – If True, the forward call also returns a new mask.

training = None
class imaginaire.layers.conv.PartialConv3d(*args, multi_channel=False, return_mask=True, **kwargs)[source]

Bases: torch.nn.modules.conv.Conv3d

Partial 3D convolution in “Image inpainting for irregular holes using partial convolutions.” Liu et al., ECCV 2018

bias = None
dilation = None
forward(x, mask_in=None)[source]
Parameters
  • x (tensor) – Input tensor.

  • mask_in (tensor, optional, default=``None``) – masks the valid input region.

groups = None
kernel_size = None
out_channels = None
output_padding = None
padding = None
padding_mode = None
stride = None
transposed = None
weight = None
class imaginaire.layers.conv.PartialConv3dBlock(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, nonlinearity='none', inplace_nonlinearity=False, multi_channel=False, return_mask=True, apply_noise=False, order='CNA', clamp=None)[source]

Bases: imaginaire.layers.conv._BasePartialConvBlock

A Wrapper class that wraps PartialConv3d with normalization and nonlinearity.

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • out_channels (int) – Number of channels in the output tensor.

  • kernel_size (int or tuple) – Size of the convolving kernel.

  • stride (int or float or tuple, optional, default=1) – Stride of the convolution.

  • padding (int or tuple, optional, default=0) – Zero-padding added to both sides of the input.

  • dilation (int or tuple, optional, default=1) – Spacing between kernel elements.

  • groups (int, optional, default=1) – Number of blocked connections from input channels to output channels.

  • bias (bool, optional, default=True) – If True, adds a learnable bias to the output.

  • padding_mode (string, optional, default='zeros') – Type of padding: 'zeros', 'reflect', 'replicate' or 'circular'.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layer.

  • apply_noise (bool, optional, default=False) – If True, adds Gaussian noise with learnable magnitude to the convolution output.

  • order (str, optional, default='CNA') – Order of operations. 'C': convolution, 'N': normalization, 'A': nonlinear activation. For example, a block initialized with order='CNA' will do convolution first, then normalization, then nonlinearity.

  • multi_channel (bool, optional, default=False) – If True, use different masks for different channels.

  • return_mask (bool, optional, default=True) – If True, the forward call also returns a new mask.

training = None

imaginaire.layers.misc module

class imaginaire.layers.misc.ApplyNoise[source]

Bases: torch.nn.modules.module.Module

Add Gaussian noise to the input tensor.

forward(x, *_args, noise=None, **_kwargs)[source]
Parameters
  • x (tensor) – Input tensor.

  • noise (tensor, optional, default=``None``) – Noise tensor to be added to the input.

training = None
class imaginaire.layers.misc.ConstantInput(channel, size=4)[source]

Bases: torch.nn.modules.module.Module

forward()[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 = None
class imaginaire.layers.misc.PartialSequential(*modules)[source]

Bases: torch.nn.modules.container.Sequential

Sequential block for partial convolutions.

forward(x)[source]
Parameters

x (tensor) – Input tensor.

training = None

imaginaire.layers.non_local module

class imaginaire.layers.non_local.NonLocal2dBlock(in_channels, scale=True, clamp=False, weight_norm_type='none', weight_norm_params=None, bias=True)[source]

Bases: torch.nn.modules.module.Module

Self attention Layer

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • scale (bool, optional, default=True) – If True, scale the output by a learnable parameter.

  • clamp (bool, optional, default=``False``) – If True, clamp the scaling parameter to (-1, 1).

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • bias (bool, optional, default=True) – If True, adds bias in the convolutional blocks.

forward(x)[source]
Parameters

x (tensor) – input feature maps (B X C X W X H)

Returns

  • out (tensor) : self attention value + input feature

  • attention (tensor): B x N x N (N is Width*Height)

Return type

(tuple)

training = None

imaginaire.layers.nonlinearity module

class imaginaire.layers.nonlinearity.ScaledLeakyReLU(negative_slope=0.2, scale=1.4142135623730951, inplace=False)[source]

Bases: torch.nn.modules.module.Module

forward(x)[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 = None
imaginaire.layers.nonlinearity.get_nonlinearity_layer(nonlinearity_type, inplace, **kwargs)[source]

Return a nonlinearity layer.

Parameters
  • nonlinearity_type (str) – Type of nonlinear activation function. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace (bool) – If True, set inplace=True when initializing the nonlinearity layer.

imaginaire.layers.residual module

class imaginaire.layers.residual.DownRes2dBlock(in_channels, out_channels, kernel_size=3, stride=1, padding=1, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, skip_activation_norm=True, skip_nonlinearity=False, nonlinearity='leakyrelu', inplace_nonlinearity=False, apply_noise=False, hidden_channels_equal_out_channels=False, order='CNACNA', pooling=<class 'torch.nn.modules.pooling.AvgPool2d'>, down_factor=2, learn_shortcut=None, clamp=None, output_scale=1)[source]

Bases: imaginaire.layers.residual._BaseDownResBlock

Residual block for 2D input with downsampling.

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • out_channels (int) – Number of channels in the output tensor.

  • kernel_size (int, optional, default=3) – Kernel size for the convolutional filters in the residual link.

  • padding (int, optional, default=1) – Padding size.

  • dilation (int, optional, default=1) – Dilation factor.

  • groups (int, optional, default=1) – Number of convolutional/linear groups.

  • padding_mode (string, optional, default='zeros') – Type of padding: 'zeros', 'reflect', 'replicate' or 'circular'.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • skip_activation_norm (bool, optional, default=True) – If True and learn_shortcut is also True, applies activation norm to the learned shortcut connection.

  • skip_nonlinearity (bool, optional, default=True) – If True and learn_shortcut is also True, applies nonlinearity to the learned shortcut connection.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function in the residual link. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layers.

  • apply_noise (bool, optional, default=False) – If True, adds Gaussian noise with learnable magnitude to the convolution output.

  • hidden_channels_equal_out_channels (bool, optional, default=False) – If True, set the hidden channel number to be equal to the output channel number. If False, the hidden channel number equals to the smaller of the input channel number and the output channel number.

  • order (str, optional, default='CNACNA') – Order of operations in the residual link. 'C': convolution, 'N': normalization, 'A': nonlinear activation.

  • pooling (class, optional, default=nn.AvgPool2d) – Pytorch pooling layer to be used.

  • down_factor (int, optional, default=2) – Downsampling factor.

  • learn_shortcut (bool, optional, default=False) – If True, always use a convolutional shortcut instead of an identity one, otherwise only use a convolutional one if input and output have different number of channels.

training = None
class imaginaire.layers.residual.HyperRes2dBlock(in_channels, out_channels, kernel_size=3, stride=1, padding=1, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_norm_type='', weight_norm_params=None, activation_norm_type='', activation_norm_params=None, skip_activation_norm=True, skip_nonlinearity=False, nonlinearity='leakyrelu', inplace_nonlinearity=False, apply_noise=False, hidden_channels_equal_out_channels=False, order='CNACNA', is_hyper_conv=False, is_hyper_norm=False, learn_shortcut=None, clamp=None, output_scale=1)[source]

Bases: imaginaire.layers.residual._BaseHyperResBlock

Hyper residual block for 2D input.

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • out_channels (int) – Number of channels in the output tensor.

  • kernel_size (int, optional, default=3) – Kernel size for the convolutional filters in the residual link.

  • padding (int, optional, default=1) – Padding size.

  • dilation (int, optional, default=1) – Dilation factor.

  • groups (int, optional, default=1) – Number of convolutional/linear groups.

  • padding_mode (string, optional, default='zeros') – Type of padding: 'zeros', 'reflect', 'replicate' or 'circular'.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • skip_activation_norm (bool, optional, default=True) – If True and learn_shortcut is also True, applies activation norm to the learned shortcut connection.

  • skip_nonlinearity (bool, optional, default=True) – If True and learn_shortcut is also True, applies nonlinearity to the learned shortcut connection.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function in the residual link. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layers.

  • apply_noise (bool, optional, default=False) – If True, adds Gaussian noise with learnable magnitude to the convolution output.

  • hidden_channels_equal_out_channels (bool, optional, default=False) – If True, set the hidden channel number to be equal to the output channel number. If False, the hidden channel number equals to the smaller of the input channel number and the output channel number.

  • order (str, optional, default='CNACNA') – Order of operations in the residual link. 'C': convolution, 'N': normalization, 'A': nonlinear activation.

  • is_hyper_conv (bool, optional, default=False) – If True, use HyperConv2d, otherwise use torch.nn.Conv2d.

  • is_hyper_norm (bool, optional, default=False) – If True, use hyper normalizations.

  • learn_shortcut (bool, optional, default=False) – If True, always use a convolutional shortcut instead of an identity one, otherwise only use a convolutional one if input and output have different number of channels.

training = None
class imaginaire.layers.residual.ModulatedRes2dBlock(in_channels, out_channels, style_dim, kernel_size=3, stride=1, padding=1, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, skip_activation_norm=True, skip_nonlinearity=False, nonlinearity='leakyrelu', inplace_nonlinearity=False, apply_noise=True, hidden_channels_equal_out_channels=False, order='CNACNA', learn_shortcut=None, clamp=None, output_scale=1, demodulate=True, eps=1e-08)[source]

Bases: imaginaire.layers.residual._BaseResBlock

conv_blocks(x, *cond_inputs, **kw_cond_inputs)[source]

Returns the output of the residual branch.

Parameters
  • x (tensor) – Input tensor.

  • cond_inputs (list of tensors) – Conditional input tensors.

  • kw_cond_inputs (dict) – Keyword conditional inputs.

Returns

Output tensor.

Return type

dx (tensor)

training = None
class imaginaire.layers.residual.MultiOutRes2dBlock(in_channels, out_channels, kernel_size=3, stride=1, padding=1, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, skip_activation_norm=True, skip_nonlinearity=False, nonlinearity='leakyrelu', inplace_nonlinearity=False, apply_noise=False, hidden_channels_equal_out_channels=False, order='CNACNA', learn_shortcut=None, clamp=None, output_scale=1, blur=False, upsample_first=True)[source]

Bases: imaginaire.layers.residual._BaseMultiOutResBlock

Residual block for 2D input. It can return multiple outputs, if some layers in the block return more than one output.

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • out_channels (int) – Number of channels in the output tensor.

  • kernel_size (int, optional, default=3) – Kernel size for the convolutional filters in the residual link.

  • padding (int, optional, default=1) – Padding size.

  • dilation (int, optional, default=1) – Dilation factor.

  • groups (int, optional, default=1) – Number of convolutional/linear groups.

  • padding_mode (string, optional, default='zeros') – Type of padding: 'zeros', 'reflect', 'replicate' or 'circular'.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • skip_activation_norm (bool, optional, default=True) – If True and learn_shortcut is also True, applies activation norm to the learned shortcut connection.

  • skip_nonlinearity (bool, optional, default=True) – If True and learn_shortcut is also True, applies nonlinearity to the learned shortcut connection.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function in the residual link. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layers.

  • apply_noise (bool, optional, default=False) – If True, adds Gaussian noise with learnable magnitude to the convolution output.

  • hidden_channels_equal_out_channels (bool, optional, default=False) – If True, set the hidden channel number to be equal to the output channel number. If False, the hidden channel number equals to the smaller of the input channel number and the output channel number.

  • order (str, optional, default='CNACNA') – Order of operations in the residual link. 'C': convolution, 'N': normalization, 'A': nonlinear activation.

  • learn_shortcut (bool, optional, default=False) – If True, always use a convolutional shortcut instead of an identity one, otherwise only use a convolutional one if input and output have different number of channels.

training = None
class imaginaire.layers.residual.PartialRes2dBlock(in_channels, out_channels, kernel_size=3, stride=1, padding=1, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, skip_activation_norm=True, skip_nonlinearity=False, nonlinearity='leakyrelu', inplace_nonlinearity=False, multi_channel=False, return_mask=True, apply_noise=False, hidden_channels_equal_out_channels=False, order='CNACNA', learn_shortcut=None, clamp=None, output_scale=1)[source]

Bases: imaginaire.layers.residual._BasePartialResBlock

Residual block for 2D input with partial convolution.

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • out_channels (int) – Number of channels in the output tensor.

  • kernel_size (int, optional, default=3) – Kernel size for the convolutional filters in the residual link.

  • padding (int, optional, default=1) – Padding size.

  • dilation (int, optional, default=1) – Dilation factor.

  • groups (int, optional, default=1) – Number of convolutional/linear groups.

  • padding_mode (string, optional, default='zeros') – Type of padding: 'zeros', 'reflect', 'replicate' or 'circular'.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • skip_activation_norm (bool, optional, default=True) – If True and learn_shortcut is also True, applies activation norm to the learned shortcut connection.

  • skip_nonlinearity (bool, optional, default=True) – If True and learn_shortcut is also True, applies nonlinearity to the learned shortcut connection.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function in the residual link. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layers.

  • apply_noise (bool, optional, default=False) – If True, adds Gaussian noise with learnable magnitude to the convolution output.

  • hidden_channels_equal_out_channels (bool, optional, default=False) – If True, set the hidden channel number to be equal to the output channel number. If False, the hidden channel number equals to the smaller of the input channel number and the output channel number.

  • order (str, optional, default='CNACNA') – Order of operations in the residual link. 'C': convolution, 'N': normalization, 'A': nonlinear activation.

  • learn_shortcut (bool, optional, default=False) – If True, always use a convolutional shortcut instead of an identity one, otherwise only use a convolutional one if input and output have different number of channels.

training = None
class imaginaire.layers.residual.PartialRes3dBlock(in_channels, out_channels, kernel_size=3, stride=1, padding=1, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, skip_activation_norm=True, skip_nonlinearity=False, nonlinearity='leakyrelu', inplace_nonlinearity=False, multi_channel=False, return_mask=True, apply_noise=False, hidden_channels_equal_out_channels=False, order='CNACNA', learn_shortcut=None, clamp=None, output_scale=1)[source]

Bases: imaginaire.layers.residual._BasePartialResBlock

Residual block for 3D input with partial convolution.

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • out_channels (int) – Number of channels in the output tensor.

  • kernel_size (int, optional, default=3) – Kernel size for the convolutional filters in the residual link.

  • padding (int, optional, default=1) – Padding size.

  • dilation (int, optional, default=1) – Dilation factor.

  • groups (int, optional, default=1) – Number of convolutional/linear groups.

  • padding_mode (string, optional, default='zeros') – Type of padding: 'zeros', 'reflect', 'replicate' or 'circular'.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • skip_activation_norm (bool, optional, default=True) – If True and learn_shortcut is also True, applies activation norm to the learned shortcut connection.

  • skip_nonlinearity (bool, optional, default=True) – If True and learn_shortcut is also True, applies nonlinearity to the learned shortcut connection.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function in the residual link. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layers.

  • apply_noise (bool, optional, default=False) – If True, adds Gaussian noise with learnable magnitude to the convolution output.

  • hidden_channels_equal_out_channels (bool, optional, default=False) – If True, set the hidden channel number to be equal to the output channel number. If False, the hidden channel number equals to the smaller of the input channel number and the output channel number.

  • order (str, optional, default='CNACNA') – Order of operations in the residual link. 'C': convolution, 'N': normalization, 'A': nonlinear activation.

  • learn_shortcut (bool, optional, default=False) – If True, always use a convolutional shortcut instead of an identity one, otherwise only use a convolutional one if input and output have different number of channels.

training = None
class imaginaire.layers.residual.Res1dBlock(in_channels, out_channels, kernel_size=3, stride=1, padding=1, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, skip_activation_norm=True, skip_nonlinearity=False, nonlinearity='leakyrelu', inplace_nonlinearity=False, apply_noise=False, hidden_channels_equal_out_channels=False, order='CNACNA', learn_shortcut=None, clamp=None, output_scale=1)[source]

Bases: imaginaire.layers.residual._BaseResBlock

Residual block for 1D input.

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • out_channels (int) – Number of channels in the output tensor.

  • kernel_size (int, optional, default=3) – Kernel size for the convolutional filters in the residual link.

  • padding (int, optional, default=1) – Padding size.

  • dilation (int, optional, default=1) – Dilation factor.

  • groups (int, optional, default=1) – Number of convolutional/linear groups.

  • padding_mode (string, optional, default='zeros') – Type of padding: 'zeros', 'reflect', 'replicate' or 'circular'.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • skip_activation_norm (bool, optional, default=True) – If True and learn_shortcut is also True, applies activation norm to the learned shortcut connection.

  • skip_nonlinearity (bool, optional, default=True) – If True and learn_shortcut is also True, applies nonlinearity to the learned shortcut connection.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function in the residual link. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layers.

  • apply_noise (bool, optional, default=False) – If True, adds Gaussian noise with learnable magnitude to the convolution output.

  • hidden_channels_equal_out_channels (bool, optional, default=False) – If True, set the hidden channel number to be equal to the output channel number. If False, the hidden channel number equals to the smaller of the input channel number and the output channel number.

  • order (str, optional, default='CNACNA') – Order of operations in the residual link. 'C': convolution, 'N': normalization, 'A': nonlinear activation.

  • learn_shortcut (bool, optional, default=False) – If True, always use a convolutional shortcut instead of an identity one, otherwise only use a convolutional one if input and output have different number of channels.

training = None
class imaginaire.layers.residual.Res2dBlock(in_channels, out_channels, kernel_size=3, stride=1, padding=1, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, skip_activation_norm=True, skip_nonlinearity=False, skip_weight_norm=True, nonlinearity='leakyrelu', inplace_nonlinearity=False, apply_noise=False, hidden_channels_equal_out_channels=False, order='CNACNA', learn_shortcut=None, clamp=None, output_scale=1, blur=False, upsample_first=True)[source]

Bases: imaginaire.layers.residual._BaseResBlock

Residual block for 2D input.

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • out_channels (int) – Number of channels in the output tensor.

  • kernel_size (int, optional, default=3) – Kernel size for the convolutional filters in the residual link.

  • padding (int, optional, default=1) – Padding size.

  • dilation (int, optional, default=1) – Dilation factor.

  • groups (int, optional, default=1) – Number of convolutional/linear groups.

  • padding_mode (string, optional, default='zeros') – Type of padding: 'zeros', 'reflect', 'replicate' or 'circular'.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • skip_activation_norm (bool, optional, default=True) – If True and learn_shortcut is also True, applies activation norm to the learned shortcut connection.

  • skip_nonlinearity (bool, optional, default=True) – If True and learn_shortcut is also True, applies nonlinearity to the learned shortcut connection.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function in the residual link. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layers.

  • apply_noise (bool, optional, default=False) – If True, adds Gaussian noise with learnable magnitude to the convolution output.

  • hidden_channels_equal_out_channels (bool, optional, default=False) – If True, set the hidden channel number to be equal to the output channel number. If False, the hidden channel number equals to the smaller of the input channel number and the output channel number.

  • order (str, optional, default='CNACNA') – Order of operations in the residual link. 'C': convolution, 'N': normalization, 'A': nonlinear activation.

  • learn_shortcut (bool, optional, default=False) – If True, always use a convolutional shortcut instead of an identity one, otherwise only use a convolutional one if input and output have different number of channels.

training = None
class imaginaire.layers.residual.Res3dBlock(in_channels, out_channels, kernel_size=3, stride=1, padding=1, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, skip_activation_norm=True, skip_nonlinearity=False, nonlinearity='leakyrelu', inplace_nonlinearity=False, apply_noise=False, hidden_channels_equal_out_channels=False, order='CNACNA', learn_shortcut=None, clamp=None, output_scale=1)[source]

Bases: imaginaire.layers.residual._BaseResBlock

Residual block for 3D input.

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • out_channels (int) – Number of channels in the output tensor.

  • kernel_size (int, optional, default=3) – Kernel size for the convolutional filters in the residual link.

  • padding (int, optional, default=1) – Padding size.

  • dilation (int, optional, default=1) – Dilation factor.

  • groups (int, optional, default=1) – Number of convolutional/linear groups.

  • padding_mode (string, optional, default='zeros') – Type of padding: 'zeros', 'reflect', 'replicate' or 'circular'.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • skip_activation_norm (bool, optional, default=True) – If True and learn_shortcut is also True, applies activation norm to the learned shortcut connection.

  • skip_nonlinearity (bool, optional, default=True) – If True and learn_shortcut is also True, applies nonlinearity to the learned shortcut connection.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function in the residual link. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layers.

  • apply_noise (bool, optional, default=False) – If True, adds Gaussian noise with learnable magnitude to the convolution output.

  • hidden_channels_equal_out_channels (bool, optional, default=False) – If True, set the hidden channel number to be equal to the output channel number. If False, the hidden channel number equals to the smaller of the input channel number and the output channel number.

  • order (str, optional, default='CNACNA') – Order of operations in the residual link. 'C': convolution, 'N': normalization, 'A': nonlinear activation.

  • learn_shortcut (bool, optional, default=False) – If True, always use a convolutional shortcut instead of an identity one, otherwise only use a convolutional one if input and output have different number of channels.

training = None
class imaginaire.layers.residual.ResLinearBlock(in_channels, out_channels, bias=True, weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, skip_activation_norm=True, skip_nonlinearity=False, nonlinearity='leakyrelu', inplace_nonlinearity=False, apply_noise=False, hidden_channels_equal_out_channels=False, order='CNACNA', learn_shortcut=None, clamp=None, output_scale=1)[source]

Bases: imaginaire.layers.residual._BaseResBlock

Residual block with full-connected layers.

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • out_channels (int) – Number of channels in the output tensor.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • skip_activation_norm (bool, optional, default=True) – If True and learn_shortcut is also True, applies activation norm to the learned shortcut connection.

  • skip_nonlinearity (bool, optional, default=True) – If True and learn_shortcut is also True, applies nonlinearity to the learned shortcut connection.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function in the residual link. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layers.

  • apply_noise (bool, optional, default=False) – If True, add Gaussian noise with learnable magnitude after the fully-connected layer.

  • hidden_channels_equal_out_channels (bool, optional, default=False) – If True, set the hidden channel number to be equal to the output channel number. If False, the hidden channel number equals to the smaller of the input channel number and the output channel number.

  • order (str, optional, default='CNACNA') – Order of operations in the residual link. 'C': fully-connected, 'N': normalization, 'A': nonlinear activation.

  • learn_shortcut (bool, optional, default=False) – If True, always use a convolutional shortcut instead of an identity one, otherwise only use a convolutional one if input and output have different number of channels.

training = None
class imaginaire.layers.residual.UpRes2dBlock(in_channels, out_channels, kernel_size=3, stride=1, padding=1, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, skip_activation_norm=True, skip_nonlinearity=False, nonlinearity='leakyrelu', inplace_nonlinearity=False, apply_noise=False, hidden_channels_equal_out_channels=False, order='CNACNA', upsample=<class 'torch.nn.modules.upsampling.Upsample'>, up_factor=2, learn_shortcut=None, clamp=None, output_scale=1)[source]

Bases: imaginaire.layers.residual._BaseUpResBlock

Residual block for 2D input with downsampling.

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • out_channels (int) – Number of channels in the output tensor.

  • kernel_size (int, optional, default=3) – Kernel size for the convolutional filters in the residual link.

  • padding (int, optional, default=1) – Padding size.

  • dilation (int, optional, default=1) – Dilation factor.

  • groups (int, optional, default=1) – Number of convolutional/linear groups.

  • padding_mode (string, optional, default='zeros') – Type of padding: 'zeros', 'reflect', 'replicate' or 'circular'.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • skip_activation_norm (bool, optional, default=True) – If True and learn_shortcut is also True, applies activation norm to the learned shortcut connection.

  • skip_nonlinearity (bool, optional, default=True) – If True and learn_shortcut is also True, applies nonlinearity to the learned shortcut connection.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function in the residual link. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layers.

  • apply_noise (bool, optional, default=False) – If True, adds Gaussian noise with learnable magnitude to the convolution output.

  • hidden_channels_equal_out_channels (bool, optional, default=False) – If True, set the hidden channel number to be equal to the output channel number. If False, the hidden channel number equals to the smaller of the input channel number and the output channel number.

  • order (str, optional, default='CNACNA') – Order of operations in the residual link. 'C': convolution, 'N': normalization, 'A': nonlinear activation.

  • upsample (class, optional, default=NearestUpsample) – PPytorch upsampling layer to be used.

  • up_factor (int, optional, default=2) – Upsampling factor.

  • learn_shortcut (bool, optional, default=False) – If True, always use a convolutional shortcut instead of an identity one, otherwise only use a convolutional one if input and output have different number of channels.

training = None

imaginaire.layers.residual_deep module

class imaginaire.layers.residual_deep.DeepRes2dBlock(in_channels, out_channels, kernel_size=3, stride=1, padding=1, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, skip_activation_norm=True, skip_nonlinearity=False, skip_weight_norm=True, nonlinearity='leakyrelu', inplace_nonlinearity=False, apply_noise=False, hidden_channels_equal_out_channels=False, order='CNACNA', learn_shortcut=False, output_scale=1, blur=True, resample_first=True, border_free=False)[source]

Bases: imaginaire.layers.residual_deep._BaseDeepResBlock

Residual block for 2D input.

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • out_channels (int) – Number of channels in the output tensor.

  • kernel_size (int, optional, default=3) – Kernel size for the convolutional filters in the residual link.

  • padding (int, optional, default=1) – Padding size.

  • dilation (int, optional, default=1) – Dilation factor.

  • groups (int, optional, default=1) – Number of convolutional/linear groups.

  • padding_mode (string, optional, default='zeros') – Type of padding: 'zeros', 'reflect', 'replicate' or 'circular'.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • skip_activation_norm (bool, optional, default=True) – If True and learn_shortcut is also True, applies activation norm to the learned shortcut connection.

  • skip_nonlinearity (bool, optional, default=True) – If True and learn_shortcut is also True, applies nonlinearity to the learned shortcut connection.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function in the residual link. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layers.

  • apply_noise (bool, optional, default=False) – If True, adds Gaussian noise with learnable magnitude to the convolution output.

  • hidden_channels_equal_out_channels (bool, optional, default=False) – If True, set the hidden channel number to be equal to the output channel number. If False, the hidden channel number equals to the smaller of the input channel number and the output channel number.

  • order (str, optional, default='CNACNA') – Order of operations in the residual link. 'C': convolution, 'N': normalization, 'A': nonlinear activation.

  • learn_shortcut (bool, optional, default=False) – If True, always use a convolutional shortcut instead of an identity one, otherwise only use a convolutional one if input and output have different number of channels.

training = None

imaginaire.layers.vit module

class imaginaire.layers.vit.ViT2dBlock(in_channels, out_channels, kernel_size, stride, padding, dilation, groups, bias, padding_mode, weight_norm_type, weight_norm_params, activation_norm_type, activation_norm_params, nonlinearity, inplace_nonlinearity, apply_noise, blur, order, input_dim, clamp, blur_kernel=(1, 3, 3, 1), output_scale=None, init_gain=1.0)[source]

Bases: torch.nn.modules.module.Module

An abstract wrapper class that wraps a torch convolution or linear layer with normalization and nonlinearity.

forward(x, *cond_inputs, **kw_cond_inputs)[source]
Parameters
  • x (tensor) – Input tensor.

  • cond_inputs (list of tensors) – Conditional input tensors.

  • kw_cond_inputs (dict) – Keyword conditional inputs.

training = None

imaginaire.layers.weight_norm module

class imaginaire.layers.weight_norm.ScaledLR(weight_name, bias_name)[source]

Bases: object

static apply(module, weight_name, bias_name, lr_mul, equalized)[source]
compute_bias(module)[source]
compute_weight(module)[source]
remove(module)[source]
class imaginaire.layers.weight_norm.WeightDemodulation(conv, cond_dims, eps=1e-08, adaptive_bias=False, demod=True)[source]

Bases: torch.nn.modules.module.Module

Weight demodulation in “Analyzing and Improving the Image Quality of StyleGAN”, Karras et al.

Parameters
  • conv (torch.nn.Modules) – Convolutional layer.

  • cond_dims (int) – The number of channels in the conditional input.

  • eps (float, optional, default=1e-8) – a value added to the denominator for numerical stability.

  • adaptive_bias (bool, optional, default=False) – If True, adaptively predicts bias from the conditional input.

  • demod (bool, optional, default=False) – If True, performs weight demodulation.

forward(x, y, **_kwargs)[source]

Weight demodulation forward

training = None
imaginaire.layers.weight_norm.get_weight_norm_layer(norm_type, **norm_params)[source]

Return weight normalization.

Parameters
  • norm_type (str) – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • norm_params – Arbitrary keyword arguments that will be used to initialize the weight normalization.

imaginaire.layers.weight_norm.remove_equalized_lr(module, weight_name='weight', bias_name='bias')[source]
imaginaire.layers.weight_norm.remove_weight_norms(module, weight_name='weight', bias_name='bias')[source]
imaginaire.layers.weight_norm.scaled_lr(module, weight_name='weight', bias_name='bias', lr_mul=1.0, equalized=False)[source]
imaginaire.layers.weight_norm.weight_demod(conv, cond_dims=256, eps=1e-08, adaptive_bias=False, demod=True)[source]

Weight demodulation.

Module contents

class imaginaire.layers.Conv1dBlock(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, nonlinearity='none', inplace_nonlinearity=False, apply_noise=False, blur=False, order='CNA', clamp=None, output_scale=None, init_gain=1.0, **_kwargs)[source]

Bases: imaginaire.layers.conv._BaseConvBlock

A Wrapper class that wraps torch.nn.Conv1d with normalization and nonlinearity.

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • out_channels (int) – Number of channels in the output tensor.

  • kernel_size (int or tuple) – Size of the convolving kernel.

  • stride (int or float or tuple, optional, default=1) – Stride of the convolution.

  • padding (int or tuple, optional, default=0) – Zero-padding added to both sides of the input.

  • dilation (int or tuple, optional, default=1) – Spacing between kernel elements.

  • groups (int, optional, default=1) – Number of blocked connections from input channels to output channels.

  • bias (bool, optional, default=True) – If True, adds a learnable bias to the output.

  • padding_mode (string, optional, default='zeros') – Type of padding: 'zeros', 'reflect', 'replicate' or 'circular'.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layer.

  • apply_noise (bool, optional, default=False) – If True, adds Gaussian noise with learnable magnitude to the convolution output.

  • order (str, optional, default='CNA') – Order of operations. 'C': convolution, 'N': normalization, 'A': nonlinear activation. For example, a block initialized with order='CNA' will do convolution first, then normalization, then nonlinearity.

training = None
class imaginaire.layers.Conv2dBlock(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, nonlinearity='none', inplace_nonlinearity=False, apply_noise=False, blur=False, order='CNA', clamp=None, blur_kernel=(1, 3, 3, 1), output_scale=None, init_gain=1.0)[source]

Bases: imaginaire.layers.conv._BaseConvBlock

A Wrapper class that wraps torch.nn.Conv2d with normalization and nonlinearity.

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • out_channels (int) – Number of channels in the output tensor.

  • kernel_size (int or tuple) – Size of the convolving kernel.

  • stride (int or float or tuple, optional, default=1) – Stride of the convolution.

  • padding (int or tuple, optional, default=0) – Zero-padding added to both sides of the input.

  • dilation (int or tuple, optional, default=1) – Spacing between kernel elements.

  • groups (int, optional, default=1) – Number of blocked connections from input channels to output channels.

  • bias (bool, optional, default=True) – If True, adds a learnable bias to the output.

  • padding_mode (string, optional, default='zeros') – Type of padding: 'zeros', 'reflect', 'replicate' or 'circular'.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layer.

  • apply_noise (bool, optional, default=False) – If True, adds Gaussian noise with learnable magnitude to the convolution output.

  • order (str, optional, default='CNA') – Order of operations. 'C': convolution, 'N': normalization, 'A': nonlinear activation. For example, a block initialized with order='CNA' will do convolution first, then normalization, then nonlinearity.

training = None
class imaginaire.layers.Conv3dBlock(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, nonlinearity='none', inplace_nonlinearity=False, apply_noise=False, blur=False, order='CNA', clamp=None, blur_kernel=(1, 3, 3, 1), output_scale=None, init_gain=1.0)[source]

Bases: imaginaire.layers.conv._BaseConvBlock

A Wrapper class that wraps torch.nn.Conv3d with normalization and nonlinearity.

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • out_channels (int) – Number of channels in the output tensor.

  • kernel_size (int or tuple) – Size of the convolving kernel.

  • stride (int or float or tuple, optional, default=1) – Stride of the convolution.

  • padding (int or tuple, optional, default=0) – Zero-padding added to both sides of the input.

  • dilation (int or tuple, optional, default=1) – Spacing between kernel elements.

  • groups (int, optional, default=1) – Number of blocked connections from input channels to output channels.

  • bias (bool, optional, default=True) – If True, adds a learnable bias to the output.

  • padding_mode (string, optional, default='zeros') – Type of padding: 'zeros', 'reflect', 'replicate' or 'circular'.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layer.

  • apply_noise (bool, optional, default=False) – If True, adds Gaussian noise with learnable magnitude to the convolution output.

  • order (str, optional, default='CNA') – Order of operations. 'C': convolution, 'N': normalization, 'A': nonlinear activation. For example, a block initialized with order='CNA' will do convolution first, then normalization, then nonlinearity.

training = None
class imaginaire.layers.LinearBlock(in_features, out_features, bias=True, weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, nonlinearity='none', inplace_nonlinearity=False, apply_noise=False, order='CNA', clamp=None, blur_kernel=(1, 3, 3, 1), output_scale=None, init_gain=1.0, **_kwargs)[source]

Bases: imaginaire.layers.conv._BaseConvBlock

A Wrapper class that wraps torch.nn.Linear with normalization and nonlinearity.

Parameters
  • in_features (int) – Number of channels in the input tensor.

  • out_features (int) – Number of channels in the output tensor.

  • bias (bool, optional, default=True) – If True, adds a learnable bias to the output.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layer.

  • apply_noise (bool, optional, default=False) – If True, add Gaussian noise with learnable magnitude after the fully-connected layer.

  • order (str, optional, default='CNA') – Order of operations. 'C': fully-connected, 'N': normalization, 'A': nonlinear activation. For example, a block initialized with order='CNA' will do convolution first, then normalization, then nonlinearity.

training = None
class imaginaire.layers.HyperConv2dBlock(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, is_hyper_conv=False, is_hyper_norm=False, nonlinearity='none', inplace_nonlinearity=False, apply_noise=False, blur=False, order='CNA', clamp=None)[source]

Bases: imaginaire.layers.conv._BaseHyperConvBlock

A Wrapper class that wraps HyperConv2d with normalization and nonlinearity.

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • out_channels (int) – Number of channels in the output tensor.

  • kernel_size (int or tuple) – Size of the convolving kernel.

  • stride (int or float or tuple, optional, default=1) – Stride of the convolution.

  • padding (int or tuple, optional, default=0) – Zero-padding added to both sides of the input.

  • dilation (int or tuple, optional, default=1) – Spacing between kernel elements.

  • groups (int, optional, default=1) – Number of blocked connections from input channels to output channels.

  • bias (bool, optional, default=True) – If True, adds a learnable bias to the output.

  • padding_mode (string, optional, default='zeros') – Type of padding: 'zeros', 'reflect', 'replicate' or 'circular'.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • is_hyper_conv (bool, optional, default=False) – If True, use HyperConv2d, otherwise use torch.nn.Conv2d.

  • is_hyper_norm (bool, optional, default=False) – If True, use hyper normalizations.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layer.

  • apply_noise (bool, optional, default=False) – If True, adds Gaussian noise with learnable magnitude to the convolution output.

  • order (str, optional, default='CNA') – Order of operations. 'C': convolution, 'N': normalization, 'A': nonlinear activation. For example, a block initialized with order='CNA' will do convolution first, then normalization, then nonlinearity.

training = None
class imaginaire.layers.MultiOutConv2dBlock(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, nonlinearity='none', inplace_nonlinearity=False, apply_noise=False, blur=False, order='CNA', clamp=None)[source]

Bases: imaginaire.layers.conv._MultiOutBaseConvBlock

A Wrapper class that wraps torch.nn.Conv2d with normalization and nonlinearity. It can return multiple outputs, if some layers in the block return more than one output.

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • out_channels (int) – Number of channels in the output tensor.

  • kernel_size (int or tuple) – Size of the convolving kernel.

  • stride (int or float or tuple, optional, default=1) – Stride of the convolution.

  • padding (int or tuple, optional, default=0) – Zero-padding added to both sides of the input.

  • dilation (int or tuple, optional, default=1) – Spacing between kernel elements.

  • groups (int, optional, default=1) – Number of blocked connections from input channels to output channels.

  • bias (bool, optional, default=True) – If True, adds a learnable bias to the output.

  • padding_mode (string, optional, default='zeros') – Type of padding: 'zeros', 'reflect', 'replicate' or 'circular'.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layer.

  • apply_noise (bool, optional, default=False) – If True, adds Gaussian noise with learnable magnitude to the convolution output.

  • order (str, optional, default='CNA') – Order of operations. 'C': convolution, 'N': normalization, 'A': nonlinear activation. For example, a block initialized with order='CNA' will do convolution first, then normalization, then nonlinearity.

training = None
class imaginaire.layers.PartialConv2dBlock(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, nonlinearity='none', inplace_nonlinearity=False, multi_channel=False, return_mask=True, apply_noise=False, order='CNA', clamp=None)[source]

Bases: imaginaire.layers.conv._BasePartialConvBlock

A Wrapper class that wraps PartialConv2d with normalization and nonlinearity.

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • out_channels (int) – Number of channels in the output tensor.

  • kernel_size (int or tuple) – Size of the convolving kernel.

  • stride (int or float or tuple, optional, default=1) – Stride of the convolution.

  • padding (int or tuple, optional, default=0) – Zero-padding added to both sides of the input.

  • dilation (int or tuple, optional, default=1) – Spacing between kernel elements.

  • groups (int, optional, default=1) – Number of blocked connections from input channels to output channels.

  • bias (bool, optional, default=True) – If True, adds a learnable bias to the output.

  • padding_mode (string, optional, default='zeros') – Type of padding: 'zeros', 'reflect', 'replicate' or 'circular'.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layer.

  • apply_noise (bool, optional, default=False) – If True, adds Gaussian noise with learnable magnitude to the convolution output.

  • order (str, optional, default='CNA') – Order of operations. 'C': convolution, 'N': normalization, 'A': nonlinear activation. For example, a block initialized with order='CNA' will do convolution first, then normalization, then nonlinearity.

  • multi_channel (bool, optional, default=False) – If True, use different masks for different channels.

  • return_mask (bool, optional, default=True) – If True, the forward call also returns a new mask.

training = None
class imaginaire.layers.PartialConv3dBlock(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, nonlinearity='none', inplace_nonlinearity=False, multi_channel=False, return_mask=True, apply_noise=False, order='CNA', clamp=None)[source]

Bases: imaginaire.layers.conv._BasePartialConvBlock

A Wrapper class that wraps PartialConv3d with normalization and nonlinearity.

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • out_channels (int) – Number of channels in the output tensor.

  • kernel_size (int or tuple) – Size of the convolving kernel.

  • stride (int or float or tuple, optional, default=1) – Stride of the convolution.

  • padding (int or tuple, optional, default=0) – Zero-padding added to both sides of the input.

  • dilation (int or tuple, optional, default=1) – Spacing between kernel elements.

  • groups (int, optional, default=1) – Number of blocked connections from input channels to output channels.

  • bias (bool, optional, default=True) – If True, adds a learnable bias to the output.

  • padding_mode (string, optional, default='zeros') – Type of padding: 'zeros', 'reflect', 'replicate' or 'circular'.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layer.

  • apply_noise (bool, optional, default=False) – If True, adds Gaussian noise with learnable magnitude to the convolution output.

  • order (str, optional, default='CNA') – Order of operations. 'C': convolution, 'N': normalization, 'A': nonlinear activation. For example, a block initialized with order='CNA' will do convolution first, then normalization, then nonlinearity.

  • multi_channel (bool, optional, default=False) – If True, use different masks for different channels.

  • return_mask (bool, optional, default=True) – If True, the forward call also returns a new mask.

training = None
class imaginaire.layers.Res1dBlock(in_channels, out_channels, kernel_size=3, stride=1, padding=1, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, skip_activation_norm=True, skip_nonlinearity=False, nonlinearity='leakyrelu', inplace_nonlinearity=False, apply_noise=False, hidden_channels_equal_out_channels=False, order='CNACNA', learn_shortcut=None, clamp=None, output_scale=1)[source]

Bases: imaginaire.layers.residual._BaseResBlock

Residual block for 1D input.

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • out_channels (int) – Number of channels in the output tensor.

  • kernel_size (int, optional, default=3) – Kernel size for the convolutional filters in the residual link.

  • padding (int, optional, default=1) – Padding size.

  • dilation (int, optional, default=1) – Dilation factor.

  • groups (int, optional, default=1) – Number of convolutional/linear groups.

  • padding_mode (string, optional, default='zeros') – Type of padding: 'zeros', 'reflect', 'replicate' or 'circular'.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • skip_activation_norm (bool, optional, default=True) – If True and learn_shortcut is also True, applies activation norm to the learned shortcut connection.

  • skip_nonlinearity (bool, optional, default=True) – If True and learn_shortcut is also True, applies nonlinearity to the learned shortcut connection.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function in the residual link. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layers.

  • apply_noise (bool, optional, default=False) – If True, adds Gaussian noise with learnable magnitude to the convolution output.

  • hidden_channels_equal_out_channels (bool, optional, default=False) – If True, set the hidden channel number to be equal to the output channel number. If False, the hidden channel number equals to the smaller of the input channel number and the output channel number.

  • order (str, optional, default='CNACNA') – Order of operations in the residual link. 'C': convolution, 'N': normalization, 'A': nonlinear activation.

  • learn_shortcut (bool, optional, default=False) – If True, always use a convolutional shortcut instead of an identity one, otherwise only use a convolutional one if input and output have different number of channels.

training = None
class imaginaire.layers.Res2dBlock(in_channels, out_channels, kernel_size=3, stride=1, padding=1, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, skip_activation_norm=True, skip_nonlinearity=False, skip_weight_norm=True, nonlinearity='leakyrelu', inplace_nonlinearity=False, apply_noise=False, hidden_channels_equal_out_channels=False, order='CNACNA', learn_shortcut=None, clamp=None, output_scale=1, blur=False, upsample_first=True)[source]

Bases: imaginaire.layers.residual._BaseResBlock

Residual block for 2D input.

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • out_channels (int) – Number of channels in the output tensor.

  • kernel_size (int, optional, default=3) – Kernel size for the convolutional filters in the residual link.

  • padding (int, optional, default=1) – Padding size.

  • dilation (int, optional, default=1) – Dilation factor.

  • groups (int, optional, default=1) – Number of convolutional/linear groups.

  • padding_mode (string, optional, default='zeros') – Type of padding: 'zeros', 'reflect', 'replicate' or 'circular'.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • skip_activation_norm (bool, optional, default=True) – If True and learn_shortcut is also True, applies activation norm to the learned shortcut connection.

  • skip_nonlinearity (bool, optional, default=True) – If True and learn_shortcut is also True, applies nonlinearity to the learned shortcut connection.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function in the residual link. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layers.

  • apply_noise (bool, optional, default=False) – If True, adds Gaussian noise with learnable magnitude to the convolution output.

  • hidden_channels_equal_out_channels (bool, optional, default=False) – If True, set the hidden channel number to be equal to the output channel number. If False, the hidden channel number equals to the smaller of the input channel number and the output channel number.

  • order (str, optional, default='CNACNA') – Order of operations in the residual link. 'C': convolution, 'N': normalization, 'A': nonlinear activation.

  • learn_shortcut (bool, optional, default=False) – If True, always use a convolutional shortcut instead of an identity one, otherwise only use a convolutional one if input and output have different number of channels.

training = None
class imaginaire.layers.Res3dBlock(in_channels, out_channels, kernel_size=3, stride=1, padding=1, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, skip_activation_norm=True, skip_nonlinearity=False, nonlinearity='leakyrelu', inplace_nonlinearity=False, apply_noise=False, hidden_channels_equal_out_channels=False, order='CNACNA', learn_shortcut=None, clamp=None, output_scale=1)[source]

Bases: imaginaire.layers.residual._BaseResBlock

Residual block for 3D input.

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • out_channels (int) – Number of channels in the output tensor.

  • kernel_size (int, optional, default=3) – Kernel size for the convolutional filters in the residual link.

  • padding (int, optional, default=1) – Padding size.

  • dilation (int, optional, default=1) – Dilation factor.

  • groups (int, optional, default=1) – Number of convolutional/linear groups.

  • padding_mode (string, optional, default='zeros') – Type of padding: 'zeros', 'reflect', 'replicate' or 'circular'.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • skip_activation_norm (bool, optional, default=True) – If True and learn_shortcut is also True, applies activation norm to the learned shortcut connection.

  • skip_nonlinearity (bool, optional, default=True) – If True and learn_shortcut is also True, applies nonlinearity to the learned shortcut connection.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function in the residual link. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layers.

  • apply_noise (bool, optional, default=False) – If True, adds Gaussian noise with learnable magnitude to the convolution output.

  • hidden_channels_equal_out_channels (bool, optional, default=False) – If True, set the hidden channel number to be equal to the output channel number. If False, the hidden channel number equals to the smaller of the input channel number and the output channel number.

  • order (str, optional, default='CNACNA') – Order of operations in the residual link. 'C': convolution, 'N': normalization, 'A': nonlinear activation.

  • learn_shortcut (bool, optional, default=False) – If True, always use a convolutional shortcut instead of an identity one, otherwise only use a convolutional one if input and output have different number of channels.

training = None
class imaginaire.layers.UpRes2dBlock(in_channels, out_channels, kernel_size=3, stride=1, padding=1, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, skip_activation_norm=True, skip_nonlinearity=False, nonlinearity='leakyrelu', inplace_nonlinearity=False, apply_noise=False, hidden_channels_equal_out_channels=False, order='CNACNA', upsample=<class 'torch.nn.modules.upsampling.Upsample'>, up_factor=2, learn_shortcut=None, clamp=None, output_scale=1)[source]

Bases: imaginaire.layers.residual._BaseUpResBlock

Residual block for 2D input with downsampling.

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • out_channels (int) – Number of channels in the output tensor.

  • kernel_size (int, optional, default=3) – Kernel size for the convolutional filters in the residual link.

  • padding (int, optional, default=1) – Padding size.

  • dilation (int, optional, default=1) – Dilation factor.

  • groups (int, optional, default=1) – Number of convolutional/linear groups.

  • padding_mode (string, optional, default='zeros') – Type of padding: 'zeros', 'reflect', 'replicate' or 'circular'.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • skip_activation_norm (bool, optional, default=True) – If True and learn_shortcut is also True, applies activation norm to the learned shortcut connection.

  • skip_nonlinearity (bool, optional, default=True) – If True and learn_shortcut is also True, applies nonlinearity to the learned shortcut connection.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function in the residual link. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layers.

  • apply_noise (bool, optional, default=False) – If True, adds Gaussian noise with learnable magnitude to the convolution output.

  • hidden_channels_equal_out_channels (bool, optional, default=False) – If True, set the hidden channel number to be equal to the output channel number. If False, the hidden channel number equals to the smaller of the input channel number and the output channel number.

  • order (str, optional, default='CNACNA') – Order of operations in the residual link. 'C': convolution, 'N': normalization, 'A': nonlinear activation.

  • upsample (class, optional, default=NearestUpsample) – PPytorch upsampling layer to be used.

  • up_factor (int, optional, default=2) – Upsampling factor.

  • learn_shortcut (bool, optional, default=False) – If True, always use a convolutional shortcut instead of an identity one, otherwise only use a convolutional one if input and output have different number of channels.

training = None
class imaginaire.layers.DownRes2dBlock(in_channels, out_channels, kernel_size=3, stride=1, padding=1, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, skip_activation_norm=True, skip_nonlinearity=False, nonlinearity='leakyrelu', inplace_nonlinearity=False, apply_noise=False, hidden_channels_equal_out_channels=False, order='CNACNA', pooling=<class 'torch.nn.modules.pooling.AvgPool2d'>, down_factor=2, learn_shortcut=None, clamp=None, output_scale=1)[source]

Bases: imaginaire.layers.residual._BaseDownResBlock

Residual block for 2D input with downsampling.

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • out_channels (int) – Number of channels in the output tensor.

  • kernel_size (int, optional, default=3) – Kernel size for the convolutional filters in the residual link.

  • padding (int, optional, default=1) – Padding size.

  • dilation (int, optional, default=1) – Dilation factor.

  • groups (int, optional, default=1) – Number of convolutional/linear groups.

  • padding_mode (string, optional, default='zeros') – Type of padding: 'zeros', 'reflect', 'replicate' or 'circular'.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • skip_activation_norm (bool, optional, default=True) – If True and learn_shortcut is also True, applies activation norm to the learned shortcut connection.

  • skip_nonlinearity (bool, optional, default=True) – If True and learn_shortcut is also True, applies nonlinearity to the learned shortcut connection.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function in the residual link. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layers.

  • apply_noise (bool, optional, default=False) – If True, adds Gaussian noise with learnable magnitude to the convolution output.

  • hidden_channels_equal_out_channels (bool, optional, default=False) – If True, set the hidden channel number to be equal to the output channel number. If False, the hidden channel number equals to the smaller of the input channel number and the output channel number.

  • order (str, optional, default='CNACNA') – Order of operations in the residual link. 'C': convolution, 'N': normalization, 'A': nonlinear activation.

  • pooling (class, optional, default=nn.AvgPool2d) – Pytorch pooling layer to be used.

  • down_factor (int, optional, default=2) – Downsampling factor.

  • learn_shortcut (bool, optional, default=False) – If True, always use a convolutional shortcut instead of an identity one, otherwise only use a convolutional one if input and output have different number of channels.

training = None
class imaginaire.layers.ResLinearBlock(in_channels, out_channels, bias=True, weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, skip_activation_norm=True, skip_nonlinearity=False, nonlinearity='leakyrelu', inplace_nonlinearity=False, apply_noise=False, hidden_channels_equal_out_channels=False, order='CNACNA', learn_shortcut=None, clamp=None, output_scale=1)[source]

Bases: imaginaire.layers.residual._BaseResBlock

Residual block with full-connected layers.

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • out_channels (int) – Number of channels in the output tensor.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • skip_activation_norm (bool, optional, default=True) – If True and learn_shortcut is also True, applies activation norm to the learned shortcut connection.

  • skip_nonlinearity (bool, optional, default=True) – If True and learn_shortcut is also True, applies nonlinearity to the learned shortcut connection.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function in the residual link. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layers.

  • apply_noise (bool, optional, default=False) – If True, add Gaussian noise with learnable magnitude after the fully-connected layer.

  • hidden_channels_equal_out_channels (bool, optional, default=False) – If True, set the hidden channel number to be equal to the output channel number. If False, the hidden channel number equals to the smaller of the input channel number and the output channel number.

  • order (str, optional, default='CNACNA') – Order of operations in the residual link. 'C': fully-connected, 'N': normalization, 'A': nonlinear activation.

  • learn_shortcut (bool, optional, default=False) – If True, always use a convolutional shortcut instead of an identity one, otherwise only use a convolutional one if input and output have different number of channels.

training = None
class imaginaire.layers.HyperRes2dBlock(in_channels, out_channels, kernel_size=3, stride=1, padding=1, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_norm_type='', weight_norm_params=None, activation_norm_type='', activation_norm_params=None, skip_activation_norm=True, skip_nonlinearity=False, nonlinearity='leakyrelu', inplace_nonlinearity=False, apply_noise=False, hidden_channels_equal_out_channels=False, order='CNACNA', is_hyper_conv=False, is_hyper_norm=False, learn_shortcut=None, clamp=None, output_scale=1)[source]

Bases: imaginaire.layers.residual._BaseHyperResBlock

Hyper residual block for 2D input.

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • out_channels (int) – Number of channels in the output tensor.

  • kernel_size (int, optional, default=3) – Kernel size for the convolutional filters in the residual link.

  • padding (int, optional, default=1) – Padding size.

  • dilation (int, optional, default=1) – Dilation factor.

  • groups (int, optional, default=1) – Number of convolutional/linear groups.

  • padding_mode (string, optional, default='zeros') – Type of padding: 'zeros', 'reflect', 'replicate' or 'circular'.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • skip_activation_norm (bool, optional, default=True) – If True and learn_shortcut is also True, applies activation norm to the learned shortcut connection.

  • skip_nonlinearity (bool, optional, default=True) – If True and learn_shortcut is also True, applies nonlinearity to the learned shortcut connection.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function in the residual link. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layers.

  • apply_noise (bool, optional, default=False) – If True, adds Gaussian noise with learnable magnitude to the convolution output.

  • hidden_channels_equal_out_channels (bool, optional, default=False) – If True, set the hidden channel number to be equal to the output channel number. If False, the hidden channel number equals to the smaller of the input channel number and the output channel number.

  • order (str, optional, default='CNACNA') – Order of operations in the residual link. 'C': convolution, 'N': normalization, 'A': nonlinear activation.

  • is_hyper_conv (bool, optional, default=False) – If True, use HyperConv2d, otherwise use torch.nn.Conv2d.

  • is_hyper_norm (bool, optional, default=False) – If True, use hyper normalizations.

  • learn_shortcut (bool, optional, default=False) – If True, always use a convolutional shortcut instead of an identity one, otherwise only use a convolutional one if input and output have different number of channels.

training = None
class imaginaire.layers.MultiOutRes2dBlock(in_channels, out_channels, kernel_size=3, stride=1, padding=1, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, skip_activation_norm=True, skip_nonlinearity=False, nonlinearity='leakyrelu', inplace_nonlinearity=False, apply_noise=False, hidden_channels_equal_out_channels=False, order='CNACNA', learn_shortcut=None, clamp=None, output_scale=1, blur=False, upsample_first=True)[source]

Bases: imaginaire.layers.residual._BaseMultiOutResBlock

Residual block for 2D input. It can return multiple outputs, if some layers in the block return more than one output.

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • out_channels (int) – Number of channels in the output tensor.

  • kernel_size (int, optional, default=3) – Kernel size for the convolutional filters in the residual link.

  • padding (int, optional, default=1) – Padding size.

  • dilation (int, optional, default=1) – Dilation factor.

  • groups (int, optional, default=1) – Number of convolutional/linear groups.

  • padding_mode (string, optional, default='zeros') – Type of padding: 'zeros', 'reflect', 'replicate' or 'circular'.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • skip_activation_norm (bool, optional, default=True) – If True and learn_shortcut is also True, applies activation norm to the learned shortcut connection.

  • skip_nonlinearity (bool, optional, default=True) – If True and learn_shortcut is also True, applies nonlinearity to the learned shortcut connection.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function in the residual link. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layers.

  • apply_noise (bool, optional, default=False) – If True, adds Gaussian noise with learnable magnitude to the convolution output.

  • hidden_channels_equal_out_channels (bool, optional, default=False) – If True, set the hidden channel number to be equal to the output channel number. If False, the hidden channel number equals to the smaller of the input channel number and the output channel number.

  • order (str, optional, default='CNACNA') – Order of operations in the residual link. 'C': convolution, 'N': normalization, 'A': nonlinear activation.

  • learn_shortcut (bool, optional, default=False) – If True, always use a convolutional shortcut instead of an identity one, otherwise only use a convolutional one if input and output have different number of channels.

training = None
class imaginaire.layers.PartialRes2dBlock(in_channels, out_channels, kernel_size=3, stride=1, padding=1, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, skip_activation_norm=True, skip_nonlinearity=False, nonlinearity='leakyrelu', inplace_nonlinearity=False, multi_channel=False, return_mask=True, apply_noise=False, hidden_channels_equal_out_channels=False, order='CNACNA', learn_shortcut=None, clamp=None, output_scale=1)[source]

Bases: imaginaire.layers.residual._BasePartialResBlock

Residual block for 2D input with partial convolution.

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • out_channels (int) – Number of channels in the output tensor.

  • kernel_size (int, optional, default=3) – Kernel size for the convolutional filters in the residual link.

  • padding (int, optional, default=1) – Padding size.

  • dilation (int, optional, default=1) – Dilation factor.

  • groups (int, optional, default=1) – Number of convolutional/linear groups.

  • padding_mode (string, optional, default='zeros') – Type of padding: 'zeros', 'reflect', 'replicate' or 'circular'.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • skip_activation_norm (bool, optional, default=True) – If True and learn_shortcut is also True, applies activation norm to the learned shortcut connection.

  • skip_nonlinearity (bool, optional, default=True) – If True and learn_shortcut is also True, applies nonlinearity to the learned shortcut connection.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function in the residual link. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layers.

  • apply_noise (bool, optional, default=False) – If True, adds Gaussian noise with learnable magnitude to the convolution output.

  • hidden_channels_equal_out_channels (bool, optional, default=False) – If True, set the hidden channel number to be equal to the output channel number. If False, the hidden channel number equals to the smaller of the input channel number and the output channel number.

  • order (str, optional, default='CNACNA') – Order of operations in the residual link. 'C': convolution, 'N': normalization, 'A': nonlinear activation.

  • learn_shortcut (bool, optional, default=False) – If True, always use a convolutional shortcut instead of an identity one, otherwise only use a convolutional one if input and output have different number of channels.

training = None
class imaginaire.layers.PartialRes3dBlock(in_channels, out_channels, kernel_size=3, stride=1, padding=1, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_norm_type='none', weight_norm_params=None, activation_norm_type='none', activation_norm_params=None, skip_activation_norm=True, skip_nonlinearity=False, nonlinearity='leakyrelu', inplace_nonlinearity=False, multi_channel=False, return_mask=True, apply_noise=False, hidden_channels_equal_out_channels=False, order='CNACNA', learn_shortcut=None, clamp=None, output_scale=1)[source]

Bases: imaginaire.layers.residual._BasePartialResBlock

Residual block for 3D input with partial convolution.

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • out_channels (int) – Number of channels in the output tensor.

  • kernel_size (int, optional, default=3) – Kernel size for the convolutional filters in the residual link.

  • padding (int, optional, default=1) – Padding size.

  • dilation (int, optional, default=1) – Dilation factor.

  • groups (int, optional, default=1) – Number of convolutional/linear groups.

  • padding_mode (string, optional, default='zeros') – Type of padding: 'zeros', 'reflect', 'replicate' or 'circular'.

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight' or 'weight_demod'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • activation_norm_type (str, optional, default='none') – Type of activation normalization. 'none', 'instance', 'batch', 'sync_batch', 'layer', 'layer_2d', 'group', 'adaptive', 'spatially_adaptive' or 'hyper_spatially_adaptive'.

  • activation_norm_params (obj, optional, default=None) – Parameters of activation normalization. If not None, activation_norm_params.__dict__ will be used as keyword arguments when initializing activation normalization.

  • skip_activation_norm (bool, optional, default=True) – If True and learn_shortcut is also True, applies activation norm to the learned shortcut connection.

  • skip_nonlinearity (bool, optional, default=True) – If True and learn_shortcut is also True, applies nonlinearity to the learned shortcut connection.

  • nonlinearity (str, optional, default='none') – Type of nonlinear activation function in the residual link. 'none', 'relu', 'leakyrelu', 'prelu', 'tanh' , 'sigmoid' or 'softmax'.

  • inplace_nonlinearity (bool, optional, default=False) – If True, set inplace=True when initializing the nonlinearity layers.

  • apply_noise (bool, optional, default=False) – If True, adds Gaussian noise with learnable magnitude to the convolution output.

  • hidden_channels_equal_out_channels (bool, optional, default=False) – If True, set the hidden channel number to be equal to the output channel number. If False, the hidden channel number equals to the smaller of the input channel number and the output channel number.

  • order (str, optional, default='CNACNA') – Order of operations in the residual link. 'C': convolution, 'N': normalization, 'A': nonlinear activation.

  • learn_shortcut (bool, optional, default=False) – If True, always use a convolutional shortcut instead of an identity one, otherwise only use a convolutional one if input and output have different number of channels.

training = None
class imaginaire.layers.NonLocal2dBlock(in_channels, scale=True, clamp=False, weight_norm_type='none', weight_norm_params=None, bias=True)[source]

Bases: torch.nn.modules.module.Module

Self attention Layer

Parameters
  • in_channels (int) – Number of channels in the input tensor.

  • scale (bool, optional, default=True) – If True, scale the output by a learnable parameter.

  • clamp (bool, optional, default=``False``) – If True, clamp the scaling parameter to (-1, 1).

  • weight_norm_type (str, optional, default='none') – Type of weight normalization. 'none', 'spectral', 'weight'.

  • weight_norm_params (obj, optional, default=None) – Parameters of weight normalization. If not None, weight_norm_params.__dict__ will be used as keyword arguments when initializing weight normalization.

  • bias (bool, optional, default=True) – If True, adds bias in the convolutional blocks.

forward(x)[source]
Parameters

x (tensor) – input feature maps (B X C X W X H)

Returns

  • out (tensor) : self attention value + input feature

  • attention (tensor): B x N x N (N is Width*Height)

Return type

(tuple)

training = None