File size: 7,367 Bytes
8f59aab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# 🏥 Medical Image Segmentation - Complete Guide

## 📋 Tổng Quan Dự Án

Dự án này phân đoạn tự động các cơ quan trong ảnh Y tế của đường tiêu hóa sử dụng **SegFormer** model từ HuggingFace Transformers.

### 🎯 Các Cơ Quan Được Phân Đoạn
- **Dạ dày** (Stomach) - 🔵 Xanh dương
- **Ruột non** (Small bowel) - 🟢 Xanh lá
- **Ruột già** (Large bowel) - 🔴 Đỏ

---

## 🚀 Quick Start

### 1. Chạy Ứng Dụng Demo

```bash
# Vào thư mục dự án
cd UWMGI_Medical_Image_Segmentation

# Chạy ứng dụng web
python app.py
```

Mở trình duyệt: **http://127.0.0.1:7860**

### 2. Sử Dụng Ứng Dụng
1. Upload ảnh Y tế hoặc chọn ảnh mẫu
2. Click nút "Generate Predictions"
3. Xem kết quả phân đoạn với màu sắc

---

## 📚 Hướng Dẫn Training (Huấn Luyện Mô Hình Mới)

### Bước 1: Cài Đặt Dependencies

```bash
pip install -r requirements.txt
pip install kaggle pandas scikit-learn matplotlib
```

### Bước 2: Tải Dataset từ Kaggle

```bash
python download_dataset.py
```

