Base Workflow

class biapy.engine.base_workflow.Base_Workflow(cfg, job_identifier, device, args)[source]

Bases: object

Base workflow class. A new workflow should extend this class.

Parameters:
  • cfg (YACS configuration) – Running configuration.

  • Job_identifier (str) – Complete name of the running job.

  • device (Torch device) – Device used.

  • args (argpase class) – Arguments used in BiaPy’s call.

abstract define_metrics()[source]

This function must define the following variables:

self.metricsList of functions

Metrics to be calculated during model’s training and inference.

self.metric_namesList of str

Names of the metrics calculated.

self.lossFunction

Loss function used during training.

abstract metric_calculation(output, targets, metric_logger=None)[source]

Execution of the metrics defined in define_metrics() function.

Parameters:
  • output (Torch Tensor) – Prediction of the model.

  • targets (Torch Tensor) – Ground truth to compare the prediction with.

  • metric_logger (MetricLogger, optional) – Class to be updated with the new metric(s) value(s) calculated.

Returns:

value – Value of the metric for the given prediction.

Return type:

float

prepare_targets(targets, batch)[source]

Location to perform any necessary data transformations to targets before calculating the loss.

Parameters:
  • targets (Torch Tensor) – Ground truth to compare the prediction with.

  • batch (Torch Tensor) – Prediction of the model. Only used in SSL workflow.

Returns:

targets – Resulting targets.

Return type:

Torch tensor

load_train_data()[source]

Load training and validation data.

destroy_train_data()[source]

Delete training variable to release memory.

prepare_train_generators()[source]

Build train and val generators.

bmz_model_call(in_img, is_train=False)[source]

Call Bioimage model zoo model.

Parameters:
  • in_img (Tensor) – Input image to pass through the model.

  • is_train (bool, optional) – Whether if the call is during training or inference.

Returns:

prediction – Image prediction.

Return type:

Tensor

abstract torchvision_model_call(in_img, is_train=False)[source]

Call a regular Pytorch model.

Parameters:
  • in_img (Tensor) – Input image to pass through the model.

  • is_train (bool, optional) – Whether if the call is during training or inference.

Returns:

prediction – Image prediction.

Return type:

Tensor

model_call_func(in_img, to_pytorch=True, is_train=False)[source]

Call a regular Pytorch model.

Parameters:
  • in_img (Tensor) – Input image to pass through the model.

  • to_pytorch (bool, optional) – Whether if the input image needs to be converted into pytorch format or not.

  • is_train (bool, optional) – Whether if the call is during training or inference.

Returns:

prediction – Image prediction.

Return type:

Tensor

prepare_model()[source]

Build the model.

prepare_logging_tool()[source]

Prepare looging tool.

train()[source]

Training phase.

load_test_data()[source]

Load test data.

destroy_test_data()[source]

Delete test variable to release memory.

prepare_test_generators()[source]

Prepare test data generator.

apply_model_activations(pred, training=False)[source]

Function that apply the last activation (if any) to the model’s output.

Parameters:
  • pred (Torch Tensor) – Predictions of the model.

  • training (bool, optional) – To advice the function if this is being applied during training of inference. During training, CE_Sigmoid activations will NOT be applied, as torch.nn.BCEWithLogitsLoss will apply Sigmoid automatically in a way that is more stable numerically (ref).

Returns:

pred – Resulting predictions after applying last activation(s).

Return type:

Torch tensor

test()[source]

Test/Inference step.

process_sample_by_chunks(filenames)[source]

Function to process a sample in the inference phase. A final H5/Zarr file is created in “TZCYX” or “TZYXC” order depending on TEST.BY_CHUNKS.INPUT_IMG_AXES_ORDER (‘T’ is always included).

Parameters:

filenames (List of str) – Filenames fo the samples to process.

process_sample(norm)[source]

Function to process a sample in the inference phase.

Parameters:

norm (List of dicts) – Normalization used during training. Required to denormalize the predictions of the model.

normalize_stats(image_counter)[source]

Normalize statistics.

Parameters:

image_counter (int) – Number of images to average the metrics.

print_stats(image_counter)[source]

Print statistics.

Parameters:

image_counter (int) – Number of images to call normalize_stats.

print_post_processing_stats()[source]

Print post-processing statistics.

abstract after_merge_patches(pred)[source]

Place any code that needs to be done after merging all predicted patches into the original image.

Parameters:

pred (Torch Tensor) – Model prediction.

after_merge_patches_by_chunks_proccess_entire_pred(filename)[source]

Place any code that needs to be done after merging all predicted patches into the original image but in the process made chunk by chunk. This function will operate over the entire predicted image.

Parameters:

filename (List of str) – Filename of the predicted image H5/Zarr.

abstract after_merge_patches_by_chunks_proccess_patch(filename)[source]

Place any code that needs to be done after merging all predicted patches into the original image but in the process made chunk by chunk. This function will operate patch by patch defined by DATA.PATCH_SIZE.

Parameters:

filename (List of str) – Filename of the predicted image H5/Zarr.

abstract after_full_image(pred)[source]

Place here any code that must be executed after generating the prediction by supplying the entire image to the model. To enable this, the model should be convolutional, and the image(s) should be in a 2D format. Using 3D images as direct inputs to the model is not feasible due to their large size.

Parameters:

pred (Torch Tensor) – Model prediction.

after_all_images()[source]

Place here any code that must be done after predicting all images.

biapy.engine.base_workflow.extract_patch_from_dataset(data, cfg, input_queue, extract_info_queue, verbose=False)[source]

Extract patches from data and put them into a queue read by each GPU inference process. This function will be run by a child process created for every test sample.

Parameters:
  • data (Str or Numpy array) – If str it will be consider a path to load a H5/Zarr file. If not, it will be considered as the data to extract patches from.

  • cfg (YACS configuration) – Running configuration.

  • input_queue (Multiprocessing queue) – Queue to put each extracted patch into.

  • extract_info_queue (Multiprocessing queue) – Auxiliary queue to pass information between processes.

  • verbose (bool, optional) – To print useful information for debugging.

biapy.engine.base_workflow.insert_patch_into_dataset(data_filename, data_filename_mask, data_shape, output_queue, extract_info_queue, cfg, dtype_str, dtype, file_type, verbose=False)[source]

Insert predicted patches (in output_queue) in its original position in a H5/Zarr file. Each GPU will create a file containing the part it has processed (as we can not write the same H5/Zarr file ar the same time). Then, the main rank will create the final image. This function will be run by a child process created for every test sample.

Parameters:
  • data_filename (Str or Numpy array) – If str it will be consider a path to load a H5/Zarr file. If not, it will be considered as the data to extract patches from.

  • data_shape (YACS configuration) – Shape of the H5/Zarr file dataset to create.

  • output_queue (Multiprocessing queue) – Queue to get each prediction from.

  • extract_info_queue (Multiprocessing queue) – Auxiliary queue to pass information between processes.

  • cfg (YACS configuration) – Running configuration.

  • dtype_str (str) – Type of the H5/Zarr dataset to create.

  • dtype (Numpy dtype) – Type of the H5/Zarr dataset to create. Only used if a TIF file is created by selected to do so with TEST.BY_CHUNKS.SAVE_OUT_TIF variable.

  • verbose (bool, optional) – To print useful information for debugging.