File size: 4,712 Bytes
9f785a9
e4a7008
d487db0
9f785a9
d487db0
9f785a9
 
d487db0
9f785a9
 
e4a7008
d487db0
e4a7008
d487db0
e4a7008
 
 
70fca4b
94fee7e
 
d487db0
e4a7008
70fca4b
94fee7e
 
 
 
 
 
 
 
 
 
d487db0
e4a7008
 
 
 
d487db0
e4a7008
 
 
d487db0
e4a7008
 
 
 
 
 
d487db0
e4a7008
 
 
 
 
 
 
 
d487db0
e4a7008
 
 
d487db0
e4a7008
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d487db0
e4a7008
 
 
 
 
 
 
 
 
d487db0
e4a7008
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
---
title: Neuron Quantification using AI  MedCLIPSeg
emoji: 🧠
sdk: gradio
sdk_version: 5.49.1
app_file: app.py
pinned: false
license: mit
---

# Neuron Quantification using AI — MedCLIPSeg variant

Iman Sabir Ezzat, Randa K Ismail, Ayden Chavez, Marisa Zallocchi, PhD, Steven Fernandes, PhD

A variant of the neuron tracer that applies the **MedCLIPSeg** approach
(Koleilat et al., *Probabilistic Vision–Language Adaptation for Data-Efficient
and Generalizable Medical Image Segmentation*, CVPR 2026): a **text prompt**
describes the target ("nerve fibers …") and a frozen **CLIP** backbone produces a
**fiber-probability map** that acts as a *semantic region prior* — telling the
tracer **where** the nerve fibers are.

## What the app does
- **Neurofilament (MIP):** the maximum-intensity projection of the input channel.
- **Tracked skeleton (white on black):** MedCLIP-gated tubular tracing. The
  MedCLIP probability map (WHERE) seeds a **Sato tubeness** ridge filter run on
  the raw neurofilament signal (the thin fiber GEOMETRY); seeds grow along the
  raw ridge across the whole field, so the long radial fibers below the band are
  recovered, and a width gate removes any residual medial-axis mesh. This traces
  clean thin fibers instead of skeletonising the coarse probability blob (which
  produced a "cracked-mud" mesh).
- **Total trace length (µm):** the spacing-aware total length of that skeleton,
  reported above the images. The text prompt and the MedCLIP **semantic gate**
  (region-prior threshold) are editable.

## Few-shot training (included)
A runnable, few-shot version of the MedCLIPSeg approach — **frozen CLIP encoders +
a text-conditioned decoder** — is trained here on **8 animals** (1A, 1B, 2A, 3A,
3B, 4Aa, 4Bb, 5A) and **tested on the unseen C1 and C2b**:

```bash
python train_medclip_fewshot.py     # runs on CPU/MPS; saves weights_medclip_decoder.pt
```

**Result: held-out (C1 + C2b) mean Dice ≈ 0.53** (per image 0.36–0.60; train Dice
= 1.0, i.e. memorised). The segmentation lands on the fiber band and is far
better than zero-shot, but it is **coarse** — CLIP features are 14×14, so thin
fibers are only roughly captured, and the label is IMARIS's approximate
reconstruction. The app loads `weights_medclip_decoder.pt` automatically and uses
the trained model.

## Honest scope
The full authors' model (`reference_medclipseg/`, PVL adapters + probabilistic
attention) needs a **CUDA GPU**; the included few-shot decoder is the part that
runs here. Either way the MedCLIP output is a **segmentation prior** — the
**trustworthy quantification (length, fibers, diameter) comes from the classical
tracer**, and the *length number* does not generalize reliably across animals
(measured earlier). Without a trained decoder the app falls back to zero-shot
CLIP, which is only a coarse prior.

## To get the real, trained MedCLIPSeg
The original authors' code is bundled in **`reference_medclipseg/`** (needs a
**CUDA GPU**). Two steps:

1. **Build the dataset** (no manual masks needed — uses IMARIS's reconstruction
   as the label):
   ```bash
   python prepare_medclip_data.py
   # -> data/Neurofilament/{Train,Val,Test}_Folder/{img,label} + Prompts_Folder/*.xlsx
   ```
2. **Fine-tune** the model on a GPU:
   ```bash
   cd reference_medclipseg
   pip install -r requirements.txt
   python train.py --config-file configs/Neurofilament.yaml
   python test.py  --config-file configs/Neurofilament.yaml   # segmentation + uncertainty
   ```
   (`configs/Neurofilament.yaml` uses a BiomedCLIP backbone; switch `CLIP_MODEL`
   to `clip`/`pubmedclip`/`unimedclip` as desired.)

## Honest limitations (unchanged from the main project)
- The training **label** is IMARIS's *reconstructed* skeleton (from its segment
  coordinates), which is approximate, and IMARIS itself **over-traces** the true
  fibre length (~3–4×). So a trained model would imitate IMARIS, not ground truth.
- Predicting IMARIS's numbers does **not** generalize well across animals
  (measured R² ≈ 0 in earlier experiments). MedCLIPSeg's segmentation may look
  cleaner, but the **length number** remains unreliable on unseen animals.
- For exact IMARIS values, read the IMARIS statistics directly (as in the other
  tools).

## Files
- `app.py` — the Gradio variant (classical trace + MedCLIP maps).
- `medclipseg.py` — runnable text-prompted CLIP segmentation + uncertainty.
- `prepare_medclip_data.py` — builds the MedCLIPSeg dataset from CZIs + IMARIS
  reconstruction masks.
- `reference_medclipseg/` — the original MedCLIPSeg code (for GPU fine-tuning) +
  `configs/Neurofilament.yaml`.
- `processing.py` — the shared image-processing / tracing pipeline.