File size: 6,654 Bytes
8f59aab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3953079
 
8f59aab
 
 
c29bac1
 
8f59aab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3953079
 
 
8f59aab
 
 
 
 
 
 
 
 
 
 
 
 
 
3953079
 
 
 
 
 
 
 
 
 
 
 
 
8f59aab
 
 
 
 
 
 
 
3953079
 
 
 
 
 
8f59aab
 
 
 
 
 
 
 
 
 
 
 
 
3953079
 
8f59aab
 
 
3953079
 
 
 
 
 
8f59aab
 
 
 
 
 
 
 
 
3953079
8f59aab
3953079
 
8f59aab
 
 
 
3953079
 
8f59aab
 
3953079
 
 
 
 
 
 
 
 
 
8f59aab
 
 
3953079
8f59aab
 
3953079
 
 
 
 
 
 
 
 
 
 
8f59aab
 
 
3953079
 
 
 
8f59aab
3953079
 
 
 
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
import os
from typing import Tuple
import numpy as np
import gradio as gr
from glob import glob
from functools import partial
from dataclasses import dataclass

import torch
import torch.nn.functional as F
import torchvision.transforms as TF
from transformers import SegformerForSemanticSegmentation

"""
Medical Image Segmentation Web Interface

This module provides a Gradio-based web interface for performing semantic
segmentation on medical images using a pre-trained SegFormer model.

Features:
    - Real-time image segmentation
    - Confidence score visualization
    - Color-coded organ detection (stomach, large bowel, small bowel)
    - Interactive web interface
    - CPU/GPU automatic detection

Author: Medical Image Segmentation Project
License: MIT
"""


@dataclass
class Configs:
    NUM_CLASSES: int = 4  # bao gồm background
    CLASSES: Tuple[str, ...] = ("Ruột già", "Ruột non", "Dạ dày")
    IMAGE_SIZE: Tuple[int, int] = (288, 288)  # W, H
    MEAN: Tuple[float, ...] = (0.485, 0.456, 0.406)
    STD: Tuple[float, ...] = (0.229, 0.224, 0.225)
    # Sử dụng model mới huấn luyện, fallback sang model cũ nếu không tìm thấy
    MODEL_PATH: str = os.path.join(os.getcwd(), "models", "best_model") if os.path.exists(os.path.join(os.getcwd(), "models", "best_model")) else os.path.join(os.getcwd(), "segformer_trained_weights")


def get_model(*, model_path, num_classes):
    """
    Load pre-trained SegFormer model.
    """
    model = SegformerForSemanticSegmentation.from_pretrained(
        model_path, 
        num_labels=num_classes, 
        ignore_mismatched_sizes=True
    )
    return model


@torch.inference_mode()
def predict(input_image, model=None, preprocess_fn=None, device="cpu"):
    """
    Perform semantic segmentation on input medical image.
    """
    if input_image is None:
        return None, []

    shape_H_W = input_image.size[::-1]
    input_tensor = preprocess_fn(input_image)
    input_tensor = input_tensor.unsqueeze(0).to(device)

    # Generate predictions
    outputs = model(pixel_values=input_tensor.to(device), return_dict=True)
    predictions = F.interpolate(outputs["logits"], size=shape_H_W, mode="bilinear", align_corners=False)
    
    # Get predicted class and confidence
    probs = torch.softmax(predictions, dim=1)
    preds_argmax = predictions.argmax(dim=1).cpu().squeeze().numpy()
    confidence_map = probs.max(dim=1)[0].cpu().squeeze().numpy()

    # Create segmentation info with confidence
    seg_info = []
    
    # Classes: 1=Ruột già, 2=Ruột non, 3=Dạ dày
    for idx, class_name in enumerate(Configs.CLASSES, 1):
        mask = preds_argmax == idx
        if mask.sum() > 0:
            # Chỉ tính confidence nếu có pixel được dự đoán
            conf_score = confidence_map[mask].mean()
            label = f"{class_name} ({conf_score:.1%})"
            seg_info.append((mask, label))
        else:
            # Không hiển thị label nếu không phát hiện được
            pass

    return (input_image, seg_info)


