Utilities¶
-
src.pipelines.tensorflow_v2.helpers.utilities.save_prcurve_csv(run_name, mask, pred, type)[source]¶ Saves the result of a precision recall curve as a json. The elements are precision, recall, and the threshold. If there are more than 1000000 thresholds, the elements are downsampled to 1000 items.
- Parameters
run_name (
str) – the run name to use when saving the filemask (
List[array]) – the maskspred (
List[array]) – the predictionstype (
str) – the type of prcurve to be used in the save name; e.g. test or val
- Return type
None- Returns
None
-
src.pipelines.tensorflow_v2.helpers.utilities.save_predictions(prediction, folder_path, filename)[source]¶ Saves a tf model prediction
- Parameters
prediction (
array) – the tf model predictionfolder_path (
str) – the folder path to save the imagefilename (
str) – the filename to save the image
- Return type
None- Returns
None
-
src.pipelines.tensorflow_v2.helpers.utilities.save_compilation_dict(answers, lr, save_path)[source]¶ Saves the a json of the compilation used for a training run.
- Parameters
answers (
Dict) – a dictionary of answers which contains the user choices for the model, loss, optimiser, filter multiple, loss weight, initialiser, and activationlr (
float) – the learning ratesave_path (
str) – where to save the json
- Return type
None- Returns
None
-
src.pipelines.tensorflow_v2.helpers.utilities.get_sorted_list(search_string)[source]¶ Returns a sorted list of file paths given a search string.
- Parameters
search_string (
str) – the search string to use; this should be a complete path with regex expressions for filenames- Return type
List[str]- Returns
list of file paths
-
src.pipelines.tensorflow_v2.helpers.utilities.configure_for_performance(ds, batch_size=2, buffer_size=100)[source]¶ Shuffles a tf dataset and batches it. It also configures the amount of batches to prefetch which improves performance.
- Parameters
ds (
DatasetV2) – the tf datasetbatch_size (
int) – the batch sizebuffer_size (
int) – the buffer size; this controls the extent that the dataset will be shuffled
- Return type
None- Returns
tf dataset configured for performance
-
src.pipelines.tensorflow_v2.helpers.utilities.tune_model(hyperModel, train_dataset, val_dataset, results_dir, run_name, input_shape, output_channels=1)[source]¶ Performs Bayesian hyperparameter optimization using the input HyperModel.
- Parameters
hyperModel (
HyperModel) – a kerastuner HyperModeltrain_dataset (
DatasetV2) – the training dataset to use in the optimizationval_dataset (
DatasetV2) – the validation set to use in the optimizationresults_dir (
str) – where the results of the optimisation should be storedrun_name (
int) – the run name, which is used when saving the resultsinput_shape (
Tuple[int]) – the input shape of HyperModel inputoutput_channels (
int) – the number of output channels of the HyperModel output
- Return type
None- Returns
None
-
src.pipelines.tensorflow_v2.helpers.utilities.save_lrt_results(lr_range_test, save_path)[source]¶ Saves the result of a learning rate range test as a json.
- Parameters
lr_range_test – a LRRangeTest object
save_path (
str) – where to save the json
- Return type
None- Returns
None
-
src.pipelines.tensorflow_v2.helpers.utilities.get_class_weight(training_path, incl_aug)[source]¶ Gets the % of embolism pixels to use as a class weight in a weighted loss function
- Parameters
training_path (
str) – the path to the training imagesincl_aug (
bool) – whether augmented images should be included
- Return type
float- Returns
the % of embolism pixels
-
src.pipelines.tensorflow_v2.helpers.utilities.global_contrast_normalization(img, s=1, lamb=10, eps=1e-07)[source]¶ Applies global contrast normalisation, which aims to standardise the contrast across pixels in the image.
- Parameters
img (
Tensor) – input images (
int) – scale parameter; the standard deviation across all pixels is equal to slamb (
int) – lambda, which is regularisation parametereps (
float) – episilon, the minimum size of the denomintor; this can be used instead of lambda
- Return type
Tensor- Returns
a normalized image
-
src.pipelines.tensorflow_v2.helpers.utilities.read_file(img_path, shift_256, transform_uint8)[source]¶ Reads in an image from the file path and converts it to a tf tensor.
- Parameters
img_path (
str) – file path of the image to read inshift_256 (
bool) – whether to shift the image by 256transform_uint8 (
bool) – whether to transform the image to a uint8 format
- Return type
Tensor- Returns
an image as tf.float32 tensor
-
src.pipelines.tensorflow_v2.helpers.utilities.parse_numpy_image(img, batch_shape)[source]¶ Converts an np.array to tf.float32 tensor, normalises the image by dividing by 255, and reshapes the image according to the batch shape.
- Parameters
img (
array) – the image to be parsedbatch_shape (
int) – the batch shape
- Return type
Tensor- Returns
a parsed image as a tf.float32 tensor
-
src.pipelines.tensorflow_v2.helpers.utilities.parse_image_fc(leaf_shape, mask_shape, test=False, shift_256=False, transform_uint8=False)[source]¶ A function closure which creates leaf, mask samples. This is used as a preparation step for a tf.Dataset.
- Parameters
leaf_shape (
Tuple[int,int,int]) – the shape of the leafmask_shape (
Tuple[int,int,int]) – the shape of the leaftest (
bool) – whether test set samples are being parsed (currently this is not used)shift_256 (
bool) – whether to shift the image by 256transform_uint8 (
bool) – whether to transform the image to a uint8 format
- Returns
the inner function which expects an leaf path and mask path
-
src.pipelines.tensorflow_v2.helpers.utilities.im2_lt_im1(pred, input_image)[source]¶ Converts all pixels in the prediction to 0 in locations where the input image pixels are positive
- Parameters
pred (
array) – the prediction to processinput_image (
array) – the input image corresponding the prediction
- Return type
array- Returns
a processed prediction
-
src.pipelines.tensorflow_v2.helpers.utilities.gaussian_blur(img, kernel=(5, 5), base=1)[source]¶ Applies a Gaussain blur to an image. Pixels which are greater than zero are set the base pixel provided. This is intended to be used to blur masks and predictions.
- Parameters
img (
array) – the image to blurkernel (
Tuple[int]) – Gaussian kernel, the first element is kernel height and the second element is kernel width; both elements must be positive and oddbase (
int) – the value to set pixels greater than zero to, after blurring
- Return type
array- Returns
a blurred image
-
src.pipelines.tensorflow_v2.helpers.utilities.threshold(img, thresh)[source]¶ Thresholds a prediction, by converting all pixels greater than the threshold to 1 and the remainder to 0.
- Parameters
img (
array) – the image prediction to thresholdthresh (
float) – the threshold
- Return type
array- Returns
a prediction with pixel intensities of 0 or 1
-
src.pipelines.tensorflow_v2.helpers.utilities.check_model_save(model, new_model, new_loss, new_opt, answers, metrics, model_save_path, check_opt=False)[source]¶ Checks if a model have been saved correctly.
- Parameters
model (
Model) – the old model to compare tonew_model (
Model) – the saved modelnew_loss (
Loss) – an instance of tf loss to attach to the new modelnew_opt (
OptimizerV2) – an instance of tf optimiser to attach to the new model; this must be instantiated with the correct learning rateanswers (
Dict) – the answers from the input prompt used in the training runmetrics (
List[Metric]) – the metrics to attach to the modelmodel_save_path (
str) – the save path of the model (where the weights are located)check_opt – whether the optimiser state should be checked
- Return type
None- Returns
None
-
src.pipelines.tensorflow_v2.helpers.utilities.get_model_pred_batch_loss(model, leaf_shape, mask_shape)[source]¶ Gets the prediction and batch loss for an input and mask of zeros.
- Parameters
model (
Model) – the model to make a prediction withleaf_shape (
Tuple[int,int,int]) – the leaf shapemask_shape (
Tuple[int,int,int]) – the leaf shape
- Return type
Tuple[array,float]- Returns
the prediction and the batch loss
-
src.pipelines.tensorflow_v2.helpers.utilities.parse_arguments()[source]¶ Argument parser
- Return type
Namespace- Returns
An argparse namespace