The CT-DEFACE pipeline is a batch processing wrapper that reduces facial re-identification risk in head CT images. It processes DICOM case folders and generates defaced versions while preserving the original DICOM structure.
- Converts DICOM series β NIfTI format
- Runs CTA-DEFACE segmentation model
- Applies facial mask to image volume
- Converts defaced NIfTI β DICOM (preserving headers)
Note: Only pixel data are modified. Original DICOM metadata (patient IDs, UIDs) remain unchanged unless separately anonymized.
| Step | Input | Operation | Output |
|---|---|---|---|
| 1 | DICOM case folder | Convert to NIfTI | Intermediate NIfTI |
| 2 | NIfTI volume | Run CTA-DEFACE model | Defaced NIfTI |
| 3 | Defaced NIfTI | Replace voxel values | Defaced image |
| 4 | Defaced image + original DICOM | Reconstruct series | Defaced DICOM |
Input: Single case folder or directory with multiple case folders (one series per folder)
Output:
- Defaced DICOM series
- Optional defaced NIfTI (for QA/analysis)
- Temporary working files
- Organize DICOM data (one series per case folder)
- Run defacing pipeline
- Perform visual QA
- Apply metadata anonymization
- Upload processed dataset
CT-DEFACE/
β
βββ run_CT-DEFACE.py # nnUNet CPU/GPU inference + mask application
βββ ct_deface_pipeline_multi2.py # Full multi-case batch pipeline (DICOMβNIfTIβDEFACEβDICOM)
βββ ct_deface_pipeline_gpu.py # GPU-only batch pipeline (DICOMβNIfTIβDEFACEβNIfTI)
βββ ct_deface_convert.py # Standalone DICOM β NIfTI converter
βββ π§ Linux:
β βββ setup_ct_deface_cpu.sh # CPU setup (installs dependencies, downloads model)
β βββ run_ct_deface_cpu.sh # Wrapper to force CPU-only inference
β βββ requirements-ct-deface.txt # Dependencies for Linux
β
βββ πͺ Windows:
βββ setup_ct_deface_cpu.ps1 # CPU setup (PowerShell)
βββ download_ct_deface_model.ps1 # Model downloader (Windows only)
βββ run_ct_deface_batch.ps1 # Batch runner (PowerShell)
βββ requirements_ct_deface_windows.txt # Dependencies for Windows
βββ model/Dataset001_DEFACE/ # Pre-trained nnUNet weights (auto-downloaded)
βββ dicom_input/ # Input DICOMs (place your data here)
βββ dicom_output/ # Defaced DICOM output
βββ head_ct_samples/ # Sample test data
βββ nnunet_data/ # nnUNet environment data
- Any CPU-only computer
- RAM 8-16 GB recommended
- Disk space: ~3Γ your DICOM dataset
- Python 3.10-3.12
- Git
- Windows PowerShell or Linux bash
git clone https://github.com/jsfakian/CT-DEFACE.git
cd CT-DEFACEbash setup_ct_deface_cpu.shCreates .venv_ct_deface/ and installs:
- nnUNetv2
- CPU-only PyTorch
- nibabel, SimpleITK, pydicom, tqdm, numpyβ¦
source .venv_ct_deface/bin/activatepython ct_deface_pipeline_multi2.py -i dicom_input -o dicom_outputInput layout:
dicom_input/
βββ case01/
βββ case02/
βββ case03/
Run:
python ct_deface_pipeline_multi2.py -i dicom_input -o dicom_output --nifti-root-out nifti_outputGit is required to download (clone) the project from GitHub. Steps
1. Go to: https://git-scm.com/download/win
2. Download Git for Windows
3. Run the installer
4. During installation:
Keep the default options
Make sure "Git from the command line and also from 3rd-party software" is selected
5. Finish installation
Open PowerShell and run: git --version
You should see something like: git version 2.x.x
- Click the Start button (or press the Windows key)
- Type PowerShell
- Click Windows PowerShell
Open your browser and visit:
Then:
Hover over Downloads
Click Windows
On the Windows downloads page:
Find Python 3.12.x
Click "Download Python 3.12.x"
This downloads a file like:
python-3.12.x-amd64.exe
Double-click the downloaded .exe file.
β Check "Add Python 3.12 to PATH" β Then click "Install Now"
This step is critical.
Wait for installation to complete
Click Close
If prompted about long path support β Allow
Open PowerShell and run:
python --version
You should see:
Python 3.12.x
- Select a directory to clone the repo (e.g., documents)
- Clone the repo
In the Powershell terminal run:
cd C:\Users\<username>\Documents
git clone https://github.com/jsfakian/CT-DEFACE.git
- Create the virtual environment
- Setup CT-DEFACE (installs dependencies)
- Download CT-DEFACE model
In the PowerShell terminal run:
python -m venv .venv_ct_deface
powershell -ExecutionPolicy Bypass -File .\setup_ct_deface_cpu.ps1
powershell -ExecutionPolicy Bypass -File .\download_ct_deface_model.ps1
In the PowerShell terminal run:
mkdir dicom_input
mkdir dicom_output
mkdir work_deface_batch
Then place the dicom images you want to deface in dicom_input
- Open a PowerShell terminal
- Go to the directory of the CT-DEFACE
- Activate the virtual environment of CT-DEFACE
- Run the
ct_deface_pipeline_multi2.pyscript
Open a PowerShell terminal and run:
cd C:\Users\<username>\Documents\<ct deface directory>
Set-ExecutionPolicy -Scope Process Bypass
.\.venv_ct_deface\Scripts\Activate.ps1
python .\ct_deface_pipeline_multi2.py -i .\dicom_input\ -o .\dicom_output\ --nifti-root-out .\nifti_out\
If you see an error like:
File cannot be loaded because running scripts is disabled on this system.
Run this in your PowerShell terminal before executing any .ps1 script:
Set-ExecutionPolicy -Scope Process BypassThis temporarily allows script execution for the current PowerShell session only. It resets automatically when you close the terminal.
Note: Do not use
powershell -ExecutionPolicy Bypass -Fileto activate the virtual environment β that runs in a subprocess and will not activate the venv in your current shell.
- SimpleITK reads series
- Writes:
work_deface_batch/<case>/nifti_in/<SeriesUID>_0000.nii.gz
run_CT-DEFACE.py performs:
- Mask prediction (
*_mask.nii.gz) - Defaced reconstruction (
*_defaced.nii.gz)
(face replaced by safe background intensity)
Pipeline ensures:
- Picks
*_defaced.nii.gz - Verifies it differs pixel-wise from original
- Looks for best-matching SeriesInstanceUID
- Reuses all DICOM metadata:
- PatientID
- StudyInstanceUID
- SeriesInstanceUID
- Orientation
- Slice geometry
- Only
PixelDatais replaced
Output is written to:
dicom_output/<case>/<slice>.dcm
Convert DICOM series to NIfTI format without defacing:
Linux/Windows (same command):
python ct_deface_convert.py dicom2nii -i dicom_input -o nii_inputConvert defaced NIfTI back to DICOM format. Reuses original DICOM metadata:
Command:
python ct_deface_convert.py nii2dicom -n nii_output/mycase_0000.nii.gz -r dicom_input -o dicom_defacedParameters:
-n: Path to defaced NIfTI file-r: Original DICOM directory (for metadata reuse)-o: Output DICOM directory
Output:
- Defaced DICOMs in
dicom_defaced/with:- New SeriesInstanceUID and SOPInstanceUIDs
- Same geometry/spacing as original
- SeriesDescription appended with "CT-DEFACE"
- Original patient info preserved (not anonymized)
While this pipeline is optimized for CPU-only inference, you can enable GPU acceleration for faster processing:
- NVIDIA GPU with CUDA 11.8+ or ROCm support
- CUDA/cuDNN drivers installed
Instead of the CPU setup, activate your venv and install GPU PyTorch:
source .venv_ct_deface/bin/activate
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118For ROCm (AMD GPUs):
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/rocm5.7The run_CT-DEFACE.py script will automatically detect and use available GPUs. No additional configuration needed.
# Linux
python ct_deface_pipeline_multi2.py -i dicom_input -o dicom_output
# Windows (with GPU torch installed)
python .\ct_deface_pipeline_multi2.py -i .\dicom_input\ -o .\dicom_output\- GPU speedup: 5-10Γ faster on typical NVIDIA RTX GPUs
- VRAM requirement: ~4-6 GB for inference
- Backward compatible: CPU fallback automatically if GPU unavailable
- Mixed precision: For even faster inference, nnUNet supports automatic mixed precision (AMP)
# Verify CUDA is available in PyTorch
python -c "import torch; print(torch.cuda.is_available())"
# Check CUDA version
python -c "import torch; print(torch.version.cuda)"
# Check available GPUs
python -c "import torch; print(torch.cuda.device_count())"If GPU is not detected:
- Verify NVIDIA drivers installed:
nvidia-smi - Reinstall PyTorch with correct CUDA version
- Check environment variables (especially on WSL)
python ct_deface_pipeline_multi2.py -i <dicom_root_in> -o <dicom_root_out> [--nifti-root-out PATH] [-w work_deface_batch] [--ct-extra-args ...]
Please cite:
Mahmutoglu et al. (2024), CTA-DEFACE β Deep learning-based CT defacing
European Radiology Experimental





