if __name__ == "__main__":
    """
    Main application entry point.
    """
    # Mapping màu sắc cho hiển thị
    class2hexcolor = {
        "Dạ dày": "#007fff",      # Xanh dương
        "Ruột non": "#009A17",    # Xanh lá
        "Ruột già": "#FF0000"     # Đỏ
    }

    DEVICE = torch.device("cuda:0") if torch.cuda.is_available() else torch.device("cpu")

    # Load model locally
    try:
        model_dir = Configs.MODEL_PATH
        if not os.path.exists(model_dir):
            print(f"Model path not found: {model_dir}")
            model_dir = "./segformer_trained_weights"
    except Exception as e:
        print(f"Error loading model: {e}")
        model_dir = "./segformer_trained_weights"
    
    # Load model
    print(f"Loading model from: {model_dir}")
    model = get_model(model_path=model_dir, num_classes=Configs.NUM_CLASSES)
    model.to(DEVICE)
    model.eval()
    
    # Warmup
    try:
        _ = model(torch.randn(1, 3, *Configs.IMAGE_SIZE[::-1], device=DEVICE))
    except Exception as e:
        print(f"Warmup warning: {e}")

    preprocess = TF.Compose(
        [
            TF.Resize(size=Configs.IMAGE_SIZE[::-1]), 
            TF.ToTensor(),
            TF.Normalize(Configs.MEAN, Configs.STD, inplace=True),
        ]
    )

    with gr.Blocks(title="Phân Đoạn Ảnh Y Tế") as demo:
        gr.Markdown("""
        <h1><center>🏥 Phân Đoạn Ảnh Y Tế - Tập Dữ Liệu UW-Madison GI Tract</center></h1>
        <p><center>Hệ thống tự động phát hiện và phân đoạn các cơ quan tiêu hóa: Dạ dày, Ruột non, Ruột già.</center></p>
        """)
        
        with gr.Row():
            with gr.Column():
                gr.Markdown("### 📥 Ảnh Đầu Vào")
                img_input = gr.Image(type="pil", height=360, width=360, label="Tải ảnh lên")
            
            with gr.Column():
                gr.Markdown("### 📊 Kết Quả Dự Đoán")
                # AnnotatedImage hiển thị ảnh gốc + các lớp mask
                img_output = gr.AnnotatedImage(
                    label="Kết quả phân đoạn", 
                    height=360, 
                    width=360,
                    color_map=class2hexcolor
                )

        section_btn = gr.Button("🎯 Chạy Phân Đoạn", size="lg", variant="primary")
        section_btn.click(partial(predict, model=model, preprocess_fn=preprocess, device=DEVICE), img_input, img_output)

        gr.Markdown("---")
        gr.Markdown("### 📸 Ảnh Mẫu (Click để thử)")
        
        images_dir = glob(os.path.join(os.getcwd(), "samples") + os.sep + "*.png")
        if len(images_dir) > 0:
            examples = [i for i in np.random.choice(images_dir, size=min(10, len(images_dir)), replace=False)]
            
            gr.Examples(
                examples=examples,
                inputs=img_input,
                outputs=img_output,
                fn=partial(predict, model=model, preprocess_fn=preprocess, device=DEVICE),
                cache_examples=False,
                label="Thư viện ảnh mẫu"
            )
        
        gr.Markdown("""
        ---
        ### 🎨 Chú Thích Màu Sắc
        - 🔵 **Xanh Dương**: Dạ dày
        - 🟢 **Xanh Lá**: Ruột non
        - 🔴 **Đỏ**: Ruột già
        
        ### ℹ️  Thông Tin Hệ Thống
        - **Mô hình**: SegFormer (mit-b0)
        - **Kích thước đầu vào**: 288 × 288 pixels
        - **Framework**: PyTorch + Gradio
        """)

    demo.launch()