loki2.inference.inference_memory
Loki2 Inference Pipeline for Whole Slide Images (WSI) in Memory.
This module provides an in-memory inference pipeline for CellViT models, including functionalities for processing WSIs, cleaning GeoJSON outputs, and handling various output formats.
Patches dataset requirements: - Patch-Size must be 1024, with overlap of 64
Module Contents
- loki2.inference.inference_memory.clean_geojson(input_fp: str, output_fp: str, *, minify: bool = True, gzip_if_ext: bool = True, drop_empty_props: bool = True) None
Clean and optimize a GeoJSON file.
Read a GeoJSON (FeatureCollection or list of Features), remove null/empty geometries, make valid via buffer(0), cast coordinates to int, optionally strip empty properties, and write a minified (and optionally gzipped) output.
Geometry/topology is preserved except for make-valid (buffer(0)), and coordinates are integer-cast exactly as before.
- Parameters:
input_fp – Path to input GeoJSON file.
output_fp – Path to output GeoJSON file.
minify – Whether to minify JSON output. Defaults to True.
gzip_if_ext – Whether to gzip if output has .gz extension. Defaults to True.
drop_empty_props – Whether to drop empty properties. Defaults to True.
- Raises:
ValueError – If GeoJSON format is unexpected.
- class loki2.inference.inference_memory.CellViTInferenceMemory(model_path: pathlib.Path | str, gpu: int, outdir: pathlib.Path | str, classifier_path: pathlib.Path | str = None, binary: bool = False, batch_size: int = 8, patch_size: int = 1024, overlap: int = 64, geojson: bool = False, graph: bool = False, compression: bool = False, geojson_gz: bool = False, enforce_mixed_precision: bool = False)
Bases:
loki2.inference.inference_disk.CellViTInferenceCellViT Inference class for in-memory processing of WSIs.
This class extends CellViTInference to handle WSI processing entirely in memory, which is suitable for smaller WSIs or when disk I/O is a bottleneck. It integrates with Ray for parallel processing of patches.
- Parameters:
model_path – Path to model checkpoint.
gpu – CUDA GPU ID to use.
outdir – Output directory to store results.
classifier_path – Path to a classifier (.pth) to exchange UniSeg classification results with a new scheme. Defaults to None.
binary – If just a binary detection/segmentation should be performed. Cannot be used with classifier. Defaults to False.
batch_size – Batch-size for inference. Defaults to 8.
patch_size – Size of patches to extract. Defaults to 1024.
overlap – Overlap between patches in pixels. Defaults to 64.
geojson – If a geojson export should be performed. Defaults to False.
graph – If a graph export should be performed. Defaults to False.
compression – If a snappy compression should be performed. Defaults to False.
geojson_gz – If a compressed geojson.gz export should be performed with cleaned geometry. Defaults to False.
enforce_mixed_precision – Using PyTorch autocasting with dtype float16 to speed up inference. Can be used to enforce amp inference even for networks trained without amp. Otherwise, the network setting is used. Defaults to False.
- outdir
- geojson_gz
- process_wsi(wsi_path: pathlib.Path | str, wsi_properties: dict = {}, resolution: float = 0.25, apply_prefilter: bool = True, filter_patches: bool = False, **kwargs) None
Process a whole slide image with CellViT.
- Parameters:
wsi_path (Union[Path, str]) – Path to the whole slide image.
wsi_properties (dict, optional) – Optional WSI properties, Allowed keys are ‘slide_mpp’ and ‘magnification’. Defaults to {}.
resolution (float, optional) – Target resolution. Defaults to 0.25.
apply_prefilter (bool, optional) – Prefilter. Defaults to True.
filter_patches (bool, optional) – Filter patches after processing. Defaults to False.