Menu

Helper Module for Deep Learning.

A module with common functions.

class pynet.utils.Losses[source]

Class that register all the available losses.

REGISTRY = {'BetaBLoss': <class 'pynet.losses.generative.BetaBLoss'>, 'BetaHLoss': <class 'pynet.losses.generative.BetaHLoss'>, 'BtcvaeLoss': <class 'pynet.losses.generative.BtcvaeLoss'>, 'CustomKLLoss': <class 'pynet.losses.segmentation.CustomKLLoss'>, 'FactorKLoss': <class 'pynet.losses.generative.FactorKLoss'>, 'FocalLoss': <class 'pynet.losses.segmentation.FocalLoss'>, 'GMVAELoss': <class 'pynet.losses.generative.GMVAELoss'>, 'MCVAELoss': <class 'pynet.losses.generative.MCVAELoss'>, 'MSELoss': <class 'pynet.losses.common.MSELoss'>, 'MaskLoss': <class 'pynet.losses.segmentation.MaskLoss'>, 'NCCLoss': <class 'pynet.losses.common.NCCLoss'>, 'NvNetCombinedLoss': <class 'pynet.losses.segmentation.NvNetCombinedLoss'>, 'PCCLoss': <class 'pynet.losses.common.PCCLoss'>, 'RCNetLoss': <class 'pynet.losses.common.RCNetLoss'>, 'SoftDiceLoss': <class 'pynet.losses.segmentation.SoftDiceLoss'>, 'SparseLoss': <class 'pynet.losses.generative.SparseLoss'>, 'VAEGMPLoss': <class 'pynet.losses.generative.VAEGMPLoss'>, 'VMILoss': <class 'pynet.losses.common.VMILoss'>, 'VaDELoss': <class 'pynet.losses.generative.VaDELoss'>}
class pynet.utils.Metrics[source]

Class that register all the available losses.

REGISTRY = {'accuracy': <function accuracy>, 'binary_accuracy': <pynet.metrics.BinaryClassificationMetrics object>, 'binary_false_negative': <pynet.metrics.BinaryClassificationMetrics object>, 'binary_false_positive': <pynet.metrics.BinaryClassificationMetrics object>, 'binary_precision': <pynet.metrics.BinaryClassificationMetrics object>, 'binary_recall': <pynet.metrics.BinaryClassificationMetrics object>, 'binary_true_negative': <pynet.metrics.BinaryClassificationMetrics object>, 'binary_true_positive': <pynet.metrics.BinaryClassificationMetrics object>, 'f1_score': <pynet.metrics.SKMetrics object>, 'f2_score': <pynet.metrics.SKMetrics object>, 'multiclass_dice': <function multiclass_dice>, 'pearson_correlation': <function pearson_correlation>, 'sk_accuracy': <pynet.metrics.SKMetrics object>, 'sk_average_precision_score': <pynet.metrics.SKMetrics object>, 'sk_cohen_kappa_score': <pynet.metrics.SKMetrics object>, 'sk_false_discovery_rate': <pynet.metrics.SKMetrics object>, 'sk_false_negative_rate': <pynet.metrics.SKMetrics object>, 'sk_false_positive_rate': <pynet.metrics.SKMetrics object>, 'sk_log_loss': <pynet.metrics.SKMetrics object>, 'sk_matthews_corrcoef': <pynet.metrics.SKMetrics object>, 'sk_negative_predictive_value': <pynet.metrics.SKMetrics object>, 'sk_positive_predictive_value': <pynet.metrics.SKMetrics object>, 'sk_precision_score': <pynet.metrics.SKMetrics object>, 'sk_roc_auc_score': <pynet.metrics.SKMetrics object>, 'sk_true_negative_rate': <pynet.metrics.SKMetrics object>, 'sk_true_positive_rate': <pynet.metrics.SKMetrics object>}
class pynet.utils.Networks[source]

Class that register all the available networks.

