A Bachelor Thesis project to allow segmentation of astrocytes on GFAP staining using machine learning in napari.
- Load
.tif/.tiff/.czimicroscopy data via bioio and display them in napari. - Run tiled U-Net inference that produces probability maps and binary masks.
- Curate training patches with ≥300×300 validation and immediate colour feedback.
- Synthesize 1000 augmented patches per curated region and train in the background with progress updates.
- Import/export checkpoints (including ONNX) and optionally bundle curated training data.
Install gfap-segmenter via pip:
pip install gfap-segmenter
If napari (Qt backend included) is not yet installed, use:
pip install "gfap-segmenter[all]"
The plugin automatically uses GPU acceleration when available. GPU support significantly speeds up both model training and inference (typically 10-50x faster than CPU).
The plugin relies on PyTorch. By default, pip install gfap-segmenter installs the CPU-only version of PyTorch. To enable GPU acceleration, you need a CUDA-enabled PyTorch build.
Important: PyTorch bundles its own CUDA runtime, so you don't need to install the full CUDA toolkit separately. You only need NVIDIA GPU drivers (usually pre-installed on systems with NVIDIA GPUs).
For NVIDIA GPUs (CUDA):
Option 1: Fresh installation (recommended)
-
Install PyTorch with CUDA support from PyTorch's official installation guide:
CUDA 11.8:
pip install torch --index-url https://download.pytorch.org/whl/cu118
CUDA 12.4:
pip install torch --index-url https://download.pytorch.org/whl/cu124
CUDA 12.8:
pip install torch --index-url https://download.pytorch.org/whl/cu128
CUDA 13.0:
pip install torch --index-url https://download.pytorch.org/whl/cu130
-
Then install
gfap-segmenter:pip install gfap-segmenter
Option 2: Upgrade existing CPU-only PyTorch installation
If you already have gfap-segmenter installed with CPU-only PyTorch, you can upgrade to CUDA-enabled PyTorch:
-
Uninstall the CPU-only PyTorch:
pip uninstall torch
-
Install CUDA-enabled PyTorch (choose the version matching your driver):
pip install torch --index-url https://download.pytorch.org/whl/cu121
-
The plugin will automatically use the CUDA-enabled PyTorch - no need to reinstall
gfap-segmenter.
Note: You don't need to match the exact CUDA version. PyTorch includes its own CUDA runtime. As long as your NVIDIA driver is compatible (typically driver 11.0+ for CUDA 11.x, or 12.0+ for CUDA 12.x), you can use any recent PyTorch CUDA build. If unsure, use CUDA 12.1.
For Apple Silicon (macOS):
MPS (Metal Performance Shaders) support is automatically available if you have PyTorch 1.12+ installed. No additional steps required - the plugin will automatically use your Apple Silicon GPU.
To verify that GPU support is enabled:
-
Check in the plugin UI: The status bar at the bottom of the plugin widget shows the current device (e.g., "Device: CUDA (NVIDIA GeForce RTX 3090)" or "Device: CPU").
-
Check via Python:
import torch print(f"CUDA available: {torch.cuda.is_available()}") if torch.cuda.is_available(): print(f"CUDA device: {torch.cuda.get_device_name(0)}") print(f"CUDA version: {torch.version.cuda}")
-
Check PyTorch CUDA support (command line):
python -c "import torch; print('CUDA available:', torch.cuda.is_available()); print('CUDA version:', torch.version.cuda if torch.cuda.is_available() else 'N/A')"
Checking your NVIDIA driver version (optional, for reference):
-
Using nvidia-smi (if NVIDIA drivers are installed):
nvidia-smi
Look for the "CUDA Version" line (shows the maximum CUDA version your driver supports).
-
Using nvcc (if CUDA toolkit is installed):
nvcc --version
-
Using PyTorch (after PyTorch is installed):
python -c "import torch; print(torch.version.cuda)"
Problem: Plugin shows "Device: CPU" even though you have a GPU
-
PyTorch CPU-only build: You may have installed the CPU-only version of PyTorch. Uninstall and reinstall with CUDA support:
pip uninstall torch pip install torch --index-url https://download.pytorch.org/whl/cu121 # Adjust CUDA version -
CUDA driver mismatch: Your CUDA driver version may not match the PyTorch CUDA version. Check compatibility:
nvidia-smi # Shows driver version python -c "import torch; print(torch.version.cuda)" # Shows PyTorch CUDA version
The driver version should be >= the PyTorch CUDA version.
-
CUDA not detected: Ensure your NVIDIA drivers are up to date and CUDA is properly installed.
Problem: "CUDA out of memory" errors
- Reduce batch size in training or prediction settings
- Use a smaller patch size
- Close other GPU-intensive applications
Problem: Training/inference is still slow
- Verify GPU is actually being used (check status bar or console output)
- Ensure you installed the correct CUDA version for your GPU
- Check GPU utilization:
nvidia-smishould show high GPU usage during training
- The plugin automatically uses GPU for both training and inference when available - no configuration needed. The current device is displayed at the bottom of the plugin.
- If no GPU is detected, the plugin automatically falls back to CPU execution (slower but functional).
pip install git+https://github.com/MMV-Lab/gfap-segmenter.git
- Prediction tab – load the default checkpoint or import a custom one, choose an image layer, and run inference to create probability and mask layers.
- Curate Patches tab – draw rectangles in the dedicated shapes layer. Patches ≥300×300 are tinted blue; smaller ones are red. Add valid regions to the training set.
- Train tab – launch background training; progress and logs stream into the widget. Trained models are stored under
~/.gfap_segmenter/models. - Model IO tab – export/import checkpoint bundles. Bundles can optionally include the curated training data for reproducibility.
Contributions are very welcome. Tests can be run with tox; please ensure coverage at least stays the same before submitting a pull request.
Distributed under the terms of the BSD-3 license, "gfap-segmenter" is free and open source software.
If you encounter any problems, please file an issue along with a detailed description.