Sub Packages

unagi.utils

Unagi has one sub package called utils which contains several methods and classes that are used to create the dataset and train the model.

unagi.utils.augmentor_utils

Different augmentation objects are created to use during the training. This module contains those different augmentation objects used in the training.

class unagi.utils.augmentor_utils.GaussianNoiseAugmentor(probability: float, mean: float, sigma: float)[source]

Bases: Operation

Gaussian Noise in Augmentor format.

Parameters:
  • probability (float) – probability of operation being performed

  • mean (float) – mean of the pixels

  • sigma (float) – standard deviation of the pixels

perform_operation(images: List[ndarray]) List[ndarray][source]

Apply operation to a batch of images.

Parameters:

image (List[numpy.ndarray]) – list images to perform opetation

Returns:

list of image arrays after operation is performed

Return type:

List[numpy.ndarray]

class unagi.utils.augmentor_utils.InvertPartAugmentor(probability: float)[source]

Bases: Operation

Invert colors in Augmentor formant.

perform_operation(images: List[ndarray]) List[ndarray][source]

Apply invert operation to a batch of images.

Parameters:

image (List[numpy.ndarray]) – list of images to perform opetation

Returns:

list of image arrays after operation is performed

Return type:

List[numpy.ndarray]

class unagi.utils.augmentor_utils.SaltPepperNoiseAugmentor(probability: float, prop: float)[source]

Bases: Operation

Salt Pepper Noise in Augmentor format.

Parameters:
  • probability (float) – probability of operation being performed

  • prop (float) – image proportion value to keep

perform_operation(images: List[ndarray]) List[ndarray][source]

Apply operation to a batch of images.

Parameters:

image (List[numpy.ndarray]) – list of images to perform opetation

Returns:

list of image arrays after operation is performed

Return type:

List[numpy.ndarray]

unagi.utils.callback_utils

Callbacks for visulations of the training and saving the model are created using the objects in callback_utils module.

class unagi.utils.callback_utils.Visualisation(batchsize: int, dir_name: str = 'vis', monitor: str = 'val_loss', save_best_epochs_only: bool = False, mode: str = 'min')[source]

Bases: Callback

Custom Keras callback for visualizing training through GIFs.

Parameters:
  • batchsize (int) – batchsize to generate samples

  • dir_name (str, optional) – folder with images to visualize training

  • monitor (str, optional) – metric to monitor

  • save_best_epochs_only (bool, optional) – save the weights only when the metric improves

  • mode (str, optional) – mode for the metric to monitor

on_epoch_end(epoch: int, logs: Dict[str, Any]) None[source]

Saves prediction of image after epoch into a folder with the same name.

Parameters:
  • epoch (int) – current epoch number

  • logs (Dict[str, Any]) – dictionary of metrics

on_train_end(logs=None) None[source]

Saves metrics for each iteration to a dictionary and saves images as gifs.

unagi.utils.callback_utils.create_callbacks(model: Model, original_model: Model, debug: str, num_gpus: int, batchsize: int, vis: str, weights_path: str) List[str][source]

Create Keras callbacks for training.

Parameters:
  • model (keras_model) – keras model

  • original_model (keras_model) – model to use when num_gpus > 1

  • debug (str) – path to save weights and tensorboard logs

  • num_gpus (int) – number of gpus

  • batchsize (int) – batchsize to use during training visualization

  • vis (str) – images to read for training visualization

  • weights_path (str) – path to save final weights

Returns:

list of callbacks tu use in training.

Return type:

List[str]

See also

Visualisation

Example

unagi.utils.callbacks_utils.create_callbacks(

model, gpu_model, logs, 1, 32, vis_imgs, weights )

unagi.utils.img_processing_utils

Different image manipulation techniques are used in UNAGI. Dataset module uses different methods to create the images parts and preprocess the images during training.

class unagi.utils.img_processing_utils.ImageUtils[source]

Bases: object

