Marco333 commited on
Commit
3f75149
Β·
verified Β·
1 Parent(s): 27b6597

Add model card

Browse files
Files changed (1) hide show
  1. README.md +176 -0
README.md ADDED
@@ -0,0 +1,176 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ library_name: transformers
3
+ license: apache-2.0
4
+ tags:
5
+ - image-segmentation
6
+ - semantic-segmentation
7
+ - segformer
8
+ - road-scene
9
+ - cityscapes
10
+ - game-assets
11
+ - vision
12
+ pipeline_tag: image-segmentation
13
+ datasets:
14
+ - Chris1/cityscapes_segmentation
15
+ metrics:
16
+ - mean_iou
17
+ model-index:
18
+ - name: segformer-b0-road-scene-7class
19
+ results:
20
+ - task:
21
+ type: image-segmentation
22
+ name: Semantic Segmentation
23
+ dataset:
24
+ type: Chris1/cityscapes_segmentation
25
+ name: Cityscapes
26
+ split: validation
27
+ metrics:
28
+ - type: mean_iou
29
+ value: 84.0
30
+ name: Mean IoU
31
+ ---
32
+
33
+ # SegFormer-B0 β€” Road Scene Segmentation (7 Game-Asset Classes)
34
+
35
+ A **SegFormer-B0** model fine-tuned on [Cityscapes](https://huggingface.co/datasets/Chris1/cityscapes_segmentation) for **7-class semantic segmentation** of road scenes. Unlike standard road-scene models that target autonomous driving with 19+ fine-grained classes, this model uses a purpose-built taxonomy where every class maps directly to a game element β€” road texture, sky backdrop, tree sprites, building silhouettes, etc.
36
+
37
+ | | |
38
+ |---|---|
39
+ | **Architecture** | SegFormer-B0 (Mix Transformer encoder + all-MLP decoder) |
40
+ | **Parameters** | 3.7 M |
41
+ | **Input** | RGB image, any resolution (resized to 512Γ—512) |
42
+ | **Output** | 7-class pixel mask (upsampled to input resolution at inference) |
43
+ | **Best val mIoU** | **β‰₯ 84.0 %** |
44
+ | **Format** | SafeTensors (14.2 MB) |
45
+ | **Training cost** | ~$0.60 (NVIDIA T4, 50 epochs) |
46
+
47
+ ## 7-Class Game Taxonomy
48
+
49
+ | ID | Class | IoU | Game Function |
50
+ |:--:|-------|:---:|---------------|
51
+ | 0 | `road` | 97.5 % | Driving surface β€” grey asphalt texture sampling |
52
+ | 1 | `sidewalk` | 80.3 % | Ground-level non-road surfaces (sidewalk, terrain) |
53
+ | 2 | `building` | 88.1 % | Background vertical structures β€” building silhouettes |
54
+ | 3 | `vegetation` | 89.1 % | Tall greenery β€” tree sprite extraction |
55
+ | 4 | `sky` | 92.2 % | Sky band β€” direct crop for game background |
56
+ | 5 | `vehicle` | 87.8 % | Road obstacles |
57
+ | 6 | `roadside_object` | 52.7 % | Thin vertical roadside elements (poles, signs, people) |
58
+
59
+ ### Key design decisions
60
+
61
+ - **Road ↔ sidewalk kept separate** so road color sampling produces pure grey asphalt without contamination from sidewalk / terrain tones.
62
+ - **Terrain grouped with sidewalk**, not vegetation β€” ground-level grass strips serve the same game function as sidewalk.
63
+ - **All building-like structures merged** (building, wall, fence) β€” the game treats them identically as background geometry.
64
+ - **All vehicle types merged** β€” the game treats every vehicle as a road obstacle.
65
+ - **Roadside objects** (poles, traffic lights, signs, persons, riders) are grouped into a single thin-element class with fallback stock sprites when extraction quality is low.
66
+
67
+ ## Usage
68
+
69
+ ```python
70
+ from transformers import SegformerImageProcessor, SegformerForSemanticSegmentation
71
+ from PIL import Image
72
+ import torch
73
+ import torch.nn.functional as F
74
+
75
+ processor = SegformerImageProcessor.from_pretrained("Marco333/segformer-b0-road-scene-7class")
76
+ model = SegformerForSemanticSegmentation.from_pretrained("Marco333/segformer-b0-road-scene-7class")
77
+
78
+ image = Image.open("road_photo.jpg")
79
+ inputs = processor(images=image, return_tensors="pt")
80
+
81
+ with torch.no_grad():
82
+ outputs = model(**inputs)
83
+
84
+ # Upsample logits to original image size
85
+ mask = F.interpolate(
86
+ outputs.logits,
87
+ size=image.size[::-1], # (H, W)
88
+ mode="bilinear",
89
+ align_corners=False,
90
+ ).argmax(dim=1)[0]
91
+
92
+ # mask values: 0=road, 1=sidewalk, 2=building, 3=vegetation, 4=sky, 5=vehicle, 6=roadside_object
93
+ ```
94
+
95
+ ## Training Details
96
+
97
+ ### Dataset
98
+
99
+ [Chris1/cityscapes_segmentation](https://huggingface.co/datasets/Chris1/cityscapes_segmentation) β€” urban street scenes from 50 European cities.
100
+
101
+ | Split | Images |
102
+ |-------|-------:|
103
+ | Train | 2,975 |
104
+ | Val | 500 |
105
+
106
+ The original Cityscapes masks use **label IDs 0–33** stored as 3-channel RGB images. A custom preprocessing pipeline extracts channel 0 and applies a 256-element lookup table to remap all 34 Cityscapes classes into the 7-class game taxonomy in a single vectorized operation (unmapped classes β†’ 255 = ignore).
107
+
108
+ ### Hyperparameters
109
+
110
+ | Parameter | Value |
111
+ |-----------|-------|
112
+ | Base weights | `nvidia/segformer-b0-finetuned-ade-512-512` (encoder only) |
113
+ | Optimizer | AdamW |
114
+ | Learning rate | 6 Γ— 10⁻⁡ |
115
+ | LR schedule | Polynomial decay |
116
+ | Warmup | 10 % of total steps |
117
+ | Weight decay | 0.01 |
118
+ | Effective batch size | 8 (4 Γ— device Β· 2 grad accum) |
119
+ | Training resolution | 512 Γ— 512 |
120
+ | Precision | FP16 mixed precision |
121
+ | Epochs | 50 |
122
+ | Augmentation | ColorJitter (brightness=0.25, contrast=0.25, saturation=0.25, hue=0.1) β€” train only |
123
+ | Best-model selection | Highest mean IoU on validation set (`load_best_model_at_end=True`) |
124
+ | Hardware | NVIDIA T4 (16 GB VRAM) |
125
+
126
+ ### Training Curve
127
+
128
+ | Epoch | mIoU | Road | Sidewalk | Building | Vegetation | Sky | Vehicle | Roadside Obj |
129
+ |------:|-----:|-----:|---------:|---------:|-----------:|----:|--------:|-------------:|
130
+ | 1 | 59.5 % | 93.2 % | 54.6 % | 74.3 % | 73.7 % | 56.9 % | 63.9 % | 0.0 % |
131
+ | 3 | 73.8 % | 95.4 % | 69.4 % | 82.7 % | 82.5 % | 84.3 % | 79.4 % | 23.1 % |
132
+ | 5 | 80.4 % | 96.7 % | 76.5 % | 86.2 % | 87.0 % | 88.9 % | 84.3 % | 43.2 % |
133
+ | 9 | 82.9 % | 97.4 % | 79.2 % | 87.5 % | 88.3 % | 91.0 % | 86.7 % | 50.4 % |
134
+ | 16 | 84.0 % | 97.5 % | 80.3 % | 88.1 % | 89.1 % | 92.2 % | 87.8 % | 52.7 % |
135
+
136
+ The model converges quickly thanks to transfer learning β€” the pretrained encoder already understands road scene features; only the 7-class decoder head is learned from scratch.
137
+
138
+ ### Implementation Notes
139
+
140
+ Several non-obvious flags are required for correct training:
141
+
142
+ - `ignore_mismatched_sizes=True` β€” the pretrained decoder head has a different number of output classes.
143
+ - `remove_unused_columns=False` β€” prevents the Trainer from dropping image data columns.
144
+ - `label_names=["labels"]` β€” tells the Trainer which key holds the segmentation targets.
145
+ - `do_reduce_labels=False` β€” Cityscapes labels don't need ADE20K-style background subtraction.
146
+ - **Logit upsampling in `compute_metrics`** β€” SegFormer outputs at ΒΌ resolution; logits must be upsampled before comparison with ground-truth masks.
147
+
148
+ ## Intended Use
149
+
150
+ This model is designed for a **game asset extraction pipeline** where a user uploads a road photograph and the runtime transforms it into game elements:
151
+
152
+ 1. **Road shape** β€” fit road boundaries from the road mask; derive perspective and horizon.
153
+ 2. **Color palette** β€” sample dominant colors from each masked region of the original image.
154
+ 3. **Sky** β€” crop the sky band directly using the sky mask.
155
+ 4. **Tree sprites** β€” blob detection on the vegetation mask; crop with alpha transparency.
156
+ 5. **Building silhouettes** β€” extract from the building mask for background geometry.
157
+ 6. **Fallback system** β€” when extraction quality is poor for any element, use palette-matched stock assets.
158
+
159
+ ## Limitations
160
+
161
+ - Trained exclusively on European urban street scenes (Cityscapes). Performance may degrade on rural roads, highways without sidewalks, non-European road styles, or indoor scenes.
162
+ - `roadside_object` class (52.7 % IoU) is the weakest β€” thin elements like poles and signs are inherently difficult at 512Γ—512 resolution. The intended runtime uses fallback sprites for this class.
163
+ - Not suitable for safety-critical autonomous driving β€” the merged taxonomy intentionally discards distinctions (truck vs. car, wall vs. fence) that matter for driving but not for game art.
164
+
165
+ ## Citation
166
+
167
+ If you use this model, please cite the SegFormer paper:
168
+
169
+ ```bibtex
170
+ @inproceedings{xie2021segformer,
171
+ title={SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers},
172
+ author={Xie, Enze and Wang, Wenhai and Yu, Zhiding and Anber, Abualkasim and Lu, Tong and Alvarez, Jose M},
173
+ booktitle={NeurIPS},
174
+ year={2021}
175
+ }
176
+ ```