AEGIS is a toolkit for large-scale whole-slide image processing. This project was developed by Siddhesh Thakur.
[!NOTE] Contributions are welcome! Please report any issues. You may also contribute by opening a pull request.
conda create -n "aegis" python=3.10, and activate it conda activate aegis.git clone https://github.com/siddhesh-thakur/aegis.git && cd aegis.pip install -e ..Additional packages may be required to load some pretrained models. Follow error messages for instructions.
docker build -t aegis .docker run -it --gpus all -v /path/to/your/data:/data aegis /bin/bash
This will mount your local data directory into the container at /data.Already familiar with WSI processing? Perform segmentation, patching, and UNI feature extraction from a directory of WSIs with:
aegis --task all --wsi_dir ./wsis --job_dir ./aegis_processed --patch_encoder uni_v1 --mag 20 --patch_size 256
Feeling cautious?
Run this command to perform all processing steps for a single slide:
aegis --slide_path ./wsis/xxxx.svs --job_dir ./aegis_processed --patch_encoder uni_v1 --mag 20 --patch_size 256
Or follow step-by-step instructions:
Step 1: Tissue Segmentation: Segments tissue vs. background from a dir of WSIs
aegis --task seg --wsi_dir ./wsis --job_dir ./aegis_processed --gpu 0 --segmenter hest
--task seg: Specifies that you want to do tissue segmentation.--wsi_dir ./wsis: Path to dir with your WSIs.--job_dir ./aegis_processed: Output dir for processed results.--gpu 0: Uses GPU with index 0.--segmenter: Segmentation model. Defaults to hest. Switch to grandqc for fast H&E segmentation. Add the option --remove_artifacts for additional artifact clean up../aegis_processed/thumbnails../aegis_processed/contours../aegis_processed/contours_geojson. These can be opened in QuPath for editing/quality control, if necessary.Step 2: Tissue Patching: Extracts patches from segmented tissue regions at a specific magnification.
aegis --task coords --wsi_dir ./wsis --job_dir ./aegis_processed --mag 20 --patch_size 256 --overlap 0
--task coords: Specifies that you want to do patching.--wsi_dir wsis: Path to the dir with your WSIs.--job_dir ./aegis_processed: Output dir for processed results.--mag 20: Extracts patches at 20x magnification.--patch_size 256: Each patch is 256x256 pixels.--overlap 0: Patches overlap by 0 pixels (always an absolute number in pixels, e.g., --overlap 128 for 50% overlap for 256x256 patches../aegis_processed/20x_256px/patches../aegis_processed/20x_256px/visualization.Step 3a: Patch Feature Extraction: Extracts features from tissue patches using a specified encoder
aegis --task feat --wsi_dir ./wsis --job_dir ./aegis_processed --patch_encoder uni_v1 --mag 20 --patch_size 256
--task feat: Specifies that you want to do feature extraction.--wsi_dir wsis: Path to the dir with your WSIs.--job_dir ./aegis_processed: Output dir for processed results.--patch_encoder uni_v1: Uses the UNI patch encoder. See below for list of supported models.--mag 20: Features are extracted from patches at 20x magnification.--patch_size 256: Patches are 256x256 pixels in size../aegis_processed/20x_256px/features_uni_v1. (Shape: (n_patches, feature_dim))AEGIS supports 21 patch encoders, loaded via a patch encoder_factory. Models requiring specific installations will return error messages with additional instructions. Gated models on HuggingFace require access requests.
| Patch Encoder | Embedding Dim | Args | Link |
|---|---|---|---|
| UNI | 1024 | --patch_encoder uni_v1 --patch_size 256 --mag 20 | MahmoodLab/UNI |
| UNI2-h | 1536 | --patch_encoder uni_v2 --patch_size 256 --mag 20 | MahmoodLab/UNI2-h |
| CONCH | 512 | --patch_encoder conch_v1 --patch_size 512 --mag 20 | MahmoodLab/CONCH |
| CONCHv1.5 | 768 | --patch_encoder conch_v15 --patch_size 512 --mag 20 | MahmoodLab/conchv1_5 |
| Virchow | 2560 | --patch_encoder virchow --patch_size 224 --mag 20 | paige-ai/Virchow |
| Virchow2 | 2560 | --patch_encoder virchow2 --patch_size 224 --mag 20 | paige-ai/Virchow2 |
| Phikon | 768 | --patch_encoder phikon --patch_size 224 --mag 20 | owkin/phikon |
| Phikon-v2 | 1024 | --patch_encoder phikon_v2 --patch_size 224 --mag 20 | owkin/phikon-v2 |
| Prov-Gigapath | 1536 | --patch_encoder gigapath --patch_size 256 --mag 20 | prov-gigapath |
| H-Optimus-0 | 1536 | --patch_encoder hoptimus0 --patch_size 224 --mag 20 | bioptimus/H-optimus-0 |
| H-Optimus-1 | 1536 | --patch_encoder hoptimus1 --patch_size 224 --mag 20 | bioptimus/H-optimus-1 |
| MUSK | 1024 | --patch_encoder musk --patch_size 384 --mag 20 | xiangjx/musk |
| Midnight-12k | 3072 | --patch_encoder midnight12k --patch_size 224 --mag 20 | kaiko-ai/midnight |
| Kaiko | 384/768/1024 | --patch_encoder {kaiko-vits8, kaiko-vits16, kaiko-vitb8, kaiko-vitb16, kaiko-vitl14} --patch_size 256 --mag 20 | 1aurent/kaikoai-models-66636c99d8e1e34bc6dcf795 |
| Lunit | 384 | --patch_encoder lunit-vits8 --patch_size 224 --mag 20 | 1aurent/vit_small_patch8_224.lunit_dino |
| Hibou | 1024 | --patch_encoder hibou_l --patch_size 224 --mag 20 | histai/hibou-L |
| CTransPath-CHIEF | 768 | --patch_encoder ctranspath --patch_size 256 --mag 10 | — |
| ResNet50 | 1024 | --patch_encoder resnet50 --patch_size 256 --mag 20 | — |
Step 3b: Slide Feature Extraction: Extracts slide embeddings using a slide encoder. Will also automatically extract the right patch embeddings.
aegis --task feat --wsi_dir ./wsis --job_dir ./aegis_processed --slide_encoder titan --mag 20 --patch_size 512
--task feat: Specifies that you want to do feature extraction.--wsi_dir wsis: Path to the dir containing WSIs.--job_dir ./aegis_processed: Output dir for processed results.--slide_encoder titan: Uses the Titan slide encoder. See below for supported models.--mag 20: Features are extracted from patches at 20x magnification.--patch_size 512: Patches are 512x512 pixels in size../aegis_processed/20x_256px/slide_features_titan. (Shape: (feature_dim))AEGIS supports 5 slide encoders, loaded via a slide-level encoder_factory. Models requiring specific installations will return error messages with additional instructions. Gated models on HuggingFace require access requests.
| Slide Encoder | Patch Encoder | Args | Link |
|---|---|---|---|
| Threads | conch_v15 | --slide_encoder threads --patch_size 512 --mag 20 | (Coming Soon!) |
| Titan | conch_v15 | --slide_encoder titan --patch_size 512 --mag 20 | MahmoodLab/TITAN |
| PRISM | virchow | --slide_encoder prism --patch_size 224 --mag 20 | paige-ai/Prism |
| CHIEF | ctranspath | --slide_encoder chief --patch_size 256 --mag 10 | CHIEF |
| GigaPath | gigapath | --slide_encoder gigapath --patch_size 256 --mag 20 | prov-gigapath |
| Madeleine | conch_v1 | --slide_encoder madeleine --patch_size 256 --mag 10 | MahmoodLab/madeleine |
| Feather | conch_v15 | --slide_encoder feather --patch_size 512 --mag 20 | MahmoodLab/FEATHER |
[!NOTE] If your task includes multiple slides per patient, you can generate patient-level embeddings by: (1) processing each slide independently and taking their average slide embedding (late fusion) or (2) pooling all patches together and processing that as a single "pseudo-slide" (early fusion).
Please see our tutorials for more support.
Q: How do I extract patch embeddings from legacy patch coordinates extracted with CLAM?
aegis --task feat --wsi_dir ..wsis --job_dir legacy_dir --patch_encoder uni_v1 --mag 20 --patch_size 256 --coords_dir extracted_mag20x_patch256_fp/
Q: How do I keep patches corresponding to holes in the tissue?
aegis, this behavior is default. Set --remove_holes to exclude patches on top of holes.Q: I see weird messages when building models using timm. What is happening?
timm==0.9.16 is installed. timm==1.X.X creates issues with most models.Q: How can I use aegis in other repos with minimal work?
aegis is installed using pip install -e .. Then, the aegis command will be available in your path.Q: I am not satisfied with the tissue vs background segmentation. What can I do?
segmenter --grandqc.Q: I want to process a custom list of WSIs. Can I do it? Also, most of my WSIs don't have the micron per pixel (mpp) stored. Can I pass it?
--custom_list_of_wsis argument. Provide a list of WSI names in a CSV (with slide extension, wsi). Optionally, provide the mpp (field mpp)Q: Do I need to install any additional packages to use AEGIS?
pip install timm_ctp). When you load a model using AEGIS, it will tell you what dependencies are missing and how to install them.ⓒ Siddhesh Thakur. This repository is released under the CC-BY-NC-ND 4.0 license and may only be used for non-commercial, academic research purposes with proper attribution. Any commercial use, sale, or other monetization of this repository is prohibited and requires prior approval. By downloading any pretrained encoder, you agree to follow the model's respective license.
The project was built on top of amazing repositories such as Timm, HuggingFace, and open-source contributions from the community. We thank the authors and developers for their contribution.
If you find our work useful in your research or if you use parts of this code, please consider citing our work.
(Citation placeholder)
62 commits
Python
95.6%
Shell
3.7%
AEGIS is a toolkit for large-scale whole-slide image processing. This project was developed by Siddhesh Thakur.
[!NOTE] Contributions are welcome! Please report any issues. You may also contribute by opening a pull request.
conda create -n "aegis" python=3.10, and activate it conda activate aegis.git clone https://github.com/siddhesh-thakur/aegis.git && cd aegis.pip install -e ..Additional packages may be required to load some pretrained models. Follow error messages for instructions.
docker build -t aegis .docker run -it --gpus all -v /path/to/your/data:/data aegis /bin/bash
This will mount your local data directory into the container at /data.Already familiar with WSI processing? Perform segmentation, patching, and UNI feature extraction from a directory of WSIs with:
aegis --task all --wsi_dir ./wsis --job_dir ./aegis_processed --patch_encoder uni_v1 --mag 20 --patch_size 256
Feeling cautious?
Run this command to perform all processing steps for a single slide:
aegis --slide_path ./wsis/xxxx.svs --job_dir ./aegis_processed --patch_encoder uni_v1 --mag 20 --patch_size 256
Or follow step-by-step instructions:
Step 1: Tissue Segmentation: Segments tissue vs. background from a dir of WSIs
aegis --task seg --wsi_dir ./wsis --job_dir ./aegis_processed --gpu 0 --segmenter hest
--task seg: Specifies that you want to do tissue segmentation.--wsi_dir ./wsis: Path to dir with your WSIs.--job_dir ./aegis_processed: Output dir for processed results.--gpu 0: Uses GPU with index 0.--segmenter: Segmentation model. Defaults to hest. Switch to grandqc for fast H&E segmentation. Add the option --remove_artifacts for additional artifact clean up../aegis_processed/thumbnails../aegis_processed/contours../aegis_processed/contours_geojson. These can be opened in QuPath for editing/quality control, if necessary.Step 2: Tissue Patching: Extracts patches from segmented tissue regions at a specific magnification.
aegis --task coords --wsi_dir ./wsis --job_dir ./aegis_processed --mag 20 --patch_size 256 --overlap 0
--task coords: Specifies that you want to do patching.--wsi_dir wsis: Path to the dir with your WSIs.--job_dir ./aegis_processed: Output dir for processed results.--mag 20: Extracts patches at 20x magnification.--patch_size 256: Each patch is 256x256 pixels.--overlap 0: Patches overlap by 0 pixels (always an absolute number in pixels, e.g., --overlap 128 for 50% overlap for 256x256 patches../aegis_processed/20x_256px/patches../aegis_processed/20x_256px/visualization.Step 3a: Patch Feature Extraction: Extracts features from tissue patches using a specified encoder
aegis --task feat --wsi_dir ./wsis --job_dir ./aegis_processed --patch_encoder uni_v1 --mag 20 --patch_size 256
--task feat: Specifies that you want to do feature extraction.--wsi_dir wsis: Path to the dir with your WSIs.--job_dir ./aegis_processed: Output dir for processed results.--patch_encoder uni_v1: Uses the UNI patch encoder. See below for list of supported models.--mag 20: Features are extracted from patches at 20x magnification.--patch_size 256: Patches are 256x256 pixels in size../aegis_processed/20x_256px/features_uni_v1. (Shape: (n_patches, feature_dim))AEGIS supports 21 patch encoders, loaded via a patch encoder_factory. Models requiring specific installations will return error messages with additional instructions. Gated models on HuggingFace require access requests.
| Patch Encoder | Embedding Dim | Args | Link |
|---|---|---|---|
| UNI | 1024 | --patch_encoder uni_v1 --patch_size 256 --mag 20 | MahmoodLab/UNI |
| UNI2-h | 1536 | --patch_encoder uni_v2 --patch_size 256 --mag 20 | MahmoodLab/UNI2-h |
| CONCH | 512 | --patch_encoder conch_v1 --patch_size 512 --mag 20 | MahmoodLab/CONCH |
| CONCHv1.5 | 768 | --patch_encoder conch_v15 --patch_size 512 --mag 20 | MahmoodLab/conchv1_5 |
| Virchow | 2560 | --patch_encoder virchow --patch_size 224 --mag 20 | paige-ai/Virchow |
| Virchow2 | 2560 | --patch_encoder virchow2 --patch_size 224 --mag 20 | paige-ai/Virchow2 |
| Phikon | 768 | --patch_encoder phikon --patch_size 224 --mag 20 | owkin/phikon |
| Phikon-v2 | 1024 | --patch_encoder phikon_v2 --patch_size 224 --mag 20 | owkin/phikon-v2 |
| Prov-Gigapath | 1536 | --patch_encoder gigapath --patch_size 256 --mag 20 | prov-gigapath |
| H-Optimus-0 | 1536 | --patch_encoder hoptimus0 --patch_size 224 --mag 20 | bioptimus/H-optimus-0 |
| H-Optimus-1 | 1536 | --patch_encoder hoptimus1 --patch_size 224 --mag 20 | bioptimus/H-optimus-1 |
| MUSK | 1024 | --patch_encoder musk --patch_size 384 --mag 20 | xiangjx/musk |
| Midnight-12k | 3072 | --patch_encoder midnight12k --patch_size 224 --mag 20 | kaiko-ai/midnight |
| Kaiko | 384/768/1024 | --patch_encoder {kaiko-vits8, kaiko-vits16, kaiko-vitb8, kaiko-vitb16, kaiko-vitl14} --patch_size 256 --mag 20 | 1aurent/kaikoai-models-66636c99d8e1e34bc6dcf795 |
| Lunit | 384 | --patch_encoder lunit-vits8 --patch_size 224 --mag 20 | 1aurent/vit_small_patch8_224.lunit_dino |
| Hibou | 1024 | --patch_encoder hibou_l --patch_size 224 --mag 20 | histai/hibou-L |
| CTransPath-CHIEF | 768 | --patch_encoder ctranspath --patch_size 256 --mag 10 | — |
| ResNet50 | 1024 | --patch_encoder resnet50 --patch_size 256 --mag 20 | — |
Step 3b: Slide Feature Extraction: Extracts slide embeddings using a slide encoder. Will also automatically extract the right patch embeddings.
aegis --task feat --wsi_dir ./wsis --job_dir ./aegis_processed --slide_encoder titan --mag 20 --patch_size 512
--task feat: Specifies that you want to do feature extraction.--wsi_dir wsis: Path to the dir containing WSIs.--job_dir ./aegis_processed: Output dir for processed results.--slide_encoder titan: Uses the Titan slide encoder. See below for supported models.--mag 20: Features are extracted from patches at 20x magnification.--patch_size 512: Patches are 512x512 pixels in size../aegis_processed/20x_256px/slide_features_titan. (Shape: (feature_dim))AEGIS supports 5 slide encoders, loaded via a slide-level encoder_factory. Models requiring specific installations will return error messages with additional instructions. Gated models on HuggingFace require access requests.
| Slide Encoder | Patch Encoder | Args | Link |
|---|---|---|---|
| Threads | conch_v15 | --slide_encoder threads --patch_size 512 --mag 20 | (Coming Soon!) |
| Titan | conch_v15 | --slide_encoder titan --patch_size 512 --mag 20 | MahmoodLab/TITAN |
| PRISM | virchow | --slide_encoder prism --patch_size 224 --mag 20 | paige-ai/Prism |
| CHIEF | ctranspath | --slide_encoder chief --patch_size 256 --mag 10 | CHIEF |
| GigaPath | gigapath | --slide_encoder gigapath --patch_size 256 --mag 20 | prov-gigapath |
| Madeleine | conch_v1 | --slide_encoder madeleine --patch_size 256 --mag 10 | MahmoodLab/madeleine |
| Feather | conch_v15 | --slide_encoder feather --patch_size 512 --mag 20 | MahmoodLab/FEATHER |
[!NOTE] If your task includes multiple slides per patient, you can generate patient-level embeddings by: (1) processing each slide independently and taking their average slide embedding (late fusion) or (2) pooling all patches together and processing that as a single "pseudo-slide" (early fusion).
Please see our tutorials for more support.
Q: How do I extract patch embeddings from legacy patch coordinates extracted with CLAM?
aegis --task feat --wsi_dir ..wsis --job_dir legacy_dir --patch_encoder uni_v1 --mag 20 --patch_size 256 --coords_dir extracted_mag20x_patch256_fp/
Q: How do I keep patches corresponding to holes in the tissue?
aegis, this behavior is default. Set --remove_holes to exclude patches on top of holes.Q: I see weird messages when building models using timm. What is happening?
timm==0.9.16 is installed. timm==1.X.X creates issues with most models.Q: How can I use aegis in other repos with minimal work?
aegis is installed using pip install -e .. Then, the aegis command will be available in your path.Q: I am not satisfied with the tissue vs background segmentation. What can I do?
segmenter --grandqc.Q: I want to process a custom list of WSIs. Can I do it? Also, most of my WSIs don't have the micron per pixel (mpp) stored. Can I pass it?
--custom_list_of_wsis argument. Provide a list of WSI names in a CSV (with slide extension, wsi). Optionally, provide the mpp (field mpp)Q: Do I need to install any additional packages to use AEGIS?
pip install timm_ctp). When you load a model using AEGIS, it will tell you what dependencies are missing and how to install them.ⓒ Siddhesh Thakur. This repository is released under the CC-BY-NC-ND 4.0 license and may only be used for non-commercial, academic research purposes with proper attribution. Any commercial use, sale, or other monetization of this repository is prohibited and requires prior approval. By downloading any pretrained encoder, you agree to follow the model's respective license.
The project was built on top of amazing repositories such as Timm, HuggingFace, and open-source contributions from the community. We thank the authors and developers for their contribution.
If you find our work useful in your research or if you use parts of this code, please consider citing our work.
(Citation placeholder)
62 commits
Python
95.6%
Shell
3.7%