Neuro-SAM: Foundation Models for Dendrite and Dendritic Spine Segmentation
Project description
Neuro-SAM
Foundation Models from Dendrite and Dendritic Spine Segmentation
This project demonstrates an interactive UI to segment dendrites and dendritic spines. The model of choice is SAMv2 and the framework used is pytorch.
๐ Table of Contents
๐ง Overview
Neuro-SAM provides an end-to-end pipeline for analyzing neural structures from 3D microscopy data, featuring:
- Path Tracing: Waypoint-based A* pathfinding
- Dendrite Segmentation: SAM2-based dendrite segmentation
- Smart Spine Detection: Multi-view analysis for spine detection
- Spine Segmentation: Individual spine segmentation using trained SAM2 model
๐ฆ Built With
PyTorch - an open-source machine learning library for Python, widely used for deep learning applications.
Segment Anything Model - a foundation model used for segmentation built by Meta AI.
Weights and Biases - a tool for tracking and visualizing machine learning experiments.
Visual Studio Code - a code editor redefined and optimized for building applications.
FAU High Performance Computing - a high-performance computing cluster at Friedrich-Alexander-Universitรคt Erlangen-Nรผrnberg.
๐ Repository Structure
Neuro-SAM/
โโโ Train-SAMv2/ # SAM2 training infrastructure
โ โโโ sam2/ # SAM2 model implementation
โ โโโ checkpoints/ # Model checkpoints
โ โโโ results/ # Trained model outputs
โ โโโ utils/ # Training utilities
โ โโโ train_dendrites.py # Dendrite model training
โ โโโ train_spines.py # Spine model training
โโโ brightest_path_lib/ # Advanced pathfinding algorithms
โ โโโ algorithm/ # A* and waypoint search implementations
โ โโโ cost/ # Cost function definitions
โ โโโ heuristic/ # Heuristic functions
โ โโโ visualization/ # Path visualization tools
โ โโโ ...
โโโ napari_utils/ # Napari plugin components
โ โโโ main_widget.py # Main interface with anisotropic scaling
โ โโโ path_tracing_module.py # Interactive path tracing
โ โโโ segmentation_module.py # Dendrite segmentation interface
โ โโโ spine_detection_module.py # Spine detection with smart tracking
โ โโโ spine_segmentation_module.py # Individual spine segmentation
โ โโโ visualization_module.py # Path management and visualization
โโโ neuro_sam_plugin.py # Main plugin entry point
๐ Installation
Prerequisites
- Python 3.10+
- CUDA-compatible GPU (recommended)
- Conda/Miniconda
Environment Setup
- Clone the repository:
git clone https://github.com/nipunarora8/Neuro-SAM.git
cd Neuro-SAM
- Create local environment:
conda create -p ./.venv python=3.10 -c conda-forge
conda activate ./.venv
- Install dependencies:
pip install uv
uv sync
- Download SAM2 checkpoints:
cd Train-SAMv2/checkpoints
bash download_ckpts.sh
๐ Usage
Quick Start
from neuro_sam_plugin import run_neuro_sam
# Launch with default spacing (94nm x 94nm x 500nm)
viewer = run_neuro_sam(image_path="your_image.tif")
# Launch with custom voxel spacing
viewer = run_neuro_sam(
image_path="your_image.tif",
spacing_xyz=(100.0, 100.0, 300.0) # X, Y, Z spacing in nm
)
Command Line Interface
# Basic usage
python neuro_sam_plugin.py --image_path /path/to/your/image.tif
# Custom spacing
python neuro_sam_plugin.py --image_path image.tif \
--x-spacing 100.0 --y-spacing 100.0 --z-spacing 300.0
# Load benchmark dataset
python neuro_sam_plugin.py
๐ฌ Workflow
1. Configure Voxel Spacing
Set accurate X, Y, Z voxel spacing in the "Path Tracing" tab for proper anisotropic scaling:
- Typical two-photon: 94nm ร 94nm ร 500nm
- Confocal: varies by objective and zoom
2. Trace Dendritic Paths
- Click waypoints along dendrite structures
- Algorithm automatically finds optimal brightess paths
3. Segment Dendrites
- Load pre-trained SAMv2 dendrite model
- Segment individual path with SAMv2
4. Detect Spines
- Smart multi-view detection using tube data generation
- Angle-based matching between 2D and tubular views
5. Segment Spines
- Fine-grained spine segmentation using specialized SAMv2 model
- Dendrite mask overlay to suppress background signal
- Manual point extension across frames
- Contrasting color system for visualization
๐ง Model Training
Dendrite Model
cd Train-SAMv2
python train_dendrites.py --ppn 20 --pnn 10 --batch_size 32 --model_name "small"
Spine Model
python train_spines.py --model_name "small" --batch_size 16
๐ Data Format
Input Requirements
- Image Format: TIFF, .d3set (to reproduce training results)
- Dimensions: 3D volumes (ZรYรX)
- Bit Depth: 8-bit or 16-bit grayscale
- Size: Tested up to 2048ร2048ร500 voxels
Output Formats
- Paths: NumPy arrays with coordinates
- Masks: Binary TIFF volumes
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
Useful VSCode Extensions
- Remote Explorer - Open projects on remote servers.
- Log Viewer - A log monitoring extension.
- Black Formatter - Python auto code formatter.
- Markdown All in One - Markdown preview and editing.
๐ฌ Contact
Nipun Arora - nipun.arora@fau.de
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 neuro_sam-0.1.10.tar.gz.
File metadata
- Download URL: neuro_sam-0.1.10.tar.gz
- Upload date:
- Size: 193.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b4ab819789d23a2c237cfbd92360275606f684f93a773b322d3ea174bd2d48da
|
|
| MD5 |
58a4e0caf005b041c7179bc1c33f5e1a
|
|
| BLAKE2b-256 |
0cab11c5ab8fde63295cdc309291e1a7b39ec7817fe91f17d63b673d9eb80e72
|
File details
Details for the file neuro_sam-0.1.10-py3-none-any.whl.
File metadata
- Download URL: neuro_sam-0.1.10-py3-none-any.whl
- Upload date:
- Size: 251.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
18e5fd74709d39a0fc5a1cea2aa72f1a3a0903833c6333ba86663b83bb8cbbe2
|
|
| MD5 |
8f8d710310560bf93709dde30578236e
|
|
| BLAKE2b-256 |
4c320d20363ac5e78c171d83c9482a7105feb08a14f1e150fc9bce289b8c61b7
|