TverskyLoss

class hybrid_learning.concepts.train_eval.kpis.batch_kpis.TverskyLoss(factor_false_positives=0.7, reduction=BatchReduction.mean, target_thresh=0.0, from_logit=False)[source]

Bases: AbstractIoULoss

Calc Tversky loss (balanced Dice loss) for given outputs amd targets. The Tversky loss [Salehi2017] works on masks of prediction and ground truth (gt) indicating the foreground (fg) area. The masks may be binary, non-binary or mixed. The target masks are binarized.

Given a balancing factor b, the loss is calculated for one instance as

(1)\[\text{Tversky} = \frac{TP} {(TP + b\cdot FP + (1-b) \cdot FN)}\]

with

  • TP: true positives, respectively the intersection of predicted fg area and gt fg area

  • FP: false positives, respectively the predicted fg area minus the gt fg area

For b=0.5 this is regular Dice loss.

The following tensor dimensions are allowed:

  • 1D: The tensor is assumed to be 1D without batch dimension.

  • 2D: The tensor is assumed to be 2D without batch dimension.

  • >2D: The tensor is assumed to be 2D with batch dimension 0, width dim. -1, height dim. -2.

Salehi2017

S. S. M. Salehi, D. Erdogmus, and A. Gholipour. Tversky loss function for image segmentation using 3D fully convolutional deep networks, 2017. https://arxiv.org/abs/1706.05721

Public Data Attributes:

settings

Settings to reproduce the instance.

Inherited from : py: class:AbstractIoULoss

settings

Settings to reproduce the instance.

Inherited from : py: class:Module

dump_patches

This allows better BC support for load_state_dict().

T_destination

alias of TypeVar('T_destination', bound=Mapping[str, Tensor])

Public Methods:

forward(outputs, targets)

Tversky loss (1) calculation.

Inherited from : py: class:AbstractIoULoss

forward(outputs, targets)

Tversky loss (1) calculation.

Inherited from : py: class:AbstractIoULike

forward(outputs, targets)

Tversky loss (1) calculation.

Inherited from : py: class:Module

forward(outputs, targets)

Tversky loss (1) calculation.

register_buffer(name, tensor[, persistent])

Adds a buffer to the module.

register_parameter(name, param)

Adds a parameter to the module.

add_module(name, module)

Adds a child module to the current module.

get_submodule(target)

Returns the submodule given by target if it exists, otherwise throws an error.

get_parameter(target)

Returns the parameter given by target if it exists, otherwise throws an error.

get_buffer(target)

Returns the buffer given by target if it exists, otherwise throws an error.

apply(fn)

Applies fn recursively to every submodule (as returned by .children()) as well as self.

cuda([device])

Moves all model parameters and buffers to the GPU.

xpu([device])

Moves all model parameters and buffers to the XPU.

cpu()

Moves all model parameters and buffers to the CPU.

type(dst_type)

Casts all parameters and buffers to dst_type.

float()

Casts all floating point parameters and buffers to float datatype.

double()

Casts all floating point parameters and buffers to double datatype.

half()

Casts all floating point parameters and buffers to half datatype.

bfloat16()

Casts all floating point parameters and buffers to bfloat16 datatype.

to_empty(*, device)

Moves the parameters and buffers to the specified device without copying storage.

to(*args, **kwargs)

Moves and/or casts the parameters and buffers.

register_backward_hook(hook)

Registers a backward hook on the module.

register_full_backward_hook(hook)

Registers a backward hook on the module.

register_forward_pre_hook(hook)

Registers a forward pre-hook on the module.

register_forward_hook(hook)

Registers a forward hook on the module.

state_dict([destination, prefix, keep_vars])

Returns a dictionary containing a whole state of the module.

load_state_dict(state_dict[, strict])

Copies parameters and buffers from state_dict into this module and its descendants.

parameters([recurse])

Returns an iterator over module parameters.

named_parameters([prefix, recurse])

Returns an iterator over module parameters, yielding both the name of the parameter as well as the parameter itself.

buffers([recurse])

Returns an iterator over module buffers.

named_buffers([prefix, recurse])

Returns an iterator over module buffers, yielding both the name of the buffer as well as the buffer itself.

children()

Returns an iterator over immediate children modules.

named_children()

Returns an iterator over immediate children modules, yielding both the name of the module as well as the module itself.

modules()

Returns an iterator over all modules in the network.

named_modules([memo, prefix, remove_duplicate])

Returns an iterator over all modules in the network, yielding both the name of the module as well as the module itself.

train([mode])

Sets the module in training mode.

eval()

Sets the module in evaluation mode.

requires_grad_([requires_grad])

Change if autograd should record operations on parameters in this module.

zero_grad([set_to_none])

Sets gradients of all model parameters to zero.

share_memory()

See torch.Tensor.share_memory_()

extra_repr()

Set the extra representation of the module

Special Methods:

__init__([factor_false_positives, ...])

Init.

Inherited from : py: class:AbstractIoULoss

__init__([factor_false_positives, ...])

Init.

Inherited from : py: class:AbstractIoULike

__repr__()

Return repr(self).

__str__()

Return str(self).

Inherited from : py: class:Module

__init__([factor_false_positives, ...])

Init.

__call__(*input, **kwargs)

Call self as a function.

__setstate__(state)

__getattr__(name)

__setattr__(name, value)

Implement setattr(self, name, value).

__delattr__(name)

Implement delattr(self, name).

__repr__()

Return repr(self).

__dir__()

Default dir() implementation.


Parameters
__init__(factor_false_positives=0.7, reduction=BatchReduction.mean, target_thresh=0.0, from_logit=False)[source]

Init.

Parameters
  • target_thresh (float) – threshold to binarize targets

  • factor_false_positives (float) – factor in [0,1] applied to the false positives (see Tversky loss formula (1))

  • reduction (Union[BatchReduction, Callable[[Tensor], Tensor]]) – reduction method to aggregate the instance-wise results of the batch; must be a callable on a tensor which reduces the 0th dimension; for examples see instances of BatchReduction.

  • from_logit (bool) –

forward(outputs, targets)[source]

Tversky loss (1) calculation.

Parameters
  • outputs (Tensor) – input tensor (at least 1D); items must be floats in the range [0,1]

  • targets (Tensor) – targets to compare outputs with (at least 1D; same dimension as input)

Returns

aggregated Tversky loss (1) of outputs for given targets

Return type

Tensor

factor_false_positives: float

Factor applied to the false positives

reduction: Union[BatchReduction, Callable[[Tensor], Tensor]]

Reduction method to aggregate the instance-wise results of the batch.

property settings: Dict[str, Any]

Settings to reproduce the instance.

target_thresh: float

Threshold to binarize the targets.

training: bool