Skip to content

XVII. Bioimage.io II (download)

carlosuc3m edited this page Sep 13, 2023 · 2 revisions

JDLL was first developed to load models from the Bioimage.io in the Icy Bioimaging software and although it evolved into a library that can be used to build more general software, it is still tightly integrated and linked to the Bioimage.io.

The thigh integration with the Bioimage.io model repo allows retrieving the models available there and their information and specifications, an easy download of Bioimage.io models, an easy load of Bioimage.io models and methods to retrieve the specifications of the Bioimage.io models.

Download Bioimage.io models

Similarly to how JDLL provides methods to install easily Deep Learning frameworks, it is also equiped with tools to download the models available in the Bioimage.io repo.

Non-static methods

For the following methods, the class BioimageioRepo from the package io.bioimage.modelrunner.bioimageio needs to be instantiated first as the following:

BioimageioRepo br = BioimageioRepo.connect();

This method accesses the Bioimage.io repo information enables the retrieval of information about the models available.

In order to get the information about all the models of the Bioimage.io:

Map<Path, ModelDescriptor> infoMap = br.listAllModels();

where infoMap contains the path to the url of the rdf.yaml file of a model as the key and a ModelDescriptor instance containing all the info in the rdf.yaml file as the value. The map contains all the models available in the Bioimage.io repo.

Once the BioimageRepo has been created, the following methods can be used to download the wanted models.

br.downloadByName(String name, String modelsDirectory)

Installs the model from the Bioimage.io repo by its name as specified in the name field of the rdf.yaml file.

  • name: name of the model that wants to be downloaded. The name has to be exactly the same as the one defined in the name field of the rdf.yaml file
  • modelsDirectory: directory where the model folder will be downloaded

br.downloadModelByID(String id, String modelsDirectory)

Installs the model from the Bioimage.io repo by its model ID as specified in the id field of the rdf.yaml file.

  • id: ID of the model that wants to be downloaded. The model ID has to be exactly the same as the one defined in the id field of the rdf.yaml file
  • modelsDirectory: directory where the model folder will be downloaded

br.downloadByRdfSource(String rdfUrl, String modelsDirectory)

Installs the model from the Bioimage.io repo by th url of the rdf.yaml URL. The rdf.yaml URL is specified in the rdf_source field of the rdf.yaml file.

  • rdfUrl: URL of the rdf.yaml of the model that wants to be downloaded. The URL has to be exactly the same as the one defined in the rdf_source field of the rdf.yaml file
  • modelsDirectory: directory where the model folder will be downloaded

Static methods

There also exist a static method to download models from the Bioimage.io directly.

BioimageioRepo.downloadModel(ModelDescriptor descriptor, String modelsDirectory)

Installs the model from the Bioimage.io repo defined by the information and specs defined in the JDLL object ModelDescriptor. The ModelDescriptor of a Bioimage.io model can be obtained calling the non-static method br.listAllModels().

  • descriptor: ModelDescriptor containing all the info of the model that wants to be downloaded.
  • modelsDirectory: directory where the model folder will be downloaded

Another way to statically retrieve the ModelDescriptor of a model if the url to the rdf.yaml file is known is:

String rdfSource = "https://bioimage-io.github.io/collection-bioimage-io/rdfs/10.5281/zenodo.5874741/5874742/rdf.yaml";
ModelDescriptor descriptor = BioimageioRepo.retreiveDescriptorFromURL(rdfSource);

Tracking the model download progress

Some of the Bioimage.io models contain relatively big files (>500MB) that depending on the Internet connection of the user, might take some minutes to be downloaded, making the corresponding method used take some time to finish.

In order to enable a good user experience and favour comprehensive and intuitive user interafces, all the methods defined above can take another argument, DownloadTracker.TwoParameterConsumer<String, Double> consumer. This argument is a funcitonal interface that tracks the progress of the download of each of the files of the model. Note that it is not provided or if it is equal to ``null`, it will simply be ignored.

A DownloadTracker.TwoParameterConsumer<String, Double> can be created as follows:

DownloadTracker.TwoParameterConsumer<String, Double> consumer = DownloadTracker.createConsumerProgress();

and if it is provided to any of the download model methods as the last argument, it can be used in a separate thread to track the progress of the download.

This object contains a LinkedHashMap<String, Double> where each key is the complete path in the system to the file that is being downloaded and the value is the progress over 1 of the download. When the download is finished, all the values have to be equal to 1.

In order to get the progress LinkedHashMap<String, Double>:

LinkedHashMap<String, Double> progress = consumer.get();

A more detailed example of how to use this object to track downloads can be seen here or here.