Utilities

The dh_segment.utils module contains the parameters for config with sacred package, image label vizualization functions and miscelleanous helpers.

Parameters

ModelParams(**kwargs)

Parameters related to the model

TrainingParams(**kwargs)

Parameters to configure training process

Label image helpers

label_image_to_class(label_image, classes_file)

rtype

tensorflow.Tensor

class_to_label_image(class_label, classes_file)

rtype

tensorflow.Tensor

multilabel_image_to_class(label_image, …)

Combines image annotations with classes info of the txt file to create the input label for the training.

multiclass_to_label_image(…)

rtype

tensorflow.Tensor

get_classes_color_from_file(classes_file)

rtype

ndarray

get_n_classes_from_file(classes_file)

rtype

int

get_classes_color_from_file_multilabel(…)

Get classes and code labels from txt file.

get_n_classes_from_file_multilabel(classes_file)

rtype

int

Evaluation utils

Metrics()

intersection_over_union(cnt1, cnt2, shape_mask)

Miscellaneous helpers

parse_json(filename)

dump_json(filename, dict)

load_pickle(filename)

dump_pickle(filename, obj)

hash_dict(params)


class dh_segment.utils.PredictionType
Variables
CLASSIFICATION = 'CLASSIFICATION'
MULTILABEL = 'MULTILABEL'
REGRESSION = 'REGRESSION'
classmethod parse(prediction_type)
class dh_segment.utils.VGG16ModelParams
CORRECTED_VERSION = None
INTERMEDIATE_CONV = [[(256, 3)]]
PRETRAINED_MODEL_FILE = 'pretrained_models/vgg_16.ckpt'
SELECTED_LAYERS_UPSCALING = [True, True, True, True, False, False]
UPSCALE_PARAMS = [[(32, 3)], [(64, 3)], [(128, 3)], [(256, 3)], [(512, 3)], [(512, 3)]]
class dh_segment.utils.ResNetModelParams
CORRECT_VERSION = False
INTERMEDIATE_CONV = None
PRETRAINED_MODEL_FILE = 'pretrained_models/resnet_v1_50.ckpt'
SELECTED_LAYERS_UPSCALING = [True, True, True, True, True]
UPSCALE_PARAMS = [(32, 0), (64, 0), (128, 0), (256, 0), (512, 0)]
class dh_segment.utils.UNetModelParams
CORRECT_VERSION = False
INTERMEDIATE_CONV = None
PRETRAINED_MODEL_FILE = None
SELECTED_LAYERS_UPSCALING = None
UPSCALE_PARAMS = None
class dh_segment.utils.ModelParams(**kwargs)

Parameters related to the model

check_params()
class dh_segment.utils.TrainingParams(**kwargs)

Parameters to configure training process

Variables
  • n_epochs (int) – number of epoch for training

  • evaluate_every_epoch (int) – the model will be evaluated every n epochs

  • learning_rate (float) – the starting learning rate value

  • exponential_learning (bool) – option to use exponential learning rate

  • batch_size (int) – size of batch

  • data_augmentation (bool) – option to use data augmentation (by default is set to False)

  • data_augmentation_flip_lr (bool) – option to use image flipping in right-left direction

  • data_augmentation_flip_ud (bool) – option to use image flipping in up down direction

  • data_augmentation_color (bool) – option to use data augmentation with color

  • data_augmentation_max_rotation (float) – maximum angle of rotation (in radians) for data augmentation

  • data_augmentation_max_scaling (float) – maximum scale of zooming during data augmentation (range: [0,1])

  • make_patches (bool) – option to crop image into patches. This will cut the entire image in several patches

  • patch_shape (tuple) – shape of the patches

  • input_resized_size (int) – size (in pixel) of the image after resizing. The original ratio is kept. If no resizing is wanted, set it to -1

  • weights_labels (list) – weight given to each label. Should be a list of length = number of classes

  • training_margin (int) – size of the margin to add to the images. This is particularly useful when training with patches

  • local_entropy_ratio (float) –

  • local_entropy_sigma (float) –

  • focal_loss_gamma (float) – value of gamma for the focal loss. See paper : https://arxiv.org/abs/1708.02002

check_params()

Checks if there is no parameter inconsistency

Return type

None

dh_segment.utils.label_image_to_class(label_image, classes_file)
Return type

tensorflow.Tensor

dh_segment.utils.class_to_label_image(class_label, classes_file)
Return type

tensorflow.Tensor

dh_segment.utils.multilabel_image_to_class(label_image, classes_file)

Combines image annotations with classes info of the txt file to create the input label for the training.

Parameters
  • label_image (tensorflow.Tensor) – annotated image [H,W,Ch] or [B,H,W,Ch] (Ch = color channels)

  • classes_file (str) – the filename of the txt file containing the class info

Return type

tensorflow.Tensor

Returns

[H,W,Cl] or [B,H,W,Cl] (Cl = number of classes)

dh_segment.utils.multiclass_to_label_image(class_label_tensor, classes_file)
Return type

tensorflow.Tensor

dh_segment.utils.get_classes_color_from_file(classes_file)
Return type

ndarray

dh_segment.utils.get_n_classes_from_file(classes_file)
Return type

int

dh_segment.utils.get_classes_color_from_file_multilabel(classes_file)

Get classes and code labels from txt file. This function deals with the case of elements with multiple labels.

Parameters

classes_file (str) – file containing the classes (usually named classes.txt)

Return type

Tuple[ndarray, <built-in function array>]

Returns

for each class the RGB color (array size [N, 3]); and the label’s code (array size [N, C]), with N the number of combinations and C the number of classes

dh_segment.utils.get_n_classes_from_file_multilabel(classes_file)
Return type

int

dh_segment.utils.parse_json(filename)
dh_segment.utils.dump_json(filename, dict)
dh_segment.utils.load_pickle(filename)
dh_segment.utils.dump_pickle(filename, obj)
dh_segment.utils.hash_dict(params)
class dh_segment.utils.Metrics
compute_accuracy()
compute_iu()
compute_miou()
compute_mse()
compute_prf(beta=1)
compute_psnr()
save_to_json(json_filename)
Return type

None

dh_segment.utils.intersection_over_union(cnt1, cnt2, shape_mask)