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.
A local file or folder (
~expanded; the entry gets its absolute path):a
.pyfile:script;a folder with
metadata.json:cellmap(a cellmap-models export, which is served from itsmodel.ts);a folder with
rdf.yamlorbioimageio.yaml, such a file itself (*.rdf.yamltoo), or a.zipwith one inside:bioimage;model_checkpoint_<n>, a.tsor amodel.pt:fly, which reads the channels and voxel sizes from the files beside it; what it cannot find is reported inneeds;a folder with
adapter_config.json(a PEFT LoRA adapter), or a full finetune’smodel_state_dict.pt:finetune, which needs itsbase_model;Cellpose-SAM weights (Cellpose 4 saves a torch state dict without an extension; its keys say what it is):
cellpose;an
.onnxfile:onnx. One the browser finetuned carries its manifest, which says its geometry; any other needs its voxel size.
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.A Cellpose model name (
cpsam_v2,cpsam,cpdino,cpdino-vitb):cellpose.A URL: a bioimage.io model page, a Zenodo record, a DOI link, a Hypha artifact or the URL of an
rdf.yamlor 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’sorg/repo/folder.org/repo/folder(@revisionoptional): a folder of a Hugging Face repo of ONNX web exports, as cellmap/onnx-web-models holds them, one model a folder with itsmanifest.json:onnx.org/repo(@revisionoptional; not a DOI, which is 7’s): a Hugging Face repo. Online, its file list is read: withmetadata.jsonandmodel.tsit 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, whichorg/reposerves (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.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.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
Classes
What a reference resolved to. |
Functions
|
The model |
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 inentry().
- how
one line saying what the reference was taken to be.
- needs
constructor arguments the user must still give (a key of
paramsonce given), e.g.voxel_sizefor 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_modeltakes: type, params and name.While
needsis 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
refrefers 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_sizeand to a fly model (and an onnx one without a manifest) asinput_voxel_size; any other model is run at it, as its entry’srun_voxel_size(ModelConfig.run_voxel_size), unless it reads its input itself (a script with its ownprocess_chunk), which ignores it with a note.online – whether a Hugging Face repo or zoo id may be checked online.
- Raises:
ValueError –
refis none of the things checked, or is one that cellmap-flow cannot serve; the message says which and why.