mednet.models.loss_weights

Methods to compute loss-weights for samples based on their classes.

Functions

get_positive_weights(dataloader)

Compute the weights of each class of a DataLoader.

task_and_target_type(targets)

Determine the type of task from combined targets available.

mednet.models.loss_weights.task_and_target_type(targets)[source]

Determine the type of task from combined targets available.

This function will look into the provided targets of a dataset and will determine the sought classifier or segmenter type.

Parameters:

targets (Union[Tensor, ndarray[Any, dtype[TypeVar(_ScalarType_co, bound= generic, covariant=True)]], Iterable[Iterable[int]], Iterable[Iterable[Iterable[Iterable[int]]]]]) – The complete target set, for the whole dataset being analyzed. This matrix should be [n, C] where n is the number of samples, and C the number of classes. All values should be either 0 or 1.

Return type:

tuple[Literal['classification', 'segmentation'], Literal['binary', 'multiclass', 'multilabel']]

Returns:

The type of task and targets available.

mednet.models.loss_weights.get_positive_weights(dataloader)[source]

Compute the weights of each class of a DataLoader.

This function inputs a pytorch DataLoader and computes the ratio between number of negative and positive samples (scalar). The weight can be used to adjust minimisation criteria to in cases there is a huge data imbalance.

It returns a vector with weights (inverse counts) for each target.

Parameters:

dataloader (DataLoader) – A DataLoader from which to compute the positive weights. Entries must be a dictionary which must contain a target key.

Return type:

Tensor

Returns:

The positive weight of each class in the dataset given as input.

Raises:

NotImplementedError – In the case of “multilabel” datasets, which are currently not supported.