Downloads · 30 days
66
24% of all-time downloads
Dinusharg/segformer_environment_1
segformer_environment_1 is a image segmentation model from Dinusharg. Use it for the image segmentation task on the model card, and read the license before you ship it in a product. It is set up for transformers. The card lists the license as agpl-3.0.
This repository contains a fine-tuned SegFormer model for semantic segmentation of outdoor scenes, specifically targeting:
Downloads · 30 days
66
24% of all-time downloads
All-time downloads
271
Public
Parameters
3.7M
16.4 MB on disk
Likes
0
Public
Click a slice to open those files.
.safetensors14.9 MB · 91%
From the Hugging Face model README
This repository contains a fine-tuned SegFormer model for semantic segmentation of outdoor scenes, specifically targeting:
The model is designed for applications such as:
| ID | Label |
|---|---|
| 0 | background |
| 1 | footpath |
| 2 | grass |
| 3 | road |
| 4 | water |
pip install transformers torch pillow
from transformers import SegformerForSemanticSegmentation, SegformerImageProcessor
from PIL import Image
import torch
import torch.nn.functional as F
import numpy as np
MODEL_ID = "Dinusharg/segformer_environment_1"
IMAGE_PATH = "input_image_path"
OUT_MASK = "segmented_mask.png"
OUT_OVERLAY = "segmented_overlay.png"
palette = {
0: (0, 0, 0), # background
1: (255, 0, 0), # footpath
2: (0, 255, 0), # grass
3: (128, 128, 128), # road
4: (0, 0, 255), # water
}
processor = SegformerImageProcessor.from_pretrained(MODEL_ID)
model = SegformerForSemanticSegmentation.from_pretrained(MODEL_ID)
image = Image.open(IMAGE_PATH).convert("RGB")
inputs = processor(images=image, return_tensors="pt")
with torch.no_grad():
outputs = model(**inputs)
upsampled_logits = F.interpolate(
outputs.logits,
size=image.size[::-1],
mode="bilinear",
align_corners=False
)
pred = upsampled_logits.argmax(dim=1)[0].cpu().numpy()
print("Unique predicted classes:", sorted(set(pred.flatten().tolist())))
print("Labels:", model.config.id2label)
color_mask = np.zeros((pred.shape[0], pred.shape[1], 3), dtype=np.uint8)
for class_id, color in palette.items():
color_mask[pred == class_id] = color
Image.fromarray(color_mask).save(OUT_MASK)
print(f"Saved mask: {OUT_MASK}")
ALPHA = 0.5
image_np = np.array(image).astype(np.float32)
mask_np = color_mask.astype(np.float32)
overlay = (image_np * (1 - ALPHA) + mask_np * ALPHA).clip(0, 255).astype(np.uint8)
Image.fromarray(overlay).save(OUT_OVERLAY)
print(f"Saved overlay: {OUT_OVERLAY}")
This will returns the colored masked and overlay masked images.
Upload an image and visualize segmentation results instantly.
| Input Image | Segmentation Mask | Overlay Output |
|---|---|---|
![]() | ![]() | ![]() |
![]() | ![]() | ![]() |
![]() | ![]() | ![]() |
For questions or collaboration: