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
+
- cpu
|
| 11 |
+
datasets:
|
| 12 |
+
- cityscapes
|
| 13 |
+
metrics:
|
| 14 |
+
- type: mIoU
|
| 15 |
+
value: 0.73
|
| 16 |
+
- type: wmIoU
|
| 17 |
+
value: 0.85
|
| 18 |
+
license: mit
|
| 19 |
+
---
|
| 20 |
+
|
| 21 |
+
# Cityscapes Segmentation (8 classes) — SERNet (TorchScript)
|
| 22 |
+
|
| 23 |
+
Modèle de **segmentation sémantique** pour scènes urbaines (Cityscapes), 8 classes :
|
| 24 |
+
`void, flat, construction, object, nature, sky, human, vehicle`.
|
| 25 |
+
|
| 26 |
+
- **Architecture** : SERNet (compatible API DeepLabV3 / torchvision)
|
| 27 |
+
- **Format des poids** : **TorchScript** — `sernet_model.pt`
|
| 28 |
+
- **Métriques (val)** : **mIoU ≈ 73%**, **wmIoU ≈ 85%**
|
| 29 |
+
- **Cible** : CPU (fonctionne aussi sur GPU si déplacé)
|
| 30 |
+
|
| 31 |
+
---
|
| 32 |
+
|
| 33 |
+
## 🧩 Classes (IDs)
|
| 34 |
+
|
| 35 |
+
- 0: void
|
| 36 |
+
- 1: flat
|
| 37 |
+
- 2: construction
|
| 38 |
+
- 3: object
|
| 39 |
+
- 4: nature
|
| 40 |
+
- 5: sky
|
| 41 |
+
- 6: human
|
| 42 |
+
- 7: vehicle
|
| 43 |
+
|
| 44 |
+
---
|
| 45 |
+
|
| 46 |
+
## 🛠️ Preprocessing (torchvision)
|
| 47 |
+
|
| 48 |
+
Le modèle utilise les **transforms officiels** associés aux poids torchvision :
|
| 49 |
+
|
| 50 |
+
```python
|
| 51 |
+
from torchvision.models.segmentation import DeepLabV3_ResNet101_Weights
|
| 52 |
+
|
| 53 |
+
weights = DeepLabV3_ResNet101_Weights.DEFAULT
|
| 54 |
+
preprocess = weights.transforms() # PIL -> Tensor + normalisation ImageNet
|
| 55 |
+
```
|
| 56 |
+
|
| 57 |
+
- **Taille d’entrée** : non imposée. Pour la latence CPU, redimensionner **avant** `preprocess`, p.ex. **(H, W) = (480, 960)**.
|
| 58 |
+
- **Sortie** : logits `(B, 8, H, W)` → `argmax(dim=1)` donne un masque `(H, W)` d’IDs de classe.
|
| 59 |
+
|
| 60 |
+
---
|
| 61 |
+
|
| 62 |
+
## 📦 Utilisation — via Hugging Face Hub (TorchScript)
|
| 63 |
+
|
| 64 |
+
```python
|
| 65 |
+
from huggingface_hub import hf_hub_download
|
| 66 |
+
from torchvision.models.segmentation import DeepLabV3_ResNet101_Weights
|
| 67 |
+
from PIL import Image
|
| 68 |
+
import torch, numpy as np
|
| 69 |
+
|
| 70 |
+
REPO_ID = "<votre-user>/p9-cityscapes-model"
|
| 71 |
+
FILENAME = "sernet_model.pt"
|
| 72 |
+
|
| 73 |
+
# 1) Charger le modèle TorchScript
|
| 74 |
+
path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME) # cache auto (~/.cache/huggingface)
|
| 75 |
+
model = torch.jit.load(path, map_location="cpu").eval()
|
| 76 |
+
|
| 77 |
+
# 2) Préprocess (torchvision)
|
| 78 |
+
weights = DeepLabV3_ResNet101_Weights.DEFAULT
|
| 79 |
+
preprocess = weights.transforms()
|
| 80 |
+
|
| 81 |
+
# 3) Préparer l’image
|
| 82 |
+
Resample = getattr(Image, "Resampling", Image) # compat Pillow<9.1
|
| 83 |
+
img = Image.open("demo.jpg").convert("RGB")
|
| 84 |
+
img = img.resize((960, 480), Resample.BILINEAR) # optionnel (latence CPU)
|
| 85 |
+
x = preprocess(img).unsqueeze(0) # [1,C,H,W]
|
| 86 |
+
|
| 87 |
+
# 4) Prédire
|
| 88 |
+
with torch.inference_mode():
|
| 89 |
+
out = model(x)
|
| 90 |
+
|
| 91 |
+
logits = out["out"] if isinstance(out, dict) else out # (1,8,H,W)
|
| 92 |
+
seg = torch.argmax(logits, 1).squeeze(0).cpu().numpy().astype("uint8")
|
| 93 |
+
print("mask:", seg.shape, "classes:", np.unique(seg))
|
| 94 |
+
```
|
| 95 |
+
|
| 96 |
+
---
|
| 97 |
+
|
| 98 |
+
## 🎨 Palette (facultatif)
|
| 99 |
+
|
| 100 |
+
```python
|
| 101 |
+
import numpy as np
|
| 102 |
+
from matplotlib import colors
|
| 103 |
+
|
| 104 |
+
PALETTE = ['b','g','r','c','m','y','k','w'] # 0..7
|
| 105 |
+
|
| 106 |
+
def colorize(seg: np.ndarray) -> np.ndarray:
|
| 107 |
+
h, w = seg.shape
|
| 108 |
+
out = np.zeros((h, w, 3), dtype=np.float32)
|
| 109 |
+
for cid in range(8):
|
| 110 |
+
mask = (seg == cid)
|
| 111 |
+
r, g, b = colors.to_rgb(PALETTE[cid])
|
| 112 |
+
out[mask, 0] = r; out[mask, 1] = g; out[mask, 2] = b
|
| 113 |
+
return (out * 255).astype(np.uint8)
|
| 114 |
+
```
|
| 115 |
+
|
| 116 |
+
---
|
| 117 |
+
|
| 118 |
+
## 📊 Métriques
|
| 119 |
+
|
| 120 |
+
- **SERNet (TorchScript)** : **mIoU ≈ 73%**, **wmIoU ≈ 85%** sur Cityscapes (val).
|
| 121 |
+
- *wmIoU* = mIoU pondérée (pondérations par classe).
|
| 122 |
+
|
| 123 |
+
---
|
| 124 |
+
|
| 125 |
+
## ⚠️ Limites & conseils
|
| 126 |
+
|
| 127 |
+
- Conçu pour **scènes urbaines** (Cityscapes) : généralisation limitée hors domaine.
|
| 128 |
+
- CPU : privilégier des entrées ≤ `960×480` pour une latence raisonnable.
|
| 129 |
+
- Pour reproductibilité stricte, pinner la **révision** HF (tag/commit).
|
| 130 |
+
|
| 131 |
+
---
|
| 132 |
+
|
| 133 |
+
## 📚 Références
|
| 134 |
+
|
| 135 |
+
- Cordts et al., **Cityscapes** — CVPR 2016
|
| 136 |
+
- Chen et al., **DeepLab** — *Rethinking Atrous Convolution for Semantic Image Segmentation*
|