NN-based segmentation for
- Residual Blocks: Deep gradient flow. No vanishing.
- Attention Gates: Focus on cell boundaries. Ignore background noise.
- GroupNorm: Stable training on small batches (high-res tiles).
- Hybrid Loss: Dice + BCE. Handle class imbalance (tiny cells vs large background).
- Inference Optimization: FP16 mixed precision + overlapping patch prediction.
.
├── src/ # U-Net, Attention, Residual modules, Tiling, normalization, patching
├── train.py # Training script (Mixed Precision)
├── inference.py # Inference on .btf or .tif images
└── requirements.txt
pip install torch torchvision torchaudio
pip install opencv-python tifffile segmentation-models-pytorchVisium HD provide .btf images. Slice into
# Tile high-res image
from utils import tile_image
tile_image("visium_hd_image.btf", patch_size=256, overlap=32)python train.py --data_dir ./patches --epochs 50 --lr 1e-4 --batch_size 16Predict masks on full slide using sliding window.
python predict.py --input tissue_image.btf --model_path best_model.pth --output mask.tif- Overlapping Patches: Use 32px-64px overlap to fix edge artifacts.
- TTA: Flip/rotate input at test time. Average results.
- Mixed Precision:
scaler = torch.cuda.amp.GradScaler()
with torch.cuda.amp.autocast():
output = model(images)
loss = criterion(output, masks)Masks compatible with SpaceRanger custom segmentation or bin2cell for UMI-to-cell assignment.
MIT