DeepSpotM¶
tiatoolbox.models.architecture.deepspotm.DeepSpotM
- class DeepSpotM(repo_id_or_path='ratschlab/DeepSpotM', source='scgpt', genes=None, *, device='cpu', revision=None)[source]¶
DeepSpot-M model for virtual spatial transcriptomics from H&E.
Wraps the standalone
deepspotmpackage as a tiatoolbox model. A forward pass maps a batch of 224x224 H&E tiles to predicted spatial gene expression; run it throughtiatoolbox.models.engine.deep_feature_extractor.DeepFeatureExtractorto score a whole-slide image tile by tile.- Parameters:
repo_id_or_path (str) – Hugging Face repo id or a local directory holding the exported model (
config.json,model.safetensors,tokens.csv). Default is"ratschlab/DeepSpotM"(gated).source (str) – Gene-embedding source to activate, one of
DEEPSPOTM_SOURCES. Default is"scgpt".genes (str | Sequence[str] | None) – Restrict the prediction to these gene symbols (order preserved). Only the selected gene queries are computed, which is faster and keeps the output matrix small. Default
Nonepredicts the full panel (~19k genes). Unknown symbols raiseKeyError.device (str) – Device passed to the underlying loader. Default
"cpu". TheDeepFeatureExtractorengine manages device placement at run time, so this mainly matters when the model is used directly.revision (str | None) – Optional Hugging Face revision (tag, branch, or commit).
- model¶
The wrapped
deepspotmmodel.- Type:
- image_processor¶
DeepSpot-M’s evaluation image transform (resize to 224, center-crop, normalize). Applied per tile in
infer_batch().- Type:
Callable
- gene_indices¶
Column indices of the selected genes, or
Nonefor the full panel.- Type:
torch.Tensor | None
Example
>>> import numpy as np >>> model = DeepSpotM(source="scgpt", genes=["EPCAM"]) >>> tiles = np.random.randint(0, 255, (4, 224, 224, 3), dtype=np.uint8) >>> features = DeepSpotM.infer_batch(model, tiles, device="cpu")[0] >>> features.shape (4, 1)
Initialize
DeepSpotM.Methods
Predict gene expression for a batch of preprocessed tiles.
Run inference on a batch of raw H&E tiles.
Attributes
training- forward(imgs)[source]¶
Predict gene expression for a batch of preprocessed tiles.
- Parameters:
imgs (torch.Tensor) – Preprocessed tiles, shape
(N, 3, 224, 224).self (DeepSpotM)
- Returns:
Predicted expression, shape
(N, len(gene_names)).- Return type:
- static infer_batch(model, batch_data, device='cpu')[source]¶
Run inference on a batch of raw H&E tiles.
Applies DeepSpot-M’s own image transform to each tile before predicting, and returns a single-element list so the
DeepFeatureExtractorengine stores the predictions as an(n_tiles, n_genes)matrix (mirroring the feature-extractor contract).- Parameters:
batch_data (np.ndarray | torch.Tensor) – A batch of tiles in NHWC (channel-last) RGB layout, as produced by the tiatoolbox WSI patch loader.
device (str) – Device to run inference on. Default
"cpu".
- Returns:
Single-element list holding the
(n_tiles, n_genes)expression array.- Return type:
list[np.ndarray]