Utilities

utils.__init__.check_mkdir(dir_name: str) None

Utility function that creates a directory if the path does not exist

Parameters

dir_name – str

Returns

Tracing

Utility functions for model debugging, setup and loading

Checkpoint

GPU

utils.gpu.get_available_gpus(memory_threshold: float = 0.0, metric: str = 'mb') List

Get all the available GPUs using less memory than a specified threshold

Parameters
  • memory_threshold – maximum memory usage threshold to reject

  • metric – GB or MB

Returns

List

utils.gpu.get_memory_map() dict

Get the current gpu usage.

Returns

usage – Keys are device ids as integers. Values are memory usage as integers in MB.

Return type

dict

utils.gpu.get_stats() pandas.core.frame.DataFrame

Get statistics of all GPUs in a DataFrame

Returns

Logger

utils.logger.setup_logger(log_directory: str, model_name: str) None

Function for setting up the logger for debugging purposes

Parameters
  • log_directory

  • model_name

Returns

utils.logger.tracer(func)

Decorator to print function call details :param func: :return:

Metrics

Loss

class utils.metrics.loss.ACWLoss(ini_weight=0, ini_iteration=0, eps=1e-05, ignore_index=255)
__init__(ini_weight=0, ini_iteration=0, eps=1e-05, ignore_index=255)

Adaptive Class Weighting Loss is the loss function class for handling the highly imbalanced distribution of images Multi-class adaptive class loss function

Adaptive Class Weighting Loss

\[L_{acw}=\frac{1}{|Y|}\sum_{i\in Y}\sum_{j\in C}{\tilde{w}_{ij}\times p_{ij} -log{( \text{ MEAN}\{ d_j| j\in C \} )} }\]

Dice coefficient

\[d_j = \frac{ 2\sum_{i\in Y}y_{ij} \tilde{y}_{ij}} {\sum_{ij}y_{ij} + \sum_{i\in Y}\tilde{ y}_{ij} }\]
Parameters
  • ini_weight

  • ini_iteration

  • eps

:param ignore_index:z

adaptive_class_weight(pred, one_hot_label, mask=None)

Adaptive Class Weighting (ACW) computed based on the iterative batch-wise class derived from the median frequency to balance weights.

ACW

\[\tilde{w}_{ij}=\frac{ w^t_j} { \sum_{j\in C}(w^t_j) }\times (1 + y_{ij} + \tilde{y}_{ij})\]

Iterative Median Frequency Class Weights

\[w^t_j=\frac{ \text{MEDIAN} (\{ f^t_j | j \in C \}) } {f^t_j+\epsilon}\mid\epsilon=10^{-5}\]

Pixel Frequency

\[f^t_j=\frac{\hat{f^t_j}+(t-1)\times f^{t-1}_j} {t} \mid t\in \{1,2,...,\infty\}\]
Parameters
  • pred

  • one_hot_label

  • mask

Returns

forward(prediction, target)

pred : shape (N, C, H, W) target : shape (N, H, W) ground truth return: loss_acw

pnc(err)

Apply positive-negative class balanced function (PNC)

PNC

\[p = e - \log\left(\frac{1-e}{1+e}\right)\mid e=(y-\tilde{y})^2\]
Parameters

err

Returns

Optimizer

class utils.metrics.optimizer.Lookahead(base_optimizer, alpha=0.5, k=6)
load_state_dict(state_dict)

Loads the optimizer state.

Parameters

state_dict (dict) – optimizer state. Should be an object returned from a call to state_dict().

state_dict()

Returns the state of the optimizer as a dict.

It contains two entries:

  • state - a dict holding current optimization state. Its content

    differs between optimizer classes.

  • param_groups - a list containing all parameter groups where each

    parameter group is a dict

step(closure=None)

Performs a single optimization step (parameter update).

Parameters

closure (callable) – A closure that reevaluates the model and returns the loss. Optional for most optimizers.

Note

Unless otherwise specified, this function should not modify the .grad field of the parameters.

Learning Rate

utils.metrics.lr.adjust_initial_rate(optimizer, i_iter, opt, model='cos')

Function for adjusting scheduling learning rate in accordance to a specified model with the provided optimizer

Parameters
  • optimizer

  • i_iter

  • opt

  • model – “cos” denotes cosine annealing to reduce lr over epochs

Returns

utils.metrics.lr.adjust_learning_rate(optimizer, i_iter, opt)
Parameters
  • optimizer

  • i_iter

  • opt

Returns

utils.metrics.lr.init_params_lr(net, opt)
Parameters
  • net

  • opt

Returns

utils.metrics.lr.lr_cos(base_lr, iteration, max_iterations)
Parameters
  • base_lr

  • iteration

  • max_iterations

Returns

utils.metrics.lr.lr_poly(base_lr, iteration, max_iterations, power)
Parameters
  • base_lr

  • iteration

  • max_iterations

  • power

Returns

Validate

utils.metrics.validate.evaluate(predictions, gts, num_classes)

Function for evaluating the collection of predictions given the set of ground-truths

Parameters
  • predictions

  • gts

  • num_classes

Returns

utils.metrics.validate.multiprocess_evaluate(predictions, gts, num_classes)

Function for evaluating the collection of predictions given the set of ground-truths

Parameters
  • predictions

  • gts

  • num_classes

Returns

Export

Android

utils.export.android.convert_to_mobile(model: str, source_path: str, output_path: str, num_classes: int) torch.nn.modules.module.Module

Main function for converting MSCG core to PyTorch Mobile

NOTE Usage of PyTorch Mobile to convert the MSCG-Nets requires usage of a matching Android PyTorch Mobile Version 1.10

Parameters
  • num_classes

  • model

  • source_path

  • output_path

Returns

Visualizations

Configuration