biapy.models.bmz_utilsο
This module provides utility functions for managing and preparing BioImage Model Zoo (BMZ) models within the BiaPy framework.
It includes functionalities for:
Retrieving and parsing BMZ model information (get_bmz_model_info).
Creating necessary environment files for model deployment (create_environment_file_for_model).
Generating visual covers for models based on input/output examples (create_model_cover).
Generating comprehensive documentation files for exported models (create_model_doc).
- biapy.models.bmz_utils.download_to_local_path(source, sha256=None, suffix='')[source]ο
Download a file from a given source and return the local path to the downloaded file. The file is saved in a temporary location and will be deleted when the program exits.
- Parameters:
source (str) β The source URL or path to download the file from.
sha256 (str, optional) β The expected SHA256 hash of the file to verify its integrity after downloading. If not provided, no verification is performed.
suffix (str, optional) β The suffix to use for the temporary file (e.g., β.zipβ, β.onnxβ). This can help with identifying the file type and ensuring proper handling.
- Returns:
local_path β The local path to the downloaded file.
- Return type:
str
- biapy.models.bmz_utils.get_bmz_model_info(model: ~bioimageio.spec.model.v0_4.ModelDescr | ~bioimageio.spec.model.v0_5.ModelDescr, spec_version: ~packaging.version.Version = <Version('0.4.0')>) ArchitectureFromLibraryDescr][source]ο
Gather model info depending on its spec version. Currently supports
v0_4andv0_5spec model.- Parameters:
model (ModelDescr) β BMZ model RDF that contains all the information of the model.
spec_version (str) β Version of modelβs specs.
- Returns:
model_instance β Torch model.
- Return type:
Torch model
- biapy.models.bmz_utils.create_environment_file_for_model(building_dir)[source]ο
Create a conda environment file (environment.yaml) with the necessary dependencies to build a model with BiaPy.
- Parameters:
building_dir (str) β Directory to save the environment.yaml file.
- Returns:
env_file β Path to the environment.yaml file created.
- Return type:
str
- biapy.models.bmz_utils.extract_BMZ_sample_and_cover(img: Any, img_gt: Any, patch_size=[256, 256, 1], is_3d=False, input_axis_order: str = 'ZYXC') Tuple[ndarray[tuple[int, ...], dtype[_ScalarType_co]], ndarray[tuple[int, ...], dtype[_ScalarType_co]], ndarray[tuple[int, ...], dtype[_ScalarType_co]]][source]ο
Extract a sample patch from the input image and its corresponding img_gt.
- Parameters:
img (Any) β The input image from which to extract the patch.
img_gt (Any) β The img_gt corresponding to the input image.
patch_size (list, optional) β The size of the patch to extract (default is [256,256,1]).
is_3d (bool, optional) β Whether the input is 3D (default is False).
input_axis_order (str, optional) β The axis order for the input data (default is βZYXCβ).
- Returns:
rimg (NDArray) β The extracted image patch. Shape will be (H, W, C) for 2D or (D, H, W, C) for 3D.
cover_raw (NDArray) β The raw image cover (2D slice). Shape will be (H, W, C).
cover_gt (NDArray) β The ground truth img_gt cover (2D slice). Shape will be (H, W, C).
- biapy.models.bmz_utils.create_model_cover(img, img_gt, out_path, patch_size=256, is_3d=False, workflow='semantic-segmentation')[source]ο
Create a cover based on the files that will be read from
file_pointers.- Parameters:
img (NDArray) β Input image. E.g.
(z, y, x, channels)for 3D or(y, x, channels)for 2D.img_gt (NDArray) β Ground truth image. E.g.
(z, y, x, channels)for 3D or(y, x, channels)for 2D.out_path (str) β Directory to save the cover.
patch_size (int, optional) β Size of the image to create.
is_3d (bool, optional) β Whether if the images to load are 3D or not.
workflow (str, optional) β Workflow to create the cover to. Options are: [
"semantic-segmentation","instance-segmentation","detection","denoising","super-resolution","self-supervised","classification","image-to-image"]
- Returns:
cover_path β Path to the cover.
- Return type:
str
- biapy.models.bmz_utils.create_model_doc(biapy_obj: Any, bmz_cfg: dict, cfg_file: str, task_description: str, doc_output_path: str)[source]ο
Create a documentation file with information of the workflow and model used. It will be saved into
doc_output_path.- Parameters:
biapy_obj (biapy) β BiaPy class instance.
bmz_cfg (dict) β BMZ configuration to export the model. Expected keys are:
- descriptionstr
Description of the model.
- authorslist of dicts
Authors of the model. Need to be a list of dicts. E.g.
[{"name": "Gizmo"}].- model_namestr
Name of the model. If not set a name based on the selected configuration will be created.
- licensestr
A SPDX license identifier. E.g. βCC-BY-4.0β, βMITβ, βBSD-2-Clauseβ.
- tagsList of str
Tags to make models more findable on the website. Only set useful information related to the data the model was trained with, as the BiaPy will introduce the rest of the tags for you, such as dimensions, software (βbiapyβ in this case), workflow used etc. E.g.
['electron-microscopy','mitochondria'].- datadict
- Information of the data used to train the model. Expected keys:
name: Name of the dataset.doi: DOI of the dataset or a reference to find it.image_modality: image modality of the dataset.
cfg_file (str) β Path to the YAML configuration file used.
task_description (str) β Description of the task.
doc_output_path (str) β Output path for the documentation.