A Bachelor Thesis project to allow Segmentation of Astrocytes on GFAP Staining using Machine Learning in napari
Project description
gfap-segmenter
A Bachelor Thesis project to allow segmentation of astrocytes on GFAP staining using machine learning in napari.
Features
- 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.
Installation
Install gfap-segmenter via pip:
pip install gfap-segmenter
If napari (Qt backend included) is not yet installed, use:
pip install "gfap-segmenter[all]"
GPU support
The plugin automatically uses GPU acceleration when available. GPU support significantly speeds up both model training and inference (typically 10-50x faster than CPU).
Installation
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.
Verification
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)"
Troubleshooting
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
Performance Notes
- 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).
Development version
pip install git+https://github.com/MMV-Lab/gfap-segmenter.git
Usage overview
- 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.
Contributing
Contributions are very welcome. Tests can be run with tox; please ensure coverage at least stays the same before submitting a pull request.
License
Distributed under the terms of the BSD-3 license, "gfap-segmenter" is free and open source software.
Issues
If you encounter any problems, please file an issue along with a detailed description.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file gfap_segmenter-1.0.0.tar.gz.
File metadata
- Download URL: gfap_segmenter-1.0.0.tar.gz
- Upload date:
- Size: 56.2 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
03e1cb9db91abd07a4be3d30722129320ed32b1490248e2508ee5716c94ef496
|
|
| MD5 |
a1285bd8e6d0e95e0787f9ab69c89297
|
|
| BLAKE2b-256 |
9040927c42941b9301356562e721103e8e0038754d27e345711642e6cee783b8
|
Provenance
The following attestation bundles were made for gfap_segmenter-1.0.0.tar.gz:
Publisher:
test_and_deploy.yml on MMV-Lab/gfap_segmenter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
gfap_segmenter-1.0.0.tar.gz -
Subject digest:
03e1cb9db91abd07a4be3d30722129320ed32b1490248e2508ee5716c94ef496 - Sigstore transparency entry: 805227936
- Sigstore integration time:
-
Permalink:
MMV-Lab/gfap_segmenter@ae6553042d6a264a19b7c9b0b851f28ec4cc1c1a -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/MMV-Lab
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
test_and_deploy.yml@ae6553042d6a264a19b7c9b0b851f28ec4cc1c1a -
Trigger Event:
push
-
Statement type:
File details
Details for the file gfap_segmenter-1.0.0-py3-none-any.whl.
File metadata
- Download URL: gfap_segmenter-1.0.0-py3-none-any.whl
- Upload date:
- Size: 35.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fb3d66aa65f0be609f9c0e433d26973c1d69fae46cb8256f8cf39cbacfd486fd
|
|
| MD5 |
cd9b834fdab09c2ce17e9e9daffc0654
|
|
| BLAKE2b-256 |
c9940133c1df2decdccdb318f4c2f379e9227b71165b8c812f25922a6c4ba467
|
Provenance
The following attestation bundles were made for gfap_segmenter-1.0.0-py3-none-any.whl:
Publisher:
test_and_deploy.yml on MMV-Lab/gfap_segmenter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
gfap_segmenter-1.0.0-py3-none-any.whl -
Subject digest:
fb3d66aa65f0be609f9c0e433d26973c1d69fae46cb8256f8cf39cbacfd486fd - Sigstore transparency entry: 805227983
- Sigstore integration time:
-
Permalink:
MMV-Lab/gfap_segmenter@ae6553042d6a264a19b7c9b0b851f28ec4cc1c1a -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/MMV-Lab
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
test_and_deploy.yml@ae6553042d6a264a19b7c9b0b851f28ec4cc1c1a -
Trigger Event:
push
-
Statement type: