Add New Models#
The model wrapping class spora_bench.wrapper.SporaModelWrapper defines the
methods expected by the pipelines:
Method |
Benchmarks requiring an implementation |
|---|---|
|
In-cohort cell phenotyping via linear probing |
|
Tissue-level tasks |
|
Tissue-level tasks (optional) |
|
MPX-to-MPX virtual staining |
|
Cross-cohort cell instance segmentation |
|
Cross-cohort cell-type annotation |
|
H&E-to-MPX virtual staining |
|
H&E-to-MPX virtual staining (optional; override for cross-tile batched inference) |
The API Reference documents the expected arguments and return types.
To implement a new model, create a class that inherits from SporaModelWrapper.
The custom class must:
implement all interface methods required by the benchmarks you intend to run;
define an
__init__constructor thatfully initializes the wrapped model,
calls
super().__init__(model_name=<model-name>), andaccepts keyword arguments only; positional arguments are not supported.
See models/virtues/virtues_model.py, models/kronos/kronos_model.py, and
models/resnet/resnet.py for examples.
import torch
from spora_bench.wrapper import SporaModelWrapper
class MyModelWrapper(SporaModelWrapper):
def __init__(self, *, model_name: str, checkpoint_path: str):
super().__init__(model_name=model_name)
self.model = load_my_model(checkpoint_path) # your own loader
def compute_cell_tokens(self, dataset, tissue_id: str) -> torch.Tensor:
tissue = dataset.get_tissue(tissue_id)
... # return an (N_cells, D) tensor
Model Config#
Once the wrapper is implemented, write a model config. It is used to instantiate
the model with hydra.utils.instantiate and must therefore follow this format:
model:
_target_: path.to.model.MyModelClass
model_name: my_model
keywordarg1: <first-keyword-argument>
keywordarg2: <second-keyword-argument>
# ...
model_name also names the output directory output_dir/<model_name>/.