**Yêu cầu**: Kaggle API key (từ https://www.kaggle.com/account)

Nếu không có API key:
```bash
# Tạo thư mục Kaggle
mkdir ~/.kaggle

# Tải kaggle.json từ https://www.kaggle.com/account
# Lưu vào ~/.kaggle/kaggle.json

# Set permissions (Linux/Mac)
chmod 600 ~/.kaggle/kaggle.json
```

### Bước 3: Chuẩn Bị Dataset

```bash
python prepare_dataset.py
```

Script này sẽ:
- ✅ Giải mã RLE encoding thành mask images
- ✅ Chia train/val/test sets (80/10/10)
- ✅ Tạo cấu trúc folder chuẩn

**Kết quả**:
```
prepared_data/
├── train_images/     (80% ảnh)
├── train_masks/      (corresponding masks)
├── val_images/       (10% ảnh)
├── val_masks/
├── test_images/      (10% ảnh)
├── test_masks/
└── split.json        (file metadata)
```

### Bước 4: Train Mô Hình

```bash
python train.py \
    --data ./prepared_data \
    --output-dir ./models \
    --epochs 10 \
    --batch-size 8 \
    --learning-rate 1e-4
```

**Các tham số**:
- `--epochs`: Số lần lặp (mặc định: 10)
- `--batch-size`: Kích thước batch (mặc định: 8)
- `--learning-rate`: Tốc độ học (mặc định: 1e-4)
- `--num-workers`: Workers cho DataLoader (mặc định: 4)

**Kết quả**:
```
models/
├── best_model/            (model tốt nhất trên validation)
├── final_model/           (model sau training)
└── training_history.json  (loss history)
```

---

## 🧪 Testing & Evaluation

### 1. Đánh Giá Trên Test Set

```bash
python test.py \
    --model ./models/best_model \
    --test-images ./prepared_data/test_images \
    --test-masks ./prepared_data/test_masks \
    --output-dir ./test_results
```

**Kết quả Metrics**:
- **mIoU** (mean Intersection over Union): 0.0 - 1.0 (cao hơn tốt hơn)
- **Precision**: Độ chính xác
- **Recall**: Độ nhạy
- **Per-class IoU**: Metrics cho từng cơ quan

### 2. Tạo Visualizations

```bash
python test.py \
    --model ./models/best_model \
    --test-images ./prepared_data/test_images \
    --test-masks ./prepared_data/test_masks \
    --output-dir ./test_results \
    --visualize \
    --num-samples 10
```

Sẽ tạo ra visualizations:
- Original image
- Prediction mask
- Confidence map

---

## 💻 Sử Dụng Mô Hình Tùy Chỉnh

### Thay Thế Mô Hình Mặc Định

```python
# Chỉnh sửa app.py
# Thay đổi dòng này:
model_dir = "./models/best_model"  # Thay vào chỗ Configs.MODEL_PATH hoặc W&B artifact
```

Hoặc tạo script custom:

```python
from transformers import SegformerForSemanticSegmentation
import torch
from PIL import Image

# Load model
model = SegformerForSemanticSegmentation.from_pretrained("./models/best_model")
model.eval()

# Load image
image = Image.open("test.png").convert("RGB")

# Predict (xem app.py's predict function để chi tiết)
```

---

## 📊 Cấu Trúc File

```
UWMGI_Medical_Image_Segmentation/
├── app.py                          # Ứng dụng Gradio chính
├── download_dataset.py             # Script tải dataset từ Kaggle
├── prepare_dataset.py              # Script chuẩn bị dataset
├── train.py                        # Script training
├── test.py                         # Script testing & evaluation
├── requirements.txt                # Dependencies
├── segformer_trained_weights/      # Pre-trained weights
├── samples/                        # Ảnh mẫu

├── data/                           # Raw dataset từ Kaggle
│   ├── train_images/
│   ├── test_images/
│   └── train_masks.csv

├── prepared_data/                  # Processed dataset
│   ├── train_images/
│   ├── train_masks/
│   ├── val_images/
│   ├── val_masks/
│   ├── test_images/
│   ├── test_masks/
│   └── split.json

├── models/                         # Trained models
│   ├── best_model/
│   ├── final_model/
│   └── training_history.json

└── test_results/                   # Evaluation results
    ├── predictions/                # Predicted masks
    ├── visualizations/             # Visualization images
    └── evaluation_results.json
```

---

## 🔧 Troubleshooting

### Lỗi: "Kaggle API not installed"
```bash
pip install kaggle
```

### Lỗi: "Kaggle credentials not found"
Xem hướng dẫn trong phần "Bước 2: Tải Dataset"

### GPU Memory Error
- Giảm batch-size: `--batch-size 4`
- Sử dụng CPU: Model sẽ tự detect CPU nếu GPU không available

### Dataset Quá Lớn
- Giảm số epochs: `--epochs 5`
- Tăng learning-rate: `--learning-rate 5e-4` (cẩn thận)

---

## 📈 Performance Tips

1. **Tăng chất lượng**:
   - Tăng epochs (20-30)
   - Tăng batch size (nếu GPU cho phép)
   - Dùng augmentation (thêm vào prepare_dataset.py)

2. **Tăng tốc độ**:
   - Giảm epochs
   - Dùng mixed precision training
   - Tăng num_workers (4-8)

3. **Tinh chỉnh hyperparameters**:
   - Learning rate: 1e-5 to 5e-4
   - Batch size: 4-32
   - Warmup epochs: 2-3

---

## 📚 Dataset Format

### Input
- **Định dạng**: PNG, JPEG
- **Kích thước**: Tự động resize về 288x288
- **Channels**: RGB (3 channels)

### Output (Mask)
- **Giá trị pixel**:
  - 0 = Background
  - 1 = Large bowel
  - 2 = Small bowel
  - 3 = Stomach

---

## 🤝 Contributions

Muốn cải thiện dự án? Bạn có thể:
- Thêm augmentation techniques
- Cải tiến model architecture
- Thêm support cho các cơ quan khác
- Tối ưu performance

---

## 📞 Support

Nếu gặp vấn đề:
1. Kiểm tra error message
2. Xem phần Troubleshooting
3. Kiểm tra Kaggle/PyTorch documentation

---

## 📝 References

- **SegFormer**: https://huggingface.co/docs/transformers/model_doc/segformer
- **HuggingFace Transformers**: https://huggingface.co/
- **UW-Madison Challenge**: https://www.kaggle.com/competitions/uw-madison-gi-tract-image-segmentation
- **PyTorch**: https://pytorch.org/
- **Gradio**: https://www.gradio.app/

---

**Created**: January 2026
**License**: MIT
**Framework**: PyTorch + HuggingFace Transformers