cellmap_flow.models.resolve

A model from whatever reference a user has in hand, without knowing the model types.

resolve(ref) turns a path, a URL, a Hugging Face repo, a zoo nickname or a Cellpose model name into a Resolved: the model type, its constructor arguments, a name, and what the user must still give. Resolved.entry() is the model entry that registry.build_model (and so a YAML’s models:) takes. The dashboard’s POST /api/models/resolve and cellmap_flow add answer with it.

The checks, in this order; the first that matches wins. Each is cheap: a stat, a file name, a regex, or (only when online) one request.

  1. A local file or folder (~ expanded; the entry gets its absolute path):

    • a .py file: script;

    • a folder with metadata.json: cellmap (a cellmap-models export, which is served from its model.ts);

    • a folder with rdf.yaml or bioimageio.yaml, such a file itself (*.rdf.yaml too), or a .zip with one inside: bioimage;

    • model_checkpoint_<n>, a .ts or a model.pt: fly, which reads the channels and voxel sizes from the files beside it; what it cannot find is reported in needs;

    • a folder with adapter_config.json (a PEFT LoRA adapter), or a full finetune’s model_state_dict.pt: finetune, which needs its base_model;

    • Cellpose-SAM weights (Cellpose 4 saves a torch state dict without an extension; its keys say what it is): cellpose;

    • an .onnx file: onnx. One the browser finetuned carries its manifest, which says its geometry; any other needs its voxel size.

  2. A prefix, to say outright what a reference is: hf:org/repo[@revision] (hf:org/repo/folder: that folder’s ONNX web export), bioimageio:<id, nickname, URL or path>, cellpose:<name or path>, fly:<path>, dacapo:<run>@<iteration>, script:<path>, onnx:<path, folder or org/repo/folder>. The type’s own name works as its prefix too (huggingface:, bioimage:). Nothing after a prefix is looked up online.

  3. A Cellpose model name (cpsam_v2, cpsam, cpdino, cpdino-vitb): cellpose.

  4. A URL: a bioimage.io model page, a Zenodo record, a DOI link, a Hypha artifact or the URL of an rdf.yaml or zip: bioimage; huggingface.co/<org>/<repo> (/tree/<revision> too): as 5, and with a folder after the revision (/tree/main/setup_54, or a file in it), as 5’s org/repo/folder.

  5. org/repo/folder (@revision optional): a folder of a Hugging Face repo of ONNX web exports, as cellmap/onnx-web-models holds them, one model a folder with its manifest.json: onnx. org/repo (@revision optional; not a DOI, which is 7’s): a Hugging Face repo. Online, its file list is read: with metadata.json and model.ts it is a cellmap-models export, huggingface; a repo of web exports is refused with its folders, one of which to add; anything else is refused with what to do instead. A model a server published has both: the export at the root, which org/repo serves (only the root’s files are downloaded), and its web export in a folder, org/repo/folder. Offline, or when the Hub cannot be reached, either is taken to be what it looks like, with a note.

  6. A BioImage Model Zoo id or nickname (affable-shark, 10.5281/zenodo.5764892): bioimage. Online, it is looked up in the zoo’s model list; offline, a nickname- or DOI-shaped one is taken to be one, with a note.

  7. A folder of cellmap/onnx-web-models by its name (setup_54), online only: onnx.

Anything else is a ValueError that says what was tried.

Importing this module imports only the standard library (and models.entry, which has no more). Resolving imports the model config classes (numpy and funlib, as the registry does), and the online checks huggingface_hub; nothing imports torch or a model’s framework, and Resolved.entry() is a plain dict.

Attributes

ONLINE_TIMEOUT

ADAPTER_CONFIG

FULL_FINETUNE_WEIGHTS

ONNX_REPO

Classes

Resolved

What a reference resolved to.

Functions

resolve(ref, *[, name, voxel_size, online]) → Resolved

The model ref refers to (see the module docstring for the checks, in order).

Module Contents

cellmap_flow.models.resolve.ONLINE_TIMEOUT = 20
cellmap_flow.models.resolve.ADAPTER_CONFIG = 'adapter_config.json'
cellmap_flow.models.resolve.FULL_FINETUNE_WEIGHTS = 'model_state_dict.pt'
cellmap_flow.models.resolve.ONNX_REPO = 'cellmap/onnx-web-models'
class cellmap_flow.models.resolve.Resolved

What a reference resolved to.

type

the model type’s name (registry.model_types).

params

its constructor arguments, as a YAML entry gives them.

name

the model’s name: the one asked for, else one from the reference.

env

the type’s default environment for this model (default_env), for display; None runs it in cellmap-flow’s own. Not in entry().

how

one line saying what the reference was taken to be.

needs

constructor arguments the user must still give (a key of params once given), e.g. voxel_size for Cellpose.

notes

anything else worth saying: what was assumed, or ignored.

type: str
params: Dict[str, Any]
name: str
env: str | None
how: str
needs: List[str] = []
notes: List[str] = []
entry() → Dict[str, Any]

The model entry registry.build_model takes: type, params and name.

While needs is not empty, building it fails on what is missing.

to_json() → Dict[str, Any]

Every field, as the dashboard’s route answers with them.

cellmap_flow.models.resolve.resolve(ref: str, *, name: str = None, voxel_size=None, online: bool = True) → Resolved

The model ref refers to (see the module docstring for the checks, in order).

Parameters:
  • ref – what the user pasted.

  • name – the model’s name; by default one is made from ref.

  • voxel_size – nm per voxel (one number, one per axis, or “8,8,8”): given to Cellpose and bioimage.io models as voxel_size and to a fly model (and an onnx one without a manifest) as input_voxel_size; any other model is run at it, as its entry’s run_voxel_size (ModelConfig.run_voxel_size), unless it reads its input itself (a script with its own process_chunk), which ignores it with a note.

  • online – whether a Hugging Face repo or zoo id may be checked online.

Raises:

ValueError – ref is none of the things checked, or is one that cellmap-flow cannot serve; the message says which and why.