File size: 6,035 Bytes
6428dbe
 
 
 
 
 
7e31b64
6428dbe
 
 
 
 
562f83f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e31b64
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
---
title: Coronary Artery Segmentation
emoji: πŸ«€
colorFrom: red
colorTo: pink
sdk: gradio
sdk_version: 5.49.1
app_file: app.py
pinned: false
license: mit
---

# πŸ«€ Coronary Artery Segmentation with Fine-tuned SAM2

An interactive tool for segmenting coronary arteries in X-ray angiography images using fine-tuned Segment Anything Model 2 (SAM2). This Space provides an easy-to-use interface for medical image segmentation with point-based prompting.

---

## 🎯 How to Use This Space

### Step 1: Upload Your Image
- Click on the image upload area or drag and drop your coronary X-ray angiography image
- Supported formats: JPG, PNG, and other common image formats
- The image will be displayed in the interface for annotation

### Step 2: Select a Model
Choose from four fine-tuned SAM2 variants optimized for coronary artery segmentation:
- **SAM2 Hiera Tiny** - Fastest inference, good for quick results
- **SAM2 Hiera Small** - Balanced speed and accuracy
- **SAM2 Hiera Base Plus** - Higher accuracy, moderate speed
- **SAM2 Hiera Large** - Best accuracy, slower inference

### Step 3: Add Point Prompts

#### 🟒 Positive Points (Green)  
- Click on areas you **want to include** in the segmentation
- Use positive points to mark the coronary arteries you want to segment
- Multiple positive points help the model understand the full extent of the artery
- **Tip**: Add points along the length of the artery for better coverage

#### πŸ”΄ Negative Points (Red)
- Click on areas you **want to exclude** from the segmentation
- Use negative points to refine the segmentation and remove unwanted regions
- Helpful for separating overlapping vessels or removing background
- **Tip**: Add negative points near the artery boundaries to improve precision

#### 🎨 How to Add Points
1. The first click adds a **positive point** (green)
2. Hold **Shift** while clicking to add a **negative point** (red)
3. You can add multiple points of each type
4. Points are displayed as colored dots on your image

### Step 4: Generate Segmentation
- Click the **"Segment"** button to process your annotations
- The model will generate a segmentation mask based on your point prompts
- The result shows the segmented coronary artery highlighted on the original image

### Step 5: Refine (Optional)
- If the segmentation isn't perfect, add more points:
  - Add positive points to include missing artery sections
  - Add negative points to exclude over-segmented regions
- Click **"Segment"** again to update the result
- Repeat until you're satisfied with the segmentation

### Step 6: Clear and Start Over
- Use the **"Clear"** button to remove all points and start fresh
- Upload a new image to segment a different case

---

## πŸ’‘ Tips for Best Results

1. **Start Simple**: Begin with 1-2 positive points on the main artery structure
2. **Be Strategic**: Place positive points at key locations (branches, endpoints, curves)
3. **Refine Gradually**: Add negative points only where the model over-segments
4. **Model Selection**: Start with Tiny or Small models for faster iteration, then use larger models for final results
5. **Multiple Vessels**: For multiple arteries, focus on one at a time with clear positive points
6. **Contrast Matters**: Images with good contrast between vessels and background work best

---

## πŸ”¬ Model Architecture

![Coronary Artery Segmentation Workflow](ICARp_Updated.svg)

The workflow consists of several key stages:
1. **Image Preprocessing** - Normalization and enhancement for optimal model input
2. **Point Prompt Encoding** - Your positive/negative clicks are encoded as spatial prompts
3. **SAM2 Processing** - Fine-tuned encoder-decoder processes the image with prompts
4. **Mask Generation** - High-quality segmentation mask output
5. **Visualization** - Segmented artery overlaid on the original image

---

## πŸ“Š Model Variants

| Model | Parameters | Speed | Accuracy | Best For |
|-------|-----------|-------|----------|----------|
| Hiera Tiny | ~38M | ⚑⚑⚑ | ⭐⭐⭐ | Quick experiments, real-time feedback |
| Hiera Small | ~46M | ⚑⚑ | ⭐⭐⭐⭐ | Balanced performance, general use |
| Hiera Base Plus | ~80M | ⚑ | ⭐⭐⭐⭐⭐ | High-quality results, clinical evaluation |
| Hiera Large | ~224M | ⚑ | ⭐⭐⭐⭐⭐ | Best possible accuracy, research |

All models have been fine-tuned specifically for coronary artery segmentation on X-ray angiography images.

---

## ⚠️ Disclaimer

This tool is intended for **research and educational purposes only**. It is not approved for clinical diagnosis or treatment decisions. Always consult qualified healthcare professionals for medical image interpretation.

---

## πŸš€ Local Installation

If you want to run this application locally, follow these setup instructions:

### 1. Create Conda Environment

```bash
# Create a new conda environment named sam2_FT_env with Python 3.10
conda create -n sam2_FT_env python=3.10.0 -y

# Activate the environment
conda activate sam2_FT_env
```

### 2. Install SAM2 Library

```bash
# Clone the official repository
git clone https://github.com/facebookresearch/segment-anything-2.git
cd segment-anything-2

# Install the package in editable mode
pip install -e .
cd ..
```

### 3. Install Dependencies

```bash
pip install uv
uv pip install gradio opencv-python-headless torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu130
```

### 4. Run the Application

```bash
# Ensure model checkpoints are in the correct directories
python app.py
```

Open `http://127.0.0.1:7860` in your browser.

---

## πŸ“š Citation

If you use this tool in your research, please cite:

```bibtex
@article{ravi2024sam2,
  title={SAM 2: Segment Anything in Images and Videos},
  author={Ravi, Nikhila and others},
  journal={arXiv preprint arXiv:2408.00714},
  year={2024}
}
```

---

## πŸ“§ Contact & Support

For questions, issues, or feedback, please open an issue on the GitHub repository or contact the developers.

**Built with ❀️ using SAM2 and Gradio**