thelper.nn package

Neural network and model package.

This package contains classes that define blocks and modules used in various neural network architectures. Most of these classes have been adapted from external sources; see their individual headers for more information.

Submodules

thelper.nn.common module

thelper.nn.common.shave(imgs, border_size=0)[source]
thelper.nn.common.weights_init_kaiming(m)[source]
thelper.nn.common.weights_init_xavier(m)[source]

thelper.nn.coordconv module

thelper.nn.coordconv.get_coords_map(height, width, centered=True, normalized=True, noise=None, dtype=torch.float32)[source]

Returns a HxW intrinsic coordinates map tensor (shape=2xHxW).

thelper.nn.coordconv.make_conv2d(*args, coordconv=False, centered=True, normalized=True, noise=None, radius_channel=False, scale=None, **kwargs)[source]

Creates a 2D convolution layer with optional CoordConv support.

thelper.nn.coordconv.swap_coordconv_layers(module, centered=True, normalized=True, noise=None, radius_channel=False, scale=None)[source]

Modifies the provided module by swapping Conv2d layers for CoordConv-equivalent layers.

thelper.nn.densenet module

thelper.nn.densenet.densenet121(pretrained=False, **kwargs)[source]

Densenet-121 model from “Densely Connected Convolutional Networks”

Parameters

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

thelper.nn.densenet.densenet161(pretrained=False, **kwargs)[source]

Densenet-161 model from “Densely Connected Convolutional Networks”

Parameters

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

thelper.nn.densenet.densenet169(pretrained=False, **kwargs)[source]

Densenet-169 model from “Densely Connected Convolutional Networks”

Parameters

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

thelper.nn.densenet.densenet201(pretrained=False, **kwargs)[source]

Densenet-201 model from “Densely Connected Convolutional Networks”

Parameters

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

thelper.nn.efficientnet module

thelper.nn.fcn module

thelper.nn.fcn.get_upsampling_weight(in_channels, out_channels, kernel_size)[source]

thelper.nn.inceptionresnetv2 module

thelper.nn.lenet module

thelper.nn.mobilenet module

thelper.nn.mobilenet.conv_1x1_bn(inp, oup)[source]
thelper.nn.mobilenet.conv_bn(inp, oup, stride)[source]

thelper.nn.resnet module

thelper.nn.resnet.get_activation_layer(name: AnyStr) → torch.nn.Module[source]

thelper.nn.srm module

thelper.nn.srm.setup_srm_layer(input_channels: int = 3) → torch.nn.Module[source]

Creates a SRM convolution layer for noise analysis.

thelper.nn.srm.setup_srm_weights(input_channels: int = 3) → torch.Tensor[source]

Creates the SRM kernels for noise analysis.

thelper.nn.unet module

thelper.nn.utils module

Neural network utility functions and classes.

This module contains base interfaces and utility functions used to define and instantiate neural network models.

thelper.nn.utils.create_model(config, task, save_dir=None, ckptdata=None)[source]

Instantiates a model based on a provided task object.

The configuration must be given as a dictionary object. This dictionary will be parsed for a ‘model’ field. This field is expected to be a dictionary itself. It may then specify a type to instantiate as well as the parameters to provide to that class constructor, or a path to a checkpoint from which a model should be loaded.

All models must derive from thelper.nn.utils.Module, or they must be instantiable through thelper.nn.utils.ExternalModule (or one of its specialized classes). The provided task object will be used to make sure that the model has the required input/output layers for the requested objective.

If checkpoint data is provided by the caller, the weights it contains will be loaded into the returned model.

Usage examples inside a session configuration file:

# ...
# the function will look for a 'model' field in the provided config dict
"model": {
    # the type provides the class name to instantiate an object from
    "type": "thelper.nn.mobilenet.MobileNetV2",
    # the parameters listed below are passed to the model's constructor
    "params": {
        # ...
    }
# ...
Parameters
  • config – a session dictionary that provides a ‘model’ field containing a dictionary.

  • task – a task object that will be passed to the model’s constructor in order to specialize it. Can be None if a checkpoint is provided, and if the previous task is wanted instead of a new one.

  • save_dir – if not None, a log file containing model information will be created there.

  • ckptdata – raw checkpoint data loaded via torch.load(); the model will be given its previous state.

Returns

The instantiated model, compatible with the interface of both thelper.nn.utils.Module and torch.nn.Module.

See also

thelper.nn.utils.Module
thelper.nn.utils.ExternalModule
thelper.nn.utils.get_learnable_param_count(model: torch.nn.Module) → int[source]

Returns the learnable (grad-enabled) parameter count in a module.