REGISTRY = {'ADDNet': <class 'pynet.models.vtnet.ADDNet'>, 'BGCodeDiscriminator': <class 'pynet.models.braingengan.BGCodeDiscriminator'>, 'BGDiscriminator': <class 'pynet.models.braingengan.BGDiscriminator'>, 'BGEncoder': <class 'pynet.models.braingengan.BGEncoder'>, 'BGGenerator': <class 'pynet.models.braingengan.BGGenerator'>, 'BrainNetCNN': <class 'pynet.models.brainnetcnn.BrainNetCNN'>, 'DeepCluster': <class 'pynet.models.deepcluster.DeepCluster'>, 'DeepLabNet': <class 'pynet.models.deeplabnet.DeepLabNet'>, 'DenseNet': <class 'pynet.models.torchvisnet.DenseNet'>, 'DenseNet121': <class 'pynet.models.torchvisnet.DenseNet121'>, 'DenseNet161': <class 'pynet.models.torchvisnet.DenseNet161'>, 'DenseNet169': <class 'pynet.models.torchvisnet.DenseNet169'>, 'DenseNet201': <class 'pynet.models.torchvisnet.DenseNet201'>, 'GMVAENet': <class 'pynet.models.vae.gmvae.GMVAENet'>, 'Inception3': <class 'pynet.models.torchvisnet.Inception3'>, 'MCVAE': <class 'pynet.models.vae.mcvae.MCVAE'>, 'NvNet': <class 'pynet.models.nvnet.NvNet'>, 'PSPNet': <class 'pynet.models.pspnet.PSPNet'>, 'RCNet': <class 'pynet.models.rcnet.RCNet'>, 'ResAENet': <class 'pynet.models.resnet.ResAENet'>, 'ResNet': <class 'pynet.models.torchvisnet.ResNet'>, 'ResNet101': <class 'pynet.models.torchvisnet.ResNet101'>, 'ResNet101Wide': <class 'pynet.models.torchvisnet.ResNet101Wide'>, 'ResNet101x32x8d': <class 'pynet.models.torchvisnet.ResNet101x32x8d'>, 'ResNet152': <class 'pynet.models.torchvisnet.ResNet152'>, 'ResNet18': <class 'pynet.models.torchvisnet.ResNet18'>, 'ResNet34': <class 'pynet.models.torchvisnet.ResNet34'>, 'ResNet50': <class 'pynet.models.torchvisnet.ResNet50'>, 'ResNet50Wide': <class 'pynet.models.torchvisnet.ResNet50Wide'>, 'ResNet50x32x4d': <class 'pynet.models.torchvisnet.ResNet50x32x4d'>, 'STAAENet': <class 'pynet.models.attention.STAAENet'>, 'SonoNet': <class 'pynet.models.sononet.SonoNet'>, 'SphericalUNet': <class 'pynet.models.spherical.unet.SphericalUNet'>, 'UNet': <class 'pynet.models.unet.UNet'>, 'VAEGMPNet': <class 'pynet.models.vae.gmvae.VAEGMPNet'>, 'VAENet': <class 'pynet.models.vae.vae.VAENet'>, 'VGG': <class 'pynet.models.torchvisnet.VGG'>, 'VGG11': <class 'pynet.models.torchvisnet.VGG11'>, 'VGG13': <class 'pynet.models.torchvisnet.VGG13'>, 'VGG16': <class 'pynet.models.torchvisnet.VGG16'>, 'VGG19': <class 'pynet.models.torchvisnet.VGG19'>, 'VTNet': <class 'pynet.models.vtnet.VTNet'>, 'VaDENet': <class 'pynet.models.vae.vade.VaDENet'>, 'VaDEPreTrainNet': <class 'pynet.models.vae.vade.VaDEPreTrainNet'>, 'VoxelMorphNet': <class 'pynet.models.voxelmorphnet.VoxelMorphNet'>}
class pynet.utils.RegisteryDecorator[source]

Class that can be used to register class in a registry.

classmethod get_registry()[source]
classmethod register(obj_or_klass, *args, **kwargs)[source]
class pynet.utils.Regularizers[source]

Class that register all the available regularizers.

REGISTRY = {'ADDNetRegularizer': <class 'pynet.models.vtnet.ADDNetRegularizer'>, 'FlowRegularizer': <class 'pynet.models.voxelmorphnet.FlowRegularizer'>, 'RCNetRegularizer': <class 'pynet.models.rcnet.RCNetRegularizer'>}
class pynet.utils.TemporaryDirectory(dir=None, prefix=None, name=None)[source]

Securely creates a temporary directory. The resulting object can be used as a context manager. When the returned object is used as a context manager, the name will be assigned to the target of the as clause in the with statement, if there is one.

__init__(dir=None, prefix=None, name=None)[source]

Initialize the TempDir class.

Parameters

