Skip to main content

Neuro-SAM: Foundation Models for Dendrite and Dendritic Spine Segmentation

Project description

Neuro-SAM

Foundation Models from Dendrite and Dendritic Spine Segmentation

python pytorch wandb

This project demonstrates an interactive UI to segment dendrites and dendritic spines. The model of choice is SAMv2 and the framework used is pytorch.

A stack of neural dendrites and dendritic spines

๐Ÿ“ 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

  1. Clone the repository:
git clone https://github.com/nipunarora8/Neuro-SAM.git
cd Neuro-SAM
  1. Create local environment:
conda create -p ./.venv python=3.10 -c conda-forge
conda activate ./.venv
  1. Install dependencies:
pip install uv
uv sync
  1. 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

๐Ÿ“ฌ Contact

Nipun Arora - nipun.arora@fau.de


Made with โ™ฅ๏ธ at Anki Lab ๐Ÿง โœจ

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

neuro_sam-0.1.7.tar.gz (186.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

neuro_sam-0.1.7-py3-none-any.whl (243.9 kB view details)

Uploaded Python 3

File details

Details for the file neuro_sam-0.1.7.tar.gz.

File metadata

  • Download URL: neuro_sam-0.1.7.tar.gz
  • Upload date:
  • Size: 186.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for neuro_sam-0.1.7.tar.gz
Algorithm Hash digest
SHA256 b5fe4f1166f9fd65ebaab2c2f1f988e30588ea86c48a79425897d77bdf0757ae
MD5 8ec0026b0f3c103afe50e3dd2f6c8647
BLAKE2b-256 860ed141ec133bfefb9620883b7a05e86c1d38cf86c942808a59cf8eeb392ceb

See more details on using hashes here.

File details

Details for the file neuro_sam-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: neuro_sam-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 243.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for neuro_sam-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 5f99870b283ba701422d15a82da281292ab26e5763aac8ed67664887fa297b3a
MD5 e196837e26a176f874f25fac3443af51
BLAKE2b-256 5e34006ecb3aa36a185b6250c4ef78e72eb70d472dca1abf4e7fcf053407888d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page