1aurent commited on
Commit
d91fe0e
1 Parent(s): 9814e15

Create gen_script.py

Browse files
Files changed (1) hide show
  1. gen_script.py +229 -0
gen_script.py ADDED
@@ -0,0 +1,229 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+
3
+ import datasets
4
+ import json
5
+ from datetime import datetime
6
+
7
+ _VERSION = "0.1.0"
8
+
9
+ _CITATION = """
10
+ @inproceedings{8100027,
11
+ title = {Scene Parsing through ADE20K Dataset},
12
+ author = {Zhou, Bolei and Zhao, Hang and Puig, Xavier and Fidler, Sanja and Barriuso, Adela and Torralba, Antonio},
13
+ year = 2017,
14
+ booktitle = {2017 IEEE Conference on Computer Vision and Pattern Recognition (CVPR)},
15
+ volume = {},
16
+ number = {},
17
+ pages = {5122--5130},
18
+ doi = {10.1109/CVPR.2017.544},
19
+ keywords = {Image segmentation;Semantics;Sun;Labeling;Visualization;Neural networks;Computer vision}
20
+ }
21
+ @misc{zhou2018semantic,
22
+ title = {Semantic Understanding of Scenes through the ADE20K Dataset},
23
+ author = {Bolei Zhou and Hang Zhao and Xavier Puig and Tete Xiao and Sanja Fidler and Adela Barriuso and Antonio Torralba},
24
+ year = 2018,
25
+ eprint = {1608.05442},
26
+ archiveprefix = {arXiv},
27
+ primaryclass = {cs.CV}
28
+ }
29
+ """
30
+
31
+ _DESCRIPTION = """
32
+ ADE20K is composed of more than 27K images from the SUN and Places databases.
33
+ Images are fully annotated with objects, spanning over 3K object categories.
34
+ Many of the images also contain object parts, and parts of parts.
35
+ We also provide the original annotated polygons, as well as object instances for amodal segmentation.
36
+ Images are also anonymized, blurring faces and license plates.
37
+ """
38
+
39
+ _HOMEPAGE = "https://groups.csail.mit.edu/vision/datasets/ADE20K/"
40
+
41
+ _LICENSE = "Creative Commons BSD-3 License Agreement"
42
+
43
+ _FEATURES = datasets.Features(
44
+ {
45
+ "image": datasets.Image(mode="RGB"),
46
+ "segmentations": datasets.Sequence(datasets.Image(mode="RGB")),
47
+ "instances": datasets.Sequence(datasets.Image(mode="L")),
48
+ "filename": datasets.Value("string"),
49
+ "folder": datasets.Value("string"),
50
+ "source": datasets.Features(
51
+ {
52
+ "folder": datasets.Value("string"),
53
+ "filename": datasets.Value("string"),
54
+ "origin": datasets.Value("string"),
55
+ }
56
+ ),
57
+ "scene": datasets.Sequence(datasets.Value("string")),
58
+ "objects": [
59
+ {
60
+ "id": datasets.Value("uint16"),
61
+ "name": datasets.Value("string"),
62
+ "name_ndx": datasets.Value("uint16"),
63
+ "hypernym": datasets.Sequence(datasets.Value("string")),
64
+ "raw_name": datasets.Value("string"),
65
+ "attributes": datasets.Value("string"),
66
+ "depth_ordering_rank": datasets.Value("uint16"),
67
+ "occluded": datasets.Value("bool"),
68
+ "crop": datasets.Value(dtype="bool"),
69
+ "parts": {
70
+ "is_part_of": datasets.Value("uint16"),
71
+ "part_level": datasets.Value("uint8"),
72
+ "has_parts": datasets.Sequence(datasets.Value("uint16")),
73
+ },
74
+ "polygon": {
75
+ "x": datasets.Sequence(datasets.Value("uint16")),
76
+ "y": datasets.Sequence(datasets.Value("uint16")),
77
+ "click_date": datasets.Sequence(datasets.Value("timestamp[us]")),
78
+ },
79
+ "saved_date": datasets.Value("timestamp[us]"),
80
+ }
81
+ ],
82
+ }
83
+ )
84
+
85
+
86
+ class ADE20K(datasets.GeneratorBasedBuilder):
87
+ DEFAULT_WRITER_BATCH_SIZE = 1000
88
+
89
+ def _info(self):
90
+ return datasets.DatasetInfo(
91
+ features=_FEATURES,
92
+ supervised_keys=None,
93
+ description=_DESCRIPTION,
94
+ homepage=_HOMEPAGE,
95
+ license=_LICENSE,
96
+ version=_VERSION,
97
+ citation=_CITATION,
98
+ )
99
+
100
+ def _split_generators(self, dl_manager: datasets.DownloadManager):
101
+ archive_training = Path("ADE20K_2021_17_01/images/ADE/training")
102
+ archive_validation = Path("ADE20K_2021_17_01/images/ADE/validation")
103
+
104
+ jsons_training = sorted(list(archive_training.rglob("*.json")))
105
+ jsons_validation = sorted(list(archive_validation.rglob("*.json")))
106
+
107
+ return [
108
+ datasets.SplitGenerator(
109
+ name=datasets.Split.TRAIN,
110
+ gen_kwargs={"jsons": jsons_training},
111
+ ),
112
+ datasets.SplitGenerator(
113
+ name=datasets.Split.VALIDATION,
114
+ gen_kwargs={"jsons": jsons_validation},
115
+ ),
116
+ ]
117
+
118
+ def parse_date(self, date: str) -> datetime:
119
+ if date == []:
120
+ return None
121
+
122
+ try:
123
+ timestamp = datetime.strptime(date, "%d-%m-%y %H:%M:%S:%f")
124
+ return timestamp
125
+ except:
126
+ pass
127
+
128
+ try:
129
+ timestamp = datetime.strptime(date, "%d-%b-%Y %H:%M:%S:%f")
130
+ return timestamp
131
+ except:
132
+ pass
133
+
134
+ try:
135
+ timestamp = datetime.strptime(date, "%d-%m-%y %H:%M:%S")
136
+ return timestamp
137
+ except:
138
+ pass
139
+
140
+ try:
141
+ timestamp = datetime.strptime(date, "%d-%b-%Y %H:%M:%S")
142
+ return timestamp
143
+ except:
144
+ pass
145
+
146
+ raise ValueError(f"Could not parse date: {date}")
147
+
148
+ def parse_imsize(self, imsize: list[int]) -> list[int]:
149
+ if len(imsize) == 2:
150
+ return imsize + [3]
151
+ return imsize
152
+
153
+ def parse_json(self, json_path: Path):
154
+ with json_path.open("r", encoding="ISO-8859-1") as f:
155
+ data = json.load(f)
156
+ annotation = data["annotation"]
157
+ objects = annotation["object"]
158
+
159
+ segmentations = list(
160
+ json_path.parent.glob(
161
+ f"{annotation['filename'].removesuffix(".jpg")}_parts*"
162
+ )
163
+ )
164
+ segmentations = [str(part) for part in segmentations]
165
+ main_mask = json_path.parent / annotation["filename"]
166
+ main_mask = str(main_mask.with_suffix("")) + "_seg.png"
167
+ segmentations.insert(0, main_mask)
168
+
169
+ instances = [
170
+ json_path.parent / object["instance_mask"] for object in objects
171
+ ]
172
+ instances = [str(instance) for instance in instances]
173
+
174
+ return {
175
+ "image": str(json_path.parent / annotation["filename"]),
176
+ "segmentations": segmentations,
177
+ "instances": instances,
178
+ "filename": annotation["filename"],
179
+ "folder": annotation["folder"],
180
+ "source": {
181
+ "folder": annotation["source"]["folder"],
182
+ "filename": annotation["source"]["filename"],
183
+ "origin": annotation["source"]["origin"],
184
+ },
185
+ "scene": annotation["scene"],
186
+ "objects": [
187
+ {
188
+ "id": object["id"],
189
+ "name": object["name"],
190
+ "name_ndx": object["name_ndx"],
191
+ "hypernym": object["hypernym"],
192
+ "raw_name": object["raw_name"],
193
+ "attributes": ""
194
+ if object["attributes"] == []
195
+ else object["attributes"],
196
+ "depth_ordering_rank": object["depth_ordering_rank"],
197
+ "occluded": object["occluded"] == "yes",
198
+ "crop": object["crop"] == "1",
199
+ "parts": {
200
+ "part_level": object["parts"]["part_level"],
201
+ "is_part_of": None
202
+ if object["parts"]["ispartof"] == []
203
+ else object["parts"]["ispartof"],
204
+ "has_parts": [object["parts"]["hasparts"]]
205
+ if isinstance(object["parts"]["hasparts"], int)
206
+ else object["parts"]["hasparts"],
207
+ },
208
+ "polygon": {
209
+ "x": list(
210
+ map(lambda x: int(max(0, x)), object["polygon"]["x"])
211
+ ),
212
+ "y": list(
213
+ map(lambda y: int(max(0, y)), object["polygon"]["y"])
214
+ ),
215
+ "click_date": []
216
+ if "click_date" not in object["polygon"]
217
+ else list(
218
+ map(self.parse_date, object["polygon"]["click_date"])
219
+ ),
220
+ },
221
+ "saved_date": self.parse_date(object["saved_date"]),
222
+ }
223
+ for object in objects
224
+ ],
225
+ }
226
+
227
+ def _generate_examples(self, jsons: list[Path]):
228
+ for i, json_path in enumerate(jsons):
229
+ yield i, self.parse_json(json_path)