Util Class to hold all image processing methods. Static methods are used to do some I/O tasks and processing images, without instantiating the util class.

static add_border(img: ndarray, size_x: int = 128, size_y: int = 128) Tuple[ndarray, int, int][source]

Add border to image based on the inputs

Parameters:
  • img (numpy.ndarray) – image array to add border

  • size_x (int) – width for image part (deafult is 128).

  • size_y (int) – height for image part (deafult is 128).

Returns:

image array with border border value added on width border value added on height

Return type:

Tuple[np.ndarray, int, int]

Example

unagi.utils.img_processing_utils.ImageUtils.add_border(img_array, 128, 128)

static binarize_img(img: ndarray, model: Model, batchsize: int = 2) ndarray[source]

Binarize image, using U-net, Otsu, bottom-hat transform etc.

Parameters:
  • img (numpy.ndarray) – image array to preprocess

  • model (keras_model) – keras model

  • batchsize (int, optional) – batchsize to use with the model (default is 2)

Returns:

image array after binarizing

Return type:

numpy.ndarray

Example

unagi.utils.img_processing_utils.ImageUtils.binarize_img(

process_unet_img, model)

static combine_imgs(imgs: List[ndarray], max_y: int, max_x: int) ndarray[source]

Combine image parts to one big image.

Parameters:
  • img (List[numpy.ndarray]) – list image arraies to combine

  • max_y (int) – width for image part (deafult is 128).

  • max_x (int) – height for image part (deafult is 128).

Returns:

numpy.ndarray of combined image

Return type:

numpy.ndarray

Note

Walk through list of images and create from them one big image with sizes max_x * max_y. If border_x and border_y are non-zero, they will be removed from created image. The list of images should contain data in the following order: from left to right, from top to bottom.

Example

unagi.utils.img_processing_utils.ImageUtils.combine_imgs(

img_array_list, 128, 128)

static mkdir_s(path: str) None[source]

Create directory in specified path, if not exists.

Parameters:

path (str) – directory name to create

Return type:

None

Example

unagi.utils.img_processing_utils.ImageUtils.mkdir_s(dir_name)

static normalize_gt(img: ndarray) ndarray[source]

Normalize the gt image to have all pixels in range 0 to 1.

Parameters:

img (numpy.ndarray) – image array to normalize

Returns:

normalized image array

Return type:

numpy.ndarray

Example

unagi.utils.img_processing_utils.ImageUtils.normalize_gt(img_array)

static normalize_in(img: ndarray) ndarray[source]

Normalize the input image to have all pixels in range 0 to 1.

Parameters:

img (numpy.ndarray) – image array to normalize

Returns:

normalized image array

Return type:

numpy.ndarray

Example

unagi.utils.img_processing_utils.ImageUtils.normalize_in(img_array)

static postprocess_img(img: ndarray) ndarray[source]

Apply Otsu threshold to image.

Parameters:

img (numpy.ndarray) – image array to postprocess

Returns:

postprocessed image array

Return type:

numpy.ndarray

Example

unagi.utils.img_processing_utils.ImageUtils.postprocess_img(img_array)

static process_with_unagi(img: ndarray, model: Model, batchsize: int) ndarray[source]

Split image to 128x128 parts and run U-net for every part.

Parameters:
  • img (numpy.ndarray) – image array to preprocess

  • model (keras_model) – keras model

  • batchsize (int) – batchsize to use with the model

Returns:

image array after preprocessing

Return type:

numpy.ndarray

Example

unagi.utils.img_processing_utils.ImageUtils.process_with_unagi(

process_unet_img, model)

static shuffle_imgs(dname: str) None[source]

Shuffle input and ground-truth images.

(actual, if You are using different datasets as one).

Parameters:

dname (str) – directory name with image to shuffle

Return type:

None

Example

unagi.utils.img_processing_utils.ImageUtils.shuffle_imgs(images_dir)

static split_img(img: ndarray, size_x: int = 128, size_y: int = 128) List[ndarray][source]

