Global - > Utils Functions

Device

tardis_em.utils.device.get_device(device: str | list | tuple = '0') device

Determines and returns a device object or list of device objects based on the input, with support for different configurations such as GPU, CPU, MPS (for Apple silicon), or specific device indices.

This function is responsible for creating a torch.device object or a list of them, depending on the input device configuration. It supports various input types and values for device specification and ensures fallbacks when the desired devices are unavailable.

Parameters:

device – Any of the following supported values: - “gpu”: Uses the first GPU if available, otherwise falls back to CPU. - “cpu”: Explicitly returns a device for the CPU. - “mps”: For Apple silicon, utilizes the MPS device if available, defaults to CPU otherwise. - str: A specific GPU ID as a string (e.g., “0”). Falls back to CPU if the GPU is unavailable. - int: A specific GPU ID in integer form. - list or tuple: A collection of GPU IDs for handling multiple devices. The function returns a list of torch.device objects corresponding to the indices.

Returns:

A torch.device object or a list of torch.device objects depending on the input configuration.

tardis_em.utils.device.device_is_str(device: str = '0') bool

Checks if the provided device identifier string represents a valid integer and determines if the input can be interpreted as a valid device representation.

Parameters:

device (str) – The device identifier as a string. Defaults to “0”.

Returns:

True if the input string can be cast to an integer and is a valid integer type; otherwise, False.

Return type:

bool

Global Utils

tardis_em.utils.utils.get_n_params(model)

Calculate the total number of parameters in a given machine learning model.

This function computes the total number of learnable parameters for a given model by iterating through all its parameters and multiplying the sizes of each parameter tensor.

Parameters:

model – The machine learning model whose parameters are to be counted. The model must have a ‘parameters()’ method that provides an iterable of tensors representing the parameters.

Returns:

Integer value indicating the total number of parameters in the provided model.

Return type:

int

class tardis_em.utils.utils.EarlyStopping(patience=10, min_delta=0)

Implements early stopping for training processes.

This class monitors a specified metric (validation loss or F1 score) during a training process and determines when to stop training to prevent overfitting or unnecessary computation. Early stopping halts the training if the monitored metric stops improving according to specified patience and threshold (min_delta) values.

tardis_em.utils.utils.check_uint8(image: ndarray)

Verify and process an input image of type uint8 for specific conditions: binary mask, multi-label mask, raw data, or empty image. Based on the image’s unique pixel values, the function adapts its format accordingly.

Parameters:

image (numpy.ndarray) – Input image as a numpy array to be verified and processed

Returns:

Processed image based on the unique pixel values

Return type:

numpy.ndarray