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.
Subpackages¶
Submodules¶
thelper.nn.common module¶
-
class
thelper.nn.common.ConvBlock(input_size, output_size, kernel_size=4, stride=2, padding=1, bias=True, activation='relu', norm='batch', groups=1, prelu_params=1)[source]¶ Bases:
sphinx.ext.autodoc.importer._MockObject
-
class
thelper.nn.common.DeconvBlock(input_size, output_size, kernel_size=4, stride=2, padding=1, bias=True, activation='relu', norm='batch')[source]¶ Bases:
sphinx.ext.autodoc.importer._MockObject
-
class
thelper.nn.common.DenseBlock(input_size, output_size, bias=True, activation='relu', norm='batch')[source]¶ Bases:
sphinx.ext.autodoc.importer._MockObject
-
class
thelper.nn.common.PSBlock(input_size, output_size, scale_factor, kernel_size=3, stride=1, padding=1, bias=True, activation='relu', norm='batch')[source]¶ Bases:
sphinx.ext.autodoc.importer._MockObject
-
class
thelper.nn.common.ResNetBlock(num_filter, kernel_size=3, stride=1, padding=1, bias=True, activation='relu', norm='batch')[source]¶ Bases:
sphinx.ext.autodoc.importer._MockObject
-
class
thelper.nn.common.Upsample2xBlock(input_size, output_size, bias=True, upsample='deconv', activation='relu', norm='batch')[source]¶ Bases:
sphinx.ext.autodoc.importer._MockObject
thelper.nn.coordconv module¶
-
class
thelper.nn.coordconv.AddCoords(centered=True, normalized=True, noise=None, radius_channel=False, scale=None)[source]¶ Bases:
sphinx.ext.autodoc.importer._MockObjectCreates a torch-compatible layer that adds intrinsic coordinate layers to input tensors.
-
class
thelper.nn.coordconv.CoordConv2d(in_channels, *args, centered=True, normalized=True, noise=None, radius_channel=False, scale=None, **kwargs)[source]¶ Bases:
sphinx.ext.autodoc.importer._MockObjectCoordConv-equivalent of torch’s default Conv2d model layer.
See also
Liu et al., An Intriguing Failing of Convolutional Neural Networks and the CoordConv Solution` <https://arxiv.org/abs/1807.03247>`_ [arXiv], 2018.
-
class
thelper.nn.coordconv.CoordConvTranspose2d(in_channels, *args, centered=True, normalized=True, noise=None, radius_channel=False, scale=None, **kwargs)[source]¶ Bases:
sphinx.ext.autodoc.importer._MockObjectCoordConv-equivalent of torch’s default ConvTranspose2d model layer.
See also
Liu et al., An Intriguing Failing of Convolutional Neural Networks and the CoordConv Solution` <https://arxiv.org/abs/1807.03247>`_ [arXiv], 2018.
-
thelper.nn.coordconv.get_coords_map(height, width, centered=True, normalized=True, noise=None, dtype=<sphinx.ext.autodoc.importer._MockObject object>)[source]¶ Returns a HxW intrinsic coordinates map tensor (shape=2xHxW).
thelper.nn.densenet module¶
-
class
thelper.nn.densenet.DenseNet(growth_rate=32, block_config=(6, 12, 24, 16), num_init_features=64, bn_size=4, drop_rate=0, num_classes=1000)[source]¶ Bases:
sphinx.ext.autodoc.importer._MockObjectDensenet-BC model class, based on “Densely Connected Convolutional Networks”
Parameters: - growth_rate (int) – how many filters to add each layer (k in paper)
- block_config (list of 4 ints) – how many layers in each pooling block
- num_init_features (int) – the number of filters to learn in the first convolution layer
- bn_size (int) – multiplicative factor for number of bottle neck layers (i.e. bn_size * k features in the bottleneck layer)
- drop_rate (float) – dropout rate after each dense layer
- num_classes (int) – number of classification classes
-
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.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.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.efficientnet module¶
-
class
thelper.nn.efficientnet.EfficientNet(task, num, pretrained=False)[source]¶ Bases:
thelper.nn.utils.Module
-
class
thelper.nn.efficientnet.FCEfficientNet(task, ckptdata, map_location='cpu', avgpool_size=0)[source]¶
thelper.nn.fcn module¶
-
class
thelper.nn.fcn.FCN32s(task, init_vgg16=True)[source]¶ Bases:
thelper.nn.utils.Module
thelper.nn.inceptionresnetv2 module¶
-
class
thelper.nn.inceptionresnetv2.BasicConv2d(in_planes, out_planes, kernel_size, stride, padding=0)[source]¶ Bases:
sphinx.ext.autodoc.importer._MockObject
-
class
thelper.nn.inceptionresnetv2.Block17(scale=1.0)[source]¶ Bases:
sphinx.ext.autodoc.importer._MockObject
-
class
thelper.nn.inceptionresnetv2.Block35(scale=1.0)[source]¶ Bases:
sphinx.ext.autodoc.importer._MockObject
-
class
thelper.nn.inceptionresnetv2.Block8(scale=1.0, noReLU=False)[source]¶ Bases:
sphinx.ext.autodoc.importer._MockObject
-
class
thelper.nn.inceptionresnetv2.InceptionResNetV2(task, input_channels=3)[source]¶ Bases:
thelper.nn.utils.Module
thelper.nn.lenet module¶
-
class
thelper.nn.lenet.LeNet(task, input_shape=(1, 28, 28), conv1_filters=6, conv2_filters=16, hidden1_size=120, hidden2_size=84, output_size=10)[source]¶ Bases:
thelper.nn.utils.ModuleLeNet CNN implementation.
See http://yann.lecun.com/exdb/lenet/ for more information.
This is NOT a modern architecture; it is only provided here for tutorial purposes.
thelper.nn.mobilenet module¶
-
class
thelper.nn.mobilenet.InvertedResidual(inp, oup, stride, expand_ratio)[source]¶ Bases:
sphinx.ext.autodoc.importer._MockObject
-
class
thelper.nn.mobilenet.MobileNetV2(task, input_size=224, width_mult=1.0)[source]¶ Bases:
thelper.nn.utils.Module
thelper.nn.resnet module¶
-
class
thelper.nn.resnet.AutoEncoderResNet(task, output_pads=None, **kwargs)[source]¶ Bases:
thelper.nn.resnet.ResNetAutoencoder-classifier architecture based on ResNet blocks+layers configurations.
-
class
thelper.nn.resnet.AutoEncoderSkipResNet(task, output_pads=None, decoder_dropout=False, dropout_prob=0.1, **kwargs)[source]¶ Bases:
thelper.nn.resnet.ResNetAutoencoder-U-Net architecture based on ResNet blocks+layers configurations.
-
class
thelper.nn.resnet.BasicBlock(inplanes, planes, stride=1, downsample=None, coordconv=False, radius_channel=True, activation='relu')[source]¶ Bases:
thelper.nn.resnet.Module
-
class
thelper.nn.resnet.Bottleneck(inplanes, planes, stride=1, downsample=None, coordconv=False, radius_channel=True, activation='relu')[source]¶ Bases:
thelper.nn.resnet.Module-
__init__(inplanes, planes, stride=1, downsample=None, coordconv=False, radius_channel=True, activation='relu')[source]¶ Initialize self. See help(type(self)) for accurate signature.
-
expansion= 4¶
-
-
class
thelper.nn.resnet.ConvTailNet(n_inputs, num_classes)[source]¶ Bases:
sphinx.ext.autodoc.importer._MockObjectDEPRECATED. Will be removed in a future version.
-
class
thelper.nn.resnet.FCResNet(task, ckptdata, map_location='cpu', avgpool_size=0)[source]¶ Bases:
thelper.nn.resnet.ResNetFully Convolutional ResNet converter for pre-trained classification models.
-
class
thelper.nn.resnet.FakeModule(*args, **kwargs)[source]¶ Bases:
sphinx.ext.autodoc.importer._MockObject
-
class
thelper.nn.resnet.Module(inplanes, planes, stride=1, downsample=None, coordconv=False, radius_channel=True)[source]¶ Bases:
sphinx.ext.autodoc.importer._MockObject-
__init__(inplanes, planes, stride=1, downsample=None, coordconv=False, radius_channel=True)[source]¶ Initialize self. See help(type(self)) for accurate signature.
-
expansion= 1¶
-
-
class
thelper.nn.resnet.ResNet(task, block='thelper.nn.resnet.BasicBlock', layers=[3, 4, 6, 3], strides=[1, 2, 2, 2], input_channels=3, flexible_input_res=False, pool_size=7, head_type=None, coordconv=False, radius_channel=True, activation='relu', skip_max_pool=False, pretrained=False, conv1_config=[7, 2, 3])[source]¶ Bases:
thelper.nn.utils.Module-
__init__(task, block='thelper.nn.resnet.BasicBlock', layers=[3, 4, 6, 3], strides=[1, 2, 2, 2], input_channels=3, flexible_input_res=False, pool_size=7, head_type=None, coordconv=False, radius_channel=True, activation='relu', skip_max_pool=False, pretrained=False, conv1_config=[7, 2, 3])[source]¶ Receives a task object to hold internally for model specialization.
-
-
class
thelper.nn.resnet.ResNetFullyConv(task, block='thelper.nn.resnet.BasicBlock', layers=[3, 4, 6, 3], strides=[1, 2, 2, 2], input_channels=3, flexible_input_res=False, pool_size=7, coordconv=False, radius_channel=True, pretrained=False)[source]¶ Bases:
thelper.nn.resnet.ResNetDEPRECATED. Will be removed in a future version. Use the torchvision segmentation models or the ResNet above instead.
-
class
thelper.nn.resnet.SqueezeExcitationBlock(inplanes, planes, stride=1, downsample=None, reduction=16, coordconv=False, radius_channel=True, activation='relu')[source]¶ Bases:
thelper.nn.resnet.Module
thelper.nn.srm module¶
-
class
thelper.nn.srm.SRMWrapper(base_model: <sphinx.ext.autodoc.importer._MockObject object at 0x7f0c81825940>, input_channels: int = 3)[source]¶ Bases:
sphinx.ext.autodoc.importer._MockObjectWraps a base model for Steganalysis Rich Model (SRM)-based noise analysis.
thelper.nn.unet module¶
-
class
thelper.nn.unet.BasicBlock(in_channels, out_channels, coordconv=False, kernel_size=3, padding=1)[source]¶ Bases:
sphinx.ext.autodoc.importer._MockObjectDefault (double-conv) block used in U-Net layers.
-
class
thelper.nn.unet.UNet(task, in_channels=3, mid_channels=512, coordconv=False, srm=False)[source]¶ Bases:
thelper.nn.utils.ModuleU-Net implementation. Not identical to the original.
This version includes batchnorm and transposed conv2d layers for upsampling. Coordinate Convolutions (CoordConv) can also be toggled on if requested (see
thelper.nn.coordconvfor more information).
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.
-
class
thelper.nn.utils.ExternalClassifModule(model_type, task, **kwargs)[source]¶ Bases:
thelper.nn.utils.ExternalModuleExternal model interface specialization for classification tasks.
This interface will try to ‘rewire’ the last fully connected layer of the models it instantiates to match the number of classes to predict defined in the task object.
See also
-
class
thelper.nn.utils.ExternalDetectModule(model_type, task, **kwargs)[source]¶ Bases:
thelper.nn.utils.ExternalModuleExternal model interface specialization for object detection tasks.
This interface will try to ‘rewire’ the last fully connected layer of the models it instantiates to match the number of classes to predict defined in the task object.
See also
-
class
thelper.nn.utils.ExternalModule(model_type, task, **kwargs)[source]¶ Bases:
thelper.nn.utils.ModuleModel inteface used to hold a task object for an external implementation.
This interface is built on top of
torch.nn.Moduleand should remain fully compatible with it. It is automatically used when instantiating a model viathelper.nn.utils.create_model()that is not derived fromthelper.nn.utils.Module. Its only purpose is to hold the task object, and redirectthelper.nn.utils.Module.forward()to the actual model’s transformation function. It can also be specialized to automatically adapt some external models after their construction using the knowledge contained in the task object.See also
-
__init__(model_type, task, **kwargs)[source]¶ Receives a task object to hold internally for model specialization.
-
get_name()[source]¶ Returns the name of this module (by default, the fully qualified class name of the external model).
-
set_task(task)[source]¶ Stores the new task internally.
Note that since this external module handler is generic, it does not know what to do with the task, so it just assumes that the model is already set up. Specialized external module handlers will instead attempt to modify the model they wrap.
-
-
class
thelper.nn.utils.Module(task, **kwargs)[source]¶ Bases:
sphinx.ext.autodoc.importer._MockObjectModel interface used to hold a task object.
This interface is built on top of
torch.nn.Moduleand should remain fully compatible with it.All models used in the framework should derive from this interface, and therefore expect a task object as the first argument of their constructor. Their implementation may decide to ignore this task object when building their internal layers, but using it should help specialize the network by specifying e.g. the number of classes to support.
-
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 throughthelper.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
Noneif 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.Moduleandtorch.nn.Module.