Split image to parts (little images).

Parameters:
  • img (numpy.ndarray) – image array to split

  • size_x (int) – width for image part (deafult is 128).

  • size_y (int) – height for image part (deafult is 128).

Returns:

list of numpy.ndarray of image parts

Return type:

List[numpy.ndarray]

Note

Walk through the whole image by the window of size size_x * size_y without overlays and save all parts in list. Images sizes should divide window sizes.

Example

unagi.utils.img_processing_utils.ImageUtils.split_img(img_array, 128, 128)

unagi.utils.metric_utils

UNAGI uses different loss functions and metrics to measure the performace of the model training. This module contains different loss functions and metrics used in the training and validation of the model.

class unagi.utils.metric_utils.ModelMetrics[source]

Bases: object

Util Class to hold all metric methods for model evaluation. Static methods are used to calculate metrics without instantiating the class.

static dice_coef(y_true: Tensor, y_pred: Tensor) float[source]

Count Sorensen-Dice coefficient for output and ground-truth image.

Parameters:
  • y_true (tf.Tensor) – tensor of true pixel values

  • y_pred (tf.Tensor) – tensor of predicted pixel values

Returns:

dice coefficient calculated on predicted and input class values.

Return type:

float

Example

unagi.utils.metric_utils.ModelMetrics.dice_coef(y_true, y_pred)

static dice_coef_loss(y_true: Tensor, y_pred: Tensor) float[source]

Loss of Sorensen-Dice coefficient for output and ground-truth image.

Parameters:
  • y_true (tf.Tensor) – tensor of true pixel values

  • y_pred (tf.Tensor) – tensor of predicted pixel values

Returns:

dice loss calculated from dice coefficient.

Return type:

float

See also

dice_coef

Example

unagi.utils.metric_utils.ModelMetrics.dice_coef_loss(y_true, y_pred)

static focal_loss(alpha: float = 0.25, gamma: float = 2.0) Callable[[Any, Any], float][source]

Calculates focal loss on predicted pixels.

Parameters:
  • alpha (float) – tensor of true pixel values

  • gamma (float) – tensor of predicted pixel values

Returns:

focal loss value calculated predicted pixels.

Return type:

Callable[[Any, Any], float]

References

[1] keras implementation : lars76.github.io/neural-networks/object-detection/losses-for-segmentation/

[2] oiginal paper : Focal Loss for Dense Object Detection https://arxiv.org/abs/1708.02002

Example

unagi.utils.metric_utils.ModelMetrics.focal_loss()

static focal_tversky(y_true: Tensor, y_pred: Tensor) float[source]

Calculates focal-tversky loss based on tversky index for predicted pixels.

Parameters:
  • y_true (tf.Tensor) – tensor of true pixel values

  • y_pred (tf.Tensor) – tensor of predicted pixel valuesass

Returns:

focal-tversky loss value calculated on tversky index for predicted pixels.

Return type:

float

References

[1] keras implementation : https://github.com/nabsabraham/focal-tversky-unet

[2] oiginal paper : A Novel Focal Tversky loss function with improved Attention U-Net for lesion segmentation. https://arxiv.org/abs/1810.07842

Example

unagi.utils.metric_utils.ModelMetrics.focal_tversky(y_true, y_pred)

static jacard_coef(y_true: Tensor, y_pred: Tensor) float[source]

Count Jaccard coefficient for output and ground-truth image.

Parameters:
  • y_true (tf.Tensor) – tensor of true pixel values

  • y_pred (tf.Tensor) – tensor of predicted pixel values

Returns:

Jaccard coefficient calculated on predicted and input class values.

Return type:

float

Example

unagi.utils.metric_utils.ModelMetrics.jacard_coef(y_true, y_pred)

static jacard_coef_loss(y_true: Tensor, y_pred: Tensor) float[source]

Calculates loss based on Jaccard coefficient for output and ground-truth image.

