TotalSegmentator Documentation

repository·master·Indexed 25 days ago

https://github.com/wasserth/totalsegmentator

A tool for the automated segmentation of major anatomical structures in CT and MR images. It supports a wide range of specialized tasks including whole-body segmentation, vessels, organs, head and neck structures, and spine/vertebrae. The tool provides a CLI and Python API for processing Nifti and DICOM files, offering features for runtime optimization, body statistics, contrast phase prediction, and modality prediction. It also includes a specialized aorta report pipeline for quantitative analysis of the thoracic and abdominal aorta.

Tokens
13.2K
Snippets
32
Records
99
Agent score
84%

What's inside TotalSegmentator

  1. Understand Body Stats Prediction Models

    master

    TotalSegmentator provides two methods for predicting body size, weight, age, and sex from CT or MR images:

    1. CNN Model (Default): A Convolutional Neural Network using 2D EfficientNetV2-S. It uses 5 axial slices sampled along the z-axis as input. It is faster and more accurate than the XGBoost model.
    2. XGBoost Model: A secondary model that uses TotalSegmentator features (organ volumes, median intensities, and tissue types) as inputs for an XGBoost classifier. This is slower and less accurate; it is recommended only as a baseline or if the CNN model fails.

    Note on Tissue Types: The XGBoost model requires the tissue_types model, which is only available with a license. You can obtain a license and apply it using the -l <license_number> flag.

  2. Understand how contrast phase prediction works

    master

    TotalSegmentator predicts the contrast phase of a CT scan using a two-step process:

    1. Structure Segmentation: It first segments a specific set of structures (including liver, pancreas, heart, aorta, etc.).
    2. Feature Extraction & Classification: The median intensity (HU value) of each segmented structure is used as a feature for an XGBoost classifier to predict the post-injection time (pi_time).

    The predicted pi_time is then mapped to one of four contrast phases:

    • native
    • arterial_early
    • arterial_late
    • portal_venous

    Confidence and Reliability:

    • The classifier is an ensemble of 5 models. The output includes the standard deviation of these predictions; a low standard deviation indicates higher confidence (the models agree).
    • The output also provides a probability for each class, which is higher when the predicted pi_time is close to the ideal pi_time for that phase.
  3. Deploy cloud server infrastructure with Terraform

    master

    Use Terraform to initialize, validate, and apply the infrastructure configuration located in the resources directory. To tear down the infrastructure, use the destroy command.

    cd resources
    terraform init
    terraform validate
    terraform apply -auto-approve
    
    # To destroy infrastructure
    terraform destroy -auto-approve
  4. Calculate the Evans index

    master

    The Evans index is calculated using a multi-step pipeline involving TotalSegmentator segmentation and rigid registration to a CT brain atlas. The process follows these steps:

    1. Skull stripping: Uses the TotalSegmentator brain mask.
    2. Rigid registration: The brain is registered to a custom average CT brain atlas to ensure consistent orientation and axis alignment.
    3. Ventricle segmentation: The frontal horn of the ventricles is segmented using TotalSegmentator and registered to the brain atlas.
    4. Post-processing: Small unconnected segmentation blobs are removed.
    5. Brain mask dilation: The brain mask is dilated to fill the entire space inside the skull.
    6. Diameter measurement: The maximum diameter along the x-axis (left-right) is calculated for both the brain and the frontal horn of the ventricles.
    7. Index calculation: The Evans index is computed based on these diameters.
    8. Visualization: The result is plotted on top of the skull and ventricles mask.
  5. Use --roi_subset for speed and memory optimization

    master

    To reduce runtime and memory consumption, use the --roi_subset option. This triggers a two-step process:

    1. A low-resolution model runs quickly to locate the specified Region of Interest (ROI).
    2. The image is cropped to that ROI, and the full-resolution model is run on the smaller volume.

    This is particularly effective for large images (e.g., full-body CT) when you only need specific organs. For example, using --roi_subset left_kidney can result in 5x faster GPU runtime and 32x faster CPU runtime, with 40% lower memory usage.

  6. Evaluate nnU-Net training results

    master

    To evaluate the performance of your trained model against the test set, use the resources/evaluate.py script.

    Prerequisites:

    • pip install git+https://github.com/google-deepmind/surface-distance.git
    • pip install p_tqdm

    Usage: Pass the path to the ground truth labels and the path to your predicted labels as arguments.

    Expected Results: The resulting metrics should be similar to those found in resources/evaluate_results.txt. Because training is non-deterministic, the mean Dice score across all classes may vary by up to one Dice point.

    python resources/evaluate.py path/to/labelsTs path/to/labelsTs_predicted
  7. Install dependencies for Aorta Report

    master

    The totalseg_aorta_report tool requires several system and Python dependencies:

    1. Report Rendering: Requires wkhtmltopdf and a virtual X server. On Ubuntu/Debian:
      sudo apt-get install wkhtmltopdf xvfb
    2. **Contrast-phase detection**: Requires `xgboost`:
       ```bash
    pip install xgboost
    sudo apt-get install wkhtmltopdf xvfb
    pip install xgboost
  8. Manage licenses and weights

    master

    For non-open tasks, you must acquire a license. Use totalseg_set_license to apply it.

    Set License:

    totalseg_set_license -l <license_number>

    Download Weights Manually: If you need to pre-download weights (e.g., for Docker builds):

    totalseg_download_weights -t <task_name>

    Weights are stored in ~/.totalsegmentator/nnunet/results. You can change this location by setting the environment variable TOTALSEG_HOME_DIR.