Oliver Hahn
commited on
Commit
·
03e384b
1
Parent(s):
9c253f2
add demo
Browse files- .DS_Store +0 -0
- datasets/.DS_Store +0 -0
- datasets/cityscapes.py +106 -0
.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
datasets/.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
datasets/cityscapes.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torchvision
|
2 |
+
import numpy as np
|
3 |
+
from PIL import Image
|
4 |
+
from typing import List, Any, Callable, Tuple
|
5 |
+
from collections import namedtuple
|
6 |
+
|
7 |
+
def get_cs_labeldata():
|
8 |
+
cls_names = ['road', 'sidewalk', 'parking', 'rail track', 'building',
|
9 |
+
'wall', 'fence', 'guard rail', 'bridge', 'tunnel',
|
10 |
+
'pole', 'polegroup', 'traffic light', 'traffic sign', 'vegetation',
|
11 |
+
'terrain', 'sky', 'person', 'rider', 'car',
|
12 |
+
'truck', 'bus', 'caravan', 'trailer', 'train',
|
13 |
+
'motorcycle', 'bicycle']
|
14 |
+
colormap = np.array([
|
15 |
+
[128, 64, 128],
|
16 |
+
[244, 35, 232],
|
17 |
+
[250, 170, 160],
|
18 |
+
[230, 150, 140],
|
19 |
+
[70, 70, 70],
|
20 |
+
[102, 102, 156],
|
21 |
+
[190, 153, 153],
|
22 |
+
[180, 165, 180],
|
23 |
+
[150, 100, 100],
|
24 |
+
[150, 120, 90],
|
25 |
+
[153, 153, 153],
|
26 |
+
[153, 153, 153],
|
27 |
+
[250, 170, 30],
|
28 |
+
[220, 220, 0],
|
29 |
+
[107, 142, 35],
|
30 |
+
[152, 251, 152],
|
31 |
+
[70, 130, 180],
|
32 |
+
[220, 20, 60],
|
33 |
+
[255, 0, 0],
|
34 |
+
[0, 0, 142],
|
35 |
+
[0, 0, 70],
|
36 |
+
[0, 60, 100],
|
37 |
+
[0, 0, 90],
|
38 |
+
[0, 0, 110],
|
39 |
+
[0, 80, 100],
|
40 |
+
[0, 0, 230],
|
41 |
+
[119, 11, 32],
|
42 |
+
[0, 0, 0],
|
43 |
+
[220, 220, 220]])
|
44 |
+
return cls_names, colormap
|
45 |
+
|
46 |
+
class CityscapesDataset(torchvision.datasets.Cityscapes):
|
47 |
+
|
48 |
+
def __init__(self,
|
49 |
+
transforms: List[Callable],
|
50 |
+
*args: Any,
|
51 |
+
**kwargs: Any):
|
52 |
+
|
53 |
+
super(CityscapesDataset, self).__init__(*args,
|
54 |
+
**kwargs,
|
55 |
+
target_type="semantic")
|
56 |
+
self.transforms = transforms
|
57 |
+
self.classes = ['road', 'sidewalk', 'parking', 'rail track', 'building',
|
58 |
+
'wall', 'fence', 'guard rail', 'bridge', 'tunnel',
|
59 |
+
'pole', 'polegroup', 'traffic light', 'traffic sign', 'vegetation',
|
60 |
+
'terrain', 'sky', 'person', 'rider', 'car',
|
61 |
+
'truck', 'bus', 'caravan', 'trailer', 'train',
|
62 |
+
'motorcycle', 'bicycle']
|
63 |
+
|
64 |
+
def __getitem__(self, index: int) -> Tuple[Any, Any]:
|
65 |
+
"""
|
66 |
+
Args:
|
67 |
+
index (int): Index
|
68 |
+
Returns:
|
69 |
+
tuple: (image, target) where target is a tuple of all target types if target_type is a list with more
|
70 |
+
than one item. Otherwise target is a json object if target_type="polygon", else the image segmentation.
|
71 |
+
"""
|
72 |
+
img_pth = self.images[index]
|
73 |
+
image = Image.open(self.images[index]).convert('RGB')
|
74 |
+
|
75 |
+
targets: Any = []
|
76 |
+
for i, t in enumerate(self.target_type):
|
77 |
+
if t == 'polygon':
|
78 |
+
target = self._load_json(self.targets[index][i])
|
79 |
+
else:
|
80 |
+
target = Image.open(self.targets[index][i])
|
81 |
+
|
82 |
+
targets.append(target)
|
83 |
+
|
84 |
+
target = tuple(targets) if len(targets) > 1 else targets[0]
|
85 |
+
|
86 |
+
if self.transforms is not None:
|
87 |
+
image, target = self.transforms(image, target)
|
88 |
+
|
89 |
+
return image, target, img_pth
|
90 |
+
|
91 |
+
def cityscapes(root: str,
|
92 |
+
split: str,
|
93 |
+
transforms: List[Callable]):
|
94 |
+
return CityscapesDataset(root=root,
|
95 |
+
split=split,
|
96 |
+
transforms=transforms)
|
97 |
+
|
98 |
+
CityscapesClass = namedtuple('CityscapesClass', ['name', 'id', 'train_id', 'category', 'category_id',
|
99 |
+
'has_instances', 'ignore_in_eval', 'color'])
|
100 |
+
|
101 |
+
classes = ['road', 'sidewalk', 'parking', 'rail track', 'building',
|
102 |
+
'wall', 'fence', 'guard rail', 'bridge', 'tunnel',
|
103 |
+
'pole', 'polegroup', 'traffic light', 'traffic sign', 'vegetation',
|
104 |
+
'terrain', 'sky', 'person', 'rider', 'car',
|
105 |
+
'truck', 'bus', 'caravan', 'trailer', 'train',
|
106 |
+
'motorcycle', 'bicycle']
|