Classification

ClassificationNetworkBasePyTorch

class ClassificationNetworkBasePyTorch(in_channels: int, n_outputs: int, **kwargs)[source]

Bases: delira.models.abstract_network.AbstractPyTorchNetwork

Implements basic classification with ResNet18

References

https://arxiv.org/abs/1512.03385

See also

AbstractPyTorchNetwork

static _build_model(in_channels: int, n_outputs: int, **kwargs)[source]

builds actual model (resnet 18)

Parameters
  • in_channels (int) – number of input channels

  • n_outputs (int) – number of outputs (usually same as number of classes)

  • **kwargs (dict) – additional keyword arguments

Returns

created model

Return type

torch.nn.Module

_init_kwargs = {}
static closure(model: delira.models.abstract_network.AbstractPyTorchNetwork, data_dict: dict, optimizers: dict, losses=None, metrics=None, fold=0, **kwargs)[source]

closure method to do a single backpropagation step

Parameters
  • model (ClassificationNetworkBasePyTorch) – trainable model

  • data_dict (dict) – dictionary containing the data

  • optimizers (dict) – dictionary of optimizers to optimize model’s parameters

  • losses (dict) – dict holding the losses to calculate errors (gradients from different losses will be accumulated)

  • metrics (dict) – dict holding the metrics to calculate

  • fold (int) – Current Fold in Crossvalidation (default: 0)

  • **kwargs – additional keyword arguments

Returns

  • dict – Metric values (with same keys as input dict metrics)

  • dict – Loss values (with same keys as input dict losses)

  • list – Arbitrary number of predictions as torch.Tensor

Raises

AssertionError – if optimizers or losses are empty or the optimizers are not specified

forward(input_batch: torch.Tensor)[source]

Forward input_batch through network

Parameters

input_batch (torch.Tensor) – batch to forward through network

Returns

Classification Result

Return type

torch.Tensor

property init_kwargs

Returns all arguments registered as init kwargs

Returns

init kwargs

Return type

dict

static prepare_batch(batch: dict, input_device, output_device)[source]

Helper Function to prepare Network Inputs and Labels (convert them to correct type and shape and push them to correct devices)

Parameters
  • batch (dict) – dictionary containing all the data

  • input_device (torch.device) – device for network inputs

  • output_device (torch.device) – device for network outputs

Returns

dictionary containing data in correct type and shape and on correct device

Return type

dict

VGG3DClassificationNetworkPyTorch

class VGG3DClassificationNetworkPyTorch(in_channels: int, n_outputs: int, **kwargs)[source]

Bases: delira.models.classification.classification_network.ClassificationNetworkBasePyTorch

Exemplaric VGG Network for 3D Classification

Notes

The original network has been adjusted to fit for 3D data

References

https://arxiv.org/abs/1409.1556

static _build_model(in_channels: int, n_outputs: int, **kwargs)[source]

Helper Function to build the actual model

Parameters
  • in_channels (int) – number of input channels

  • n_outputs (int) – number of outputs

  • **kwargs – additional keyword arguments

Returns

ensembeled model

Return type

torch.nn.Module

_init_kwargs = {}
static closure(model: delira.models.abstract_network.AbstractPyTorchNetwork, data_dict: dict, optimizers: dict, losses=None, metrics=None, fold=0, **kwargs)

closure method to do a single backpropagation step

Parameters
  • model (ClassificationNetworkBasePyTorch) – trainable model

  • data_dict (dict) – dictionary containing the data

  • optimizers (dict) – dictionary of optimizers to optimize model’s parameters

  • losses (dict) – dict holding the losses to calculate errors (gradients from different losses will be accumulated)

  • metrics (dict) – dict holding the metrics to calculate

  • fold (int) – Current Fold in Crossvalidation (default: 0)

  • **kwargs – additional keyword arguments

Returns

  • dict – Metric values (with same keys as input dict metrics)

  • dict – Loss values (with same keys as input dict losses)

  • list – Arbitrary number of predictions as torch.Tensor

Raises

AssertionError – if optimizers or losses are empty or the optimizers are not specified

forward(input_batch: torch.Tensor)

Forward input_batch through network

Parameters

input_batch (torch.Tensor) – batch to forward through network

Returns

Classification Result

Return type

torch.Tensor

property init_kwargs

Returns all arguments registered as init kwargs

Returns

init kwargs

Return type

dict

static prepare_batch(batch: dict, input_device, output_device)

Helper Function to prepare Network Inputs and Labels (convert them to correct type and shape and push them to correct devices)

Parameters
  • batch (dict) – dictionary containing all the data

  • input_device (torch.device) – device for network inputs

  • output_device (torch.device) – device for network outputs

Returns

dictionary containing data in correct type and shape and on correct device

Return type

dict

ClassificationNetworkBaseTf

class ClassificationNetworkBaseTf(in_channels: int, n_outputs: int, **kwargs)[source]

Bases: delira.models.abstract_network.AbstractTfNetwork

Implements basic classification with ResNet18

See also

AbstractTfNetwork

_add_losses(losses: dict)[source]

Adds losses to model that are to be used by optimizers or during evaluation

Parameters

losses (dict) – dictionary containing all losses. Individual losses are averaged

_add_optims(optims: dict)[source]

Adds optims to model that are to be used by optimizers or during training

Parameters

optim (dict) – dictionary containing all optimizers, optimizers should be of Type[tf.train.Optimizer]

static _build_model(n_outputs: int, **kwargs)[source]

builds actual model (resnet 18)

Parameters
  • n_outputs (int) – number of outputs (usually same as number of classes)

  • **kwargs – additional keyword arguments

Returns

created model

Return type

tf.keras.Model

_init_kwargs = {}
static closure(model: Type[delira.models.abstract_network.AbstractTfNetwork], data_dict: dict, metrics=None, fold=0, **kwargs)[source]

closure method to do a single prediction. This is followed by backpropagation or not based state of on model.train

Parameters
  • model (AbstractTfNetwork) – AbstractTfNetwork or its child-clases

  • data_dict (dict) – dictionary containing the data

  • metrics (dict) – dict holding the metrics to calculate

  • fold (int) – Current Fold in Crossvalidation (default: 0)

  • **kwargs – additional keyword arguments

Returns

  • dict – Metric values (with same keys as input dict metrics)

  • dict – Loss values (with same keys as those initially passed to model.init). Additionally, a total_loss key is added

  • dict – outputs of model.run

property init_kwargs

Returns all arguments registered as init kwargs

Returns

init kwargs

Return type

dict

static prepare_batch(batch: dict, input_device, output_device)

Converts a numpy batch of data and labels to suitable datatype and pushes them to correct devices

Parameters
  • batch (dict) – dictionary containing the batch (must have keys ‘data’ and ‘label’

  • input_device – device for network inputs

  • output_device – device for network outputs

Returns

dictionary containing all necessary data in right format and type and on the correct device

Return type

dict

Raises

NotImplementedError – If not overwritten by subclass

run(*args, **kwargs)

Evaluates self.outputs_train or self.outputs_eval based on self.training

Parameters
  • *args – currently unused, exist for compatibility reasons

  • **kwargs – kwargs used to feed as self.inputs. Same keys as for self.inputs must be used

Returns

sames keys as outputs_train or outputs_eval, containing evaluated expressions as values

Return type

dict