mednet.models.loss_weights

Functions

compute_binary_weights(targets)

Compute the positive weights when using binary targets.

compute_multiclass_weights(targets)

Compute the positive weights when using exclusive, multiclass targets.

compute_non_exclusive_multiclass_weights(targets)

Compute the positive weights when using non-exclusive, multiclass targets.

get_positive_weights(dataloader)

Compute the weights of each class of a DataLoader.

is_multicalss_exclusive(targets)

Given a [C x n] tensor of integer targets, checks whether samples can only belong to a single class.

tensor_to_list(tensor)

Convert a torch.Tensor to a list.

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

Compute the positive weights when using binary targets.

Parameters:

targets – A tensor of integer values of length n.

Returns:

The positive weights per class.

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

Compute the positive weights when using exclusive, multiclass targets.

Parameters:

targets – A [C x n] tensor of integer values, where C is the number of target classes and n the number of samples.

Returns:

The positive weights per class.

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

Compute the positive weights when using non-exclusive, multiclass targets.

Parameters:

targets – A [C x n] tensor of integer values, where C is the number of target classes and n the number of samples.

Returns:

The positive weights per class.

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

Given a [C x n] tensor of integer targets, checks whether samples can only belong to a single class.

Parameters:

targets (Tensor) – A [C x n] tensor of integer values, where C is the number of target classes and n the number of samples.

Return type:

bool

Returns:

True if all samples belong to a single class, False otherwise (a sample can belong to multiple classes).

mednet.models.loss_weights.tensor_to_list(tensor)[source]

Convert a torch.Tensor to a list.

This is necessary, as torch.tolist returns an int when then tensor contains a single value.

Parameters:

tensor – The tensor to convert to a list.

Return type:

list[Any]

Returns:

The tensor converted to a list.

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.