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.
A prefix, to say outright what a reference is:
hf:org/repo[@revision],bioimageio:<id, nickname, URL or path>,cellpose:<name or path>,fly:<path>,dacapo:<run>@<iteration>,script:<path>. 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.org/repo(@revisionoptional; not a DOI, which is 6’s): a Hugging Face repo. Online, its file list is read: withmetadata.jsonandmodel.tsit is a cellmap-models export,huggingface; anything else is refused with what to do instead. Offline, or when the Hub cannot be reached, it is taken to be one, 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.
Anything else is a ValueError that says what was tried.
Importing this module imports only the standard library. 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'
- 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”), for the types that do not read it from the model: given to Cellpose and bioimage.io models as
voxel_sizeand to a fly model asinput_voxel_size, and ignored, with a note, by the others.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.
- cellmap_flow.models.resolve.bioimage_params(model: str, voxel_size=None, cls=None) Tuple[Dict[str, Any], List[str]]
BioModelConfig’s arguments formodel, and the ones still needed, from its signature.Its model argument is
modelor (before)model_name, and itsvoxel_sizemay be required or not; whichever it is, the entry names it as the constructor does.voxel_sizegoes in when given and taken, and is needed when not given and required.- Raises:
ValueError – the constructor takes neither
modelnormodel_name.