Tutorial 0: Pretrain STANDS basic extractor¶
STANDS employ the pretrained image extractor (ResNet backbones) and modality fusion block (Transformer bacbones). For a more accurate performance in subsequent tasks, we encourage you to pretrain STANDS on large-scale public spatial transcriptomics datasets. This tutorial will guide you to pretrain STANDS and save the pretrained weights automatically.
Loading package¶
import warnings
warnings.filterwarnings("ignore")
import stands
import numpy as np
import scanpy as sc
Reading ST data¶
We strongly recommend using multiple ST datasets that contain both images and spatial gene expressions to pretrain STANDS. Here, the demo dataset we provide consists of three slices taken from different donors, with spatial coordinates stored in adata.obsm['spatial']
and images in adata.uns['spatial']
.
adata_list = []
path = './HumanBreast/process/'
data_name = ['V05', 'V06', 'V07']
for d in data_name:
read_path = path + d + '.h5ad'
a = sc.read_h5ad(read_path)
adata_list.append(a)
Pretraining the model¶
By directly inputting the data list into STANDS.pretrain
, specifying the patch size and the GPU for pretraining, you can automatically conduct pretraining and save the weights. In this case, the pretraining task is set as unsupervised reconstruction, so there is no need for cell type annotations.
stands.pretrain(adata_list, patch_size=64, GPU='cuda:0')
Begin to pretrain STANDS...
Pretrain STANDS: 100%|██████████| 100/100 [37:29<00:00, 22.49s/it, Loss=0.14]
The pretrained weights for STANDS have been automatically saved at /stands/model/backbone/model.pth!