dir: str, default None

the location where the temporary folder is created. If specified the folder is persistent.

prefix: str, default None

if set the directory name will begin with that prefix.

name: str, default

if set the directory name will have this name.

pynet.utils.checkpoint(model, epoch, fold, outdir, optimizer=None, scheduler=None, **kwargs)[source]

Save the weights of a given model.

Parameters

model: Net

the network model.

epoch: int

the epoch index.

fold: int

the fold index.

outdir: str

the destination directory where a ‘model_<fold>_epoch_<epoch>.pth’ file will be generated.

optimizer: Optimizer, default None

the network optimizer (save the hyperparameters, etc.).

scheduler: Scheduler, default None

the network scheduler.

kwargs: dict

others parameters to save.

pynet.utils.freeze_layers(model, layer_names)[source]

Freeze some wights in a network based on layer names.

Parameters

model: Net

the network model.

layer_names: list of str

the layer associated weights to be frozen.

pynet.utils.get_activation_name(activation)[source]

Return the name of the activation.

Parameters

activation: torch.nn.modules.activation or str

the activation function.

Returns

name: str

the name of the activation function.

pynet.utils.get_named_layers(model, allowed_layers=[<MagicMock name='mock.Conv2d' id='140139318840288'>, <MagicMock name='mock.Conv3d' id='140139318869296'>, <MagicMock name='mock.ConvTranspose2d' id='140139318898248'>, <MagicMock name='mock.ConvTranspose3d' id='140139318927256'>, <MagicMock name='mock.BatchNorm2d' id='140139318960424'>, <MagicMock name='mock.BatchNorm3d' id='140139318989432'>, <MagicMock name='mock.Linear' id='140139319014400'>], resume=False)[source]

Function that returned a dictionary with named layers.

Parameters

model: Net

the network model.

allowed_layers: list of str, default ALLOWED_LAYERS

the allowed modules.

resume: bool, default False

simplify layer names and skip type checking.

Returns

layers: dict

the named layers.

pynet.utils.get_tools(tool_name=None)[source]

List all available Deep Learning tools.

Parameters

tool_name: str, default None

the tool of interset (‘networks’, ‘regularizers’, ‘losses’ or ‘metrics’), if ‘None’ return all tools available.

Returns

tools: dict

all available tools for Deep Learning application.

pynet.utils.init_weight(module, act_func=None)[source]

Init input module weights.

[1] Understanding the difficulty of training deep feedforward neural networks, PMLR 2010. [2] Delving Deep into Rectifiers: Surpassing Human-Level Performance on ImageNet Classification, arXiv 2015.

Parameters

module: torch.nn.Module

module to initilaize.

act_func: torch.nn.modules.activation, default None

the activation functions.

pynet.utils.layer_at(model, layer_name, x, allowed_layers=[<MagicMock name='mock.Conv2d' id='140139318840288'>, <MagicMock name='mock.Conv3d' id='140139318869296'>, <MagicMock name='mock.ConvTranspose2d' id='140139318898248'>, <MagicMock name='mock.ConvTranspose3d' id='140139318927256'>, <MagicMock name='mock.BatchNorm2d' id='140139318960424'>, <MagicMock name='mock.BatchNorm3d' id='140139318989432'>, <MagicMock name='mock.Linear' id='140139319014400'>])[source]

Access intermediate layers of pretrained network.

Parameters

model: Net

the network model.

layer_name: str

the layer name to be inspected.

x: torch.Tensor

an input tensor.

allowed_layers: list of str, default ALLOWED_LAYERS

the allowed modules.

Returns

layer_data: torch.Tensor or list

the tensor generated at the requested location.

weight: torch.Tensor

the layer associated weight.

pySAP logo is ascii art using Big Money-ne.

Returns

logo: str

the logo.

pynet.utils.reset_weights(model, checkpoint=None)[source]

Reset all the weights of a model. If a checkpoint is given, restore the checkpoint weights.

Parameters

model: Net

the network model.

checkpoint: dict

the saved model weights.

pynet.utils.setup_logging(level='info', logfile=None)[source]

Setup the logging.

Parameters

logfile: str, default None

the log file.

pynet.utils.test_model(model, shape)[source]

Simple function to test a model.

Parameters

model: Net

the network model.

shape: list of int

the shape of a classical input batch dataset.

Follow us

© 2019, pynet developers .
Inspired by AZMIND template.