File size: 6,047 Bytes
9c72c46
 
 
 
 
 
 
 
 
 
 
e8bdd8d
9c72c46
35f8ab5
9c72c46
 
 
35f8ab5
9c72c46
 
 
 
 
 
 
 
 
35f8ab5
9c72c46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35f8ab5
9c72c46
 
 
 
 
 
 
35f8ab5
9c72c46
 
0bd8e51
 
9c72c46
 
0bd8e51
9c72c46
0bd8e51
9c72c46
 
 
 
 
61a66ca
9c72c46
35f8ab5
9c72c46
 
 
61a66ca
9c72c46
 
 
 
35f8ab5
9c72c46
35f8ab5
9c72c46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35f8ab5
9c72c46
 
 
 
 
 
 
 
 
e8bdd8d
 
 
 
 
9c72c46
35f8ab5
9c72c46
 
 
 
 
 
 
 
6c7c201
 
 
 
 
63ea188
 
 
 
6c7c201
 
7376a61
 
 
6c7c201
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c72c46
 
 
 
 
 
 
 
 
 
d4ed7da
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
import gradio as gr
import torch
import torch.nn.functional as F
import numpy as np
from PIL import Image
from torchvision import transforms
from transformers import SegformerForSemanticSegmentation
from huggingface_hub import hf_hub_download
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import io
import os

# config
IMG_SIZE   = (512, 512)       # your cfg.img_size
DEVICE     = "cuda" if torch.cuda.is_available() else "cpu"

# label mapping
id_to_label = {
    0:"road",         1:"sidewalk",    2:"building",      3:"wall",
    4:"fence",        5:"pole",        6:"traffic light", 7:"traffic sign",
    8:"vegetation",   9:"terrain",    10:"sky",           11:"person",
    12:"rider",      13:"car",        14:"truck",         15:"bus",
    16:"train",      17:"motorcycle", 18:"bicycle",
}
label_to_id = {v: k for k, v in id_to_label.items()}


PALETTE = [
    (128, 64,128), (244, 35,232), ( 70, 70, 70), (102,102,156),
    (190,153,153), (153,153,153), (250,170, 30), (220,220,  0),
    (107,142, 35), (152,251,152), ( 70,130,180), (220, 20, 60),
    (255,  0,  0), (  0,  0,142), (  0,  0, 70), (  0, 60,100),
    (  0, 80,100), (  0,  0,230), (119, 11, 32),
]

mean = [0.485, 0.456, 0.406]
std  = [0.229, 0.224, 0.225]

preprocess = transforms.Compose([
    transforms.Resize(IMG_SIZE),          # same as val_transforms
    transforms.ToTensor(),
    transforms.Normalize(mean=mean, std=std),
])


def mask_to_colour(mask: np.ndarray) -> np.ndarray:
    colour = np.zeros((*mask.shape, 3), dtype=np.uint8)
    for cls_id, rgb in enumerate(PALETTE):
        colour[mask == cls_id] = rgb
    colour[mask == 255] = (0, 0, 0)
    return colour


def load_model():
    model = SegformerForSemanticSegmentation.from_pretrained(
        "nvidia/mit-b2",
        num_labels=19,
        id2label=id_to_label,
        label2id=label_to_id,
        ignore_mismatched_sizes=True,
    )
    ckpt = torch.load("best.pth", map_location=DEVICE)
    state_dict = ckpt["state_dict"]
    if any(k.startswith("_orig_mod.") for k in state_dict.keys()):
        state_dict = {k.replace("_orig_mod.", ""): v for k, v in state_dict.items()}
    model.load_state_dict(state_dict)
    return model.to(DEVICE).eval()
model = load_model()


def run_inference(pil_image: Image.Image):
    img_tensor = preprocess(pil_image.convert("RGB")).unsqueeze(0).to(DEVICE)

    with torch.no_grad(), torch.amp.autocast(DEVICE):
        out    = model(pixel_values=img_tensor)
        logits = F.interpolate(out.logits, size=IMG_SIZE,
                               mode="bilinear", align_corners=False)

    pred        = logits.argmax(dim=1).squeeze(0).cpu().numpy()
    mask_colour = Image.fromarray(mask_to_colour(pred))
    return mask_colour  

def make_legend() -> Image.Image:
    patches = [
        mpatches.Patch(color=[c/255 for c in PALETTE[i]],
                       label=id_to_label[i])
        for i in range(19)
    ]
    fig, ax = plt.subplots(figsize=(14, 1.2))
    ax.axis("off")
    ax.legend(handles=patches, loc="center", ncol=10, fontsize=8, frameon=False)
    buf = io.BytesIO()
    plt.savefig(buf, format="png", dpi=120, bbox_inches="tight")
    plt.close()
    buf.seek(0)
    return Image.open(buf).copy()

legend_img = make_legend()


DATASET_RESULTS = [
    ("result/idx_001.png",  "Val #001"),
    ("result/idx_003.png",  "Val #003"),
    ("result/idx_120.png",  "Val #120"),
    ("result/idx_123.png",  "Val #123"),
    ("result/idx_300.png",  "Val #300")
]

EXAMPLES = [
    [os.path.join(os.path.dirname(__file__), "example/example_1.jpg")],
    [os.path.join(os.path.dirname(__file__), "example/example_2.jpg")],
    [os.path.join(os.path.dirname(__file__), "example/example_3.png")],
    [os.path.join(os.path.dirname(__file__), "example/example_4.jpeg")],
    [os.path.join(os.path.dirname(__file__), "example/example_5.jpg")],
]
# Gradio UI 
with gr.Blocks(title="Cityscapes Segmentation") as demo:

    gr.Markdown(
        "# πŸ™οΈ Cityscapes Semantic Segmentation\n"
        "**Model:** SegFormer-B2 Β· **Training:** 80 epochs Β· "
        "**Backbone LR:** 6e-5 Β· **Loss:** 0.7 CE + 0.3 Dice"
    )

    with gr.Tab("Try it yourself"):

        # ── Row 1: Upload left, Output right ───────────────────────
        with gr.Row():
            with gr.Column(scale=1):
                input_img = gr.Image(type="pil", label="Upload a street image",
                                    height=512,
                                    width=512)
                run_btn   = gr.Button("Run β–Ά", variant="primary")
            with gr.Column(scale=1):
                out_mask = gr.Image(label="Prediction mask",
                    interactive=False,
                    height=512,
                    width=512)

        # ── Row 2: Examples under the button ───────────────────────
        with gr.Row():
            gr.Examples(
                examples=EXAMPLES,
                inputs=input_img,
                outputs=[out_mask],
                fn=run_inference,
                cache_examples=False,
            )

        gr.Image(value=legend_img, label="Colour legend β€” 19 Cityscapes classes",
                interactive=False)

        run_btn.click(fn=run_inference,
                    inputs=input_img,
                    outputs=[out_mask])

    # ── Tab 2: Dataset results gallery ────────────────────────────
    with gr.Tab("Dataset results"):
        gr.Markdown(
            "Pre-computed results on the **Cityscapes validation set**.\n"
            "Each row shows: raw image Β· ground truth Β· model prediction."
        )
        for path, caption in DATASET_RESULTS:
            gr.Image(value=path, label=caption, interactive=False)

demo.launch()