Parameters:
  • y_true (tf.Tensor) – tensor of true pixel values

  • y_pred (tf.Tensor) – tensor of predicted pixel values

Returns:

Jaccard loss calculated from Jaccard coefficient.

Return type:

float

See also

jacard_coef

Example

unagi.utils.metric_utils.ModelMetrics.jacard_coef_loss(y_true, y_pred)

static tversky(y_true: Tensor, y_pred: Tensor) float[source]

Calculates tversky index based on predicted pixels.

Parameters:
  • y_true (tf.Tensor) – tensor of true pixel values

  • y_pred (tf.Tensor) – tensor of predicted pixel valuesass

Returns:

tversky index value calculated on predicted pixels.

Return type:

float

Example

unagi.utils.metric_utils.ModelMetrics.tversky(y_true, y_pred)

static tversky_loss(y_true: Tensor, y_pred: Tensor) float[source]

Calculates tversky loss based on tversky index for predicted pixels.

Parameters:
  • y_true (tf.Tensor) – tensor of true pixel values

  • y_pred (tf.Tensor) – tensor of predicted pixel valuesass

Returns:

tversky loss value calculated on tversky index for predicted pixels.

Return type:

float

Example

unagi.utils.metric_utils.ModelMetrics.tversky_loss(y_true, y_pred)

static weighted_cross_entropy(pos_weight: float = 0.2) Callable[[Any, Any], float][source]

Calculates weighted cross entropy loss on predicted pixels.

Parameters:
  • y_true (tf.Tensor) – tensor of true pixel values

  • y_pred (tf.Tensor) – tensor of predicted pixel values

Returns:

loss value calculated based on the weight factor

Return type:

Callable[[Any, Any], float]

References

[1] keras implementation : lars76.github.io/neural-networks/object-detection/losses-for-segmentation/

Example

unagi.utils.metric_utils.ModelMetrics.weighted_cross_entropy(y_true, y_pred)

unagi.utils.model_utils

Contains methods used to create the U-net architechture used in the model training such as DoubelConv layer, upsampling, and downsampling paths.

class unagi.utils.model_utils.ModelLayers[source]

Bases: object

Util Class to hold all methods to create model layers for UNAGI Model.

static double_conv_layer(inputs: Tensor, kernel_size: int) Tensor[source]

Create a double convolution layer with mentioned inputs and filters.

Parameters:
  • inputs (tf_tensor) – input keras layer

  • kernel_size (int) – filter size/ kernel size for the tf_tensor

Returns:

tensor with mentioned number of filters

Return type:

tf_tensor

Example

unagi.utils.model_utils.ModelLayers.double_conv_layer(Input, 32)

static down_layer(inputs: Tensor, kernel_size: int) Tensor[source]

Create downsampling layer.

Parameters:
  • inputs (tf_tensor) – input keras layer

  • kernel_size (int) – filter size/ kernel size for the tf_tensor

Returns:

tensor with mentioned number of filters

Return type:

tf_tensor

Example

unagi.utils.model_utils.ModelLayers.down_layer(tf_tensor, 64)

static up_layer(inputs: Tensor, concats: Tensor, kernel_size: int) Tensor[source]

Create upsampling layer.

Parameters:
  • inputs (tf_tensor) – input keras layer

  • concats (tf_tensor) – keras layer to concat

  • kernel_size (int) – filter size/ kernel size for the tf_tensor

Returns:

tensor with mentioned number of filters

Return type:

tf_tensor

Example

unagi.utils.model_utils.ModelLayers.up_layer(input, pool_layer, 128)

class unagi.utils.model_utils.UNAGIModel(width: int = 128, height: int = 128, channels: int = 1)[source]

Bases: object

Util Class to create U-net model for UNAGI.

unet() Model[source]

Create U-net for input image/tensor size.

Returns:

keras Model with mentioned number of filters and layers

Return type:

Model

Example

unagi.utils.model_utils.UNetModel.unet()