Views
No views yet
| ID | Label |
|---|---|
| 0 | background |
| 1 | footpath |
| 2 | grass |
| 3 | road |
| 4 | water |
pip install transformers torch pillow1from transformers import SegformerForSemanticSegmentation, SegformerImageProcessor
2from PIL import Image
3import torch
4import torch.nn.functional as F
5import numpy as np
6
7MODEL_ID = "Dinusharg/segformer_environment_1"
8IMAGE_PATH = "input_image_path"
9
10OUT_MASK = "segmented_mask.png"
11OUT_OVERLAY = "segmented_overlay.png"
12
13palette = {
14 0: (0, 0, 0), # background
15 1: (255, 0, 0), # footpath
16 2: (0, 255, 0), # grass
17 3: (128, 128, 128), # road
18 4: (0, 0, 255), # water
19}
20
21processor = SegformerImageProcessor.from_pretrained(MODEL_ID)
22model = SegformerForSemanticSegmentation.from_pretrained(MODEL_ID)
23
24image = Image.open(IMAGE_PATH).convert("RGB")
25inputs = processor(images=image, return_tensors="pt")
26
27with torch.no_grad():
28 outputs = model(**inputs)
29
30upsampled_logits = F.interpolate(
31 outputs.logits,
32 size=image.size[::-1],
33 mode="bilinear",
34 align_corners=False
35)
36
37pred = upsampled_logits.argmax(dim=1)[0].cpu().numpy()
38
39print("Unique predicted classes:", sorted(set(pred.flatten().tolist())))
40print("Labels:", model.config.id2label)
41
42
43color_mask = np.zeros((pred.shape[0], pred.shape[1], 3), dtype=np.uint8)
44
45for class_id, color in palette.items():
46 color_mask[pred == class_id] = color
47
48Image.fromarray(color_mask).save(OUT_MASK)
49print(f"Saved mask: {OUT_MASK}")
50
51
52ALPHA = 0.5
53
54image_np = np.array(image).astype(np.float32)
55mask_np = color_mask.astype(np.float32)
56
57overlay = (image_np * (1 - ALPHA) + mask_np * ALPHA).clip(0, 255).astype(np.uint8)
58
59Image.fromarray(overlay).save(OUT_OVERLAY)
60print(f"Saved overlay: {OUT_OVERLAY}")| Input Image | Segmentation Mask | Overlay Output |
|---|---|---|
![]() | ![]() | ![]() |
![]() | ![]() | ![]() |
![]() | ![]() | ![]() |