datagnosis.plugins.utils module#

apply_augly(image: PIL.Image.Image) torch.Tensor[source]#

The function applies a set of image augmentations using the AugLy library and returns the augmented image as a tensor. It is used for the ALLS HCM for the augmentation

Parameters

image (Image.Image) – The input image that needs to be augmented.

Returns

An augmented tensor image. The image is being transformed using a list of augmentations and then converted to a tensor using PyTorch’s transforms.ToTensor() function.

Return type

torch.Tensor

cache_update_values(update_values: List[Any], path: Union[str, pathlib.Path]) Any[source]#

This function caches the update values required for the plugin _update() call.

Parameters
  • update_values (List[Any]) – The values required for the plugin _update() call.

  • path (Union[str, Path]) – The path to the cache file.

Returns

The cached values required for the plugin _update() call.

Return type

Any

get_all_args_hash(all_args: dict) str[source]#

This function takes all the args for a plugin and returns a hash of them. It is used in the cacheing system to determine if a plugin has already been run with the same args.

Parameters

all_args (dict) – A dictionary of all the args for a plugin.

Returns

A hash of the args.

Return type

str

get_intermediate_outputs(net: torch.nn.modules.module.Module, dataloader: torch.utils.data.dataloader.DataLoader, device: Union[str, torch.device] = device(type='cpu')) Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor][source]#

This function takes a neural network, a dataloader, and a device, and returns the logits, targets, probabilities, and indices of the intermediate outputs of the network on the given data.

Parameters
  • net (nn.Module) – a PyTorch neural network model

  • dataloader (DataLoader) – A PyTorch DataLoader object that provides batches of data to the model for inference

or evaluation.

device (Union[str, torch.device] ): The device on which the computation is being performed, such as “cpu” or “cuda”.

Returns

A tuple of four tensors: logits, targets, probs, and indices.

Return type

(Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor])

get_json_serializable_args(args: Dict) Dict[source]#

This function should take the args for a plugin and makes them serializable with json. Currently it only handles pathlib.Path and DataHandler objects.

Parameters

args (Dict) – A dictionary of args to be made serializable.

Returns

A dictionary of the same args, but now json serializable.

Return type

Dict

kl_divergence(p: Union[numpy.ndarray, torch.Tensor], q: Union[numpy.ndarray, torch.Tensor]) Union[numpy.ndarray, torch.Tensor][source]#

The function calculates the Kullback-Leibler divergence between two probability distributions.

Parameters
  • p (Union[np.ndarray, torch.Tensor]) – The variable p represents a probability distribution. It could be a tensor or a numpy array containing probabilities of different events.

  • q (Union[np.ndarray, torch.Tensor]) – The parameter q is a probability distribution that we are comparing to another probability distribution p using the Kullback-Leibler (KL) divergence formula. KL divergence measures the difference between two probability distributions.

Returns

The function kl_divergence returns the Kullback-Leibler divergence between two probability distributions p and q.

Return type

(Union[np.ndarray, torch.Tensor])

load_update_values_from_cache(path: Union[str, pathlib.Path]) Any[source]#

This function loads the update values from the cache.

Parameters

path (Union[str, Path]) – The path to the cache file.

Returns

The cached values required for the plugin _update() call.

Return type

Any

make_functional_with_buffers(mod: torch.nn.modules.module.Module, disable_autograd_tracking: bool = False) Tuple[Callable, Any, Tuple][source]#

This function takes a PyTorch module and returns a functional version of it, along with the buffers and parameters of the module. This is a workaround for the fact that when functorch was brought into PyTorch 2.0 it no longer supported functional modules with buffers. This function is used in the “GraNd” plugin to migrate from the old functorch implementation to the new PyTorch implementation.

Parameters
  • mod (nn.Module) – A PyTorch module.

  • disable_autograd_tracking (bool, optional) – Whether to disable autograd tracking. Defaults to False.

Returns

A tuple of the functional module, the parameters, and the buffers,.

Return type

Tuple[Callable, Any, Tuple]