fuliucansheng commited on
Commit
411cb45
1 Parent(s): 94ed3b6

add new file

Browse files
Files changed (4) hide show
  1. .gitattributes +2 -0
  2. pascal_voc.py +411 -0
  3. voc2007.tar.gz +3 -0
  4. voc2012.tar.gz +3 -0
.gitattributes CHANGED
@@ -25,3 +25,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
25
  *.zip filter=lfs diff=lfs merge=lfs -text
26
  *.zstandard filter=lfs diff=lfs merge=lfs -text
27
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
25
  *.zip filter=lfs diff=lfs merge=lfs -text
26
  *.zstandard filter=lfs diff=lfs merge=lfs -text
27
  *tfevents* filter=lfs diff=lfs merge=lfs -text
28
+ ./voc2007.tar.gz filter=lfs diff=lfs merge=lfs -text
29
+ ./voc2012.tar.gz filter=lfs diff=lfs merge=lfs -text
pascal_voc.py ADDED
@@ -0,0 +1,411 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import logging
3
+ import datasets
4
+ import xml.etree.ElementTree as ET
5
+ from PIL import Image
6
+ from collections import defaultdict
7
+
8
+ _CITATION = """
9
+ PASCAL_VOC
10
+ """
11
+
12
+ _DESCRIPTION = """
13
+ PASCAL_VOC
14
+ """
15
+
16
+ _URLS = {
17
+ "voc2007": "voc2007.tar.gz",
18
+ "voc2012": "voc2012.tar.gz",
19
+ }
20
+
21
+ # fmt: off
22
+ CLASS_INFOS = [
23
+ # class name id train color
24
+ ( 'aeroplane' , 0 , 0 , ( 128, 0, 0) ),
25
+ ( 'bicycle' , 1 , 1 , ( 0, 128, 0) ),
26
+ ( 'bird' , 2 , 2 , ( 128, 128, 0) ),
27
+ ( 'boat' , 3 , 3 , ( 0, 0, 128) ),
28
+ ( 'bottle' , 4 , 4 , ( 128, 0, 128) ),
29
+ ( 'bus' , 5 , 5 , ( 0, 128, 128) ),
30
+ ( 'car' , 6 , 6 , ( 128, 128, 128) ),
31
+ ( 'cat' , 7 , 7 , ( 64, 0, 0) ),
32
+ ( 'chair' , 8 , 8 , ( 192, 0, 0) ),
33
+ ( 'cow' , 9 , 9 , ( 64, 128, 0) ),
34
+ ( 'diningtable' , 10 , 10 , ( 192, 128, 0) ),
35
+ ( 'dog' , 11 , 11 , ( 64, 0, 128) ),
36
+ ( 'horse' , 12 , 12 , ( 192, 0, 128) ),
37
+ ( 'motorbike' , 13 , 13 , ( 64, 128, 128) ),
38
+ ( 'person' , 14 , 14 , ( 192, 128, 128) ),
39
+ ( 'pottedplant' , 15 , 15 , ( 0, 64, 0) ),
40
+ ( 'sheep' , 16 , 16 , ( 128, 64, 0) ),
41
+ ( 'sofa' , 17 , 17 , ( 0, 192, 0) ),
42
+ ( 'train' , 18 , 18 , ( 128, 192, 0) ),
43
+ ( 'tvmonitor' , 19 , 19 , ( 0, 64, 128) ),
44
+ ( 'background' , 20 , 20 , ( 0, 0, 0) ),
45
+ ( 'borderingregion' , 255, 21 , ( 224, 224, 192) ),
46
+ ]
47
+
48
+ ACTION_INFOS = [
49
+ # class name id
50
+ ( 'phoning' , 0 ),
51
+ ( 'playinginstrument' , 1 ),
52
+ ( 'reading' , 2 ),
53
+ ( 'ridingbike' , 3 ),
54
+ ( 'ridinghorse' , 4 ),
55
+ ( 'running' , 5 ),
56
+ ( 'takingphoto' , 6 ),
57
+ ( 'usingcomputer' , 7 ),
58
+ ( 'walking' , 8 ),
59
+ ( 'jumping' , 9 ),
60
+ ( 'other' , 10 ),
61
+ ]
62
+
63
+ LAYOUT_INFOS = [
64
+ # class name id
65
+ ( 'Frontal' , 0 ),
66
+ ( 'Left' , 1 ),
67
+ ( 'Rear' , 2 ),
68
+ ( 'Right' , 3 ),
69
+ ( 'Unspecified' , 4 ),
70
+ ]
71
+
72
+ # fmt: on
73
+
74
+ CLASS_NAMES = [CLASS_INFO[0] for CLASS_INFO in CLASS_INFOS]
75
+ ACTION_NAMES = [ACTION_INFO[0] for ACTION_INFO in ACTION_INFOS]
76
+ LAYOUT_NAMES = [LAYOUT_INFO[0] for LAYOUT_INFO in LAYOUT_INFOS]
77
+
78
+ CLASS_DICT = {CLASS_INFO[0]: CLASS_INFO[2] for CLASS_INFO in CLASS_INFOS}
79
+ ACTION_DICT = {ACTION_INFO[0]: ACTION_INFO[1] for ACTION_INFO in ACTION_INFOS}
80
+ LAYOUT_DICT = {LAYOUT_INFO[0]: LAYOUT_INFO[1] for LAYOUT_INFO in LAYOUT_INFOS}
81
+
82
+ # datasets.Features
83
+
84
+ action_features = datasets.Features(
85
+ {
86
+ "id": datasets.Value("int32"),
87
+ "image": datasets.features.Image(),
88
+ "height": datasets.Value("int32"),
89
+ "width": datasets.Value("int32"),
90
+ "classes": datasets.features.Sequence(datasets.Value("int32")),
91
+ "objects": datasets.features.Sequence(
92
+ {
93
+ "bboxes": datasets.Sequence(datasets.Value("float32")),
94
+ "classes": datasets.features.ClassLabel(names=ACTION_NAMES),
95
+ "difficult": datasets.Value("int32"),
96
+ }
97
+ ),
98
+ }
99
+ )
100
+
101
+ layout_features = datasets.Features(
102
+ {
103
+ "id": datasets.Value("int32"),
104
+ "image": datasets.features.Image(),
105
+ "height": datasets.Value("int32"),
106
+ "width": datasets.Value("int32"),
107
+ "classes": datasets.features.Sequence(datasets.Value("int32")),
108
+ "objects": datasets.features.Sequence(
109
+ {
110
+ "bboxes": datasets.Sequence(datasets.Value("float32")),
111
+ "classes": datasets.features.ClassLabel(names=LAYOUT_NAMES),
112
+ "difficult": datasets.Value("int32"),
113
+ }
114
+ ),
115
+ }
116
+ )
117
+
118
+ main_features = datasets.Features(
119
+ {
120
+ "id": datasets.Value("int32"),
121
+ "image": datasets.features.Image(),
122
+ "height": datasets.Value("int32"),
123
+ "width": datasets.Value("int32"),
124
+ "classes": datasets.features.Sequence(datasets.Value("int32")),
125
+ "objects": datasets.features.Sequence(
126
+ {
127
+ "bboxes": datasets.Sequence(datasets.Value("float32")),
128
+ "classes": datasets.features.ClassLabel(names=CLASS_NAMES),
129
+ "difficult": datasets.Value("int32"),
130
+ }
131
+ ),
132
+ }
133
+ )
134
+
135
+ segmentation_features = datasets.Features(
136
+ {
137
+ "id": datasets.Value("int32"),
138
+ "image": datasets.features.Image(),
139
+ "height": datasets.Value("int32"),
140
+ "width": datasets.Value("int32"),
141
+ "classes": datasets.features.Sequence(datasets.Value("int32")),
142
+ "class_gt_image": datasets.features.Image(),
143
+ "object_gt_image": datasets.features.Image(),
144
+ }
145
+ )
146
+
147
+ _DATASET_FEATURES = {
148
+ "action": action_features,
149
+ "layout": layout_features,
150
+ "main": main_features,
151
+ "segmentation": segmentation_features,
152
+ }
153
+
154
+
155
+ def get_main_classes(data_folder):
156
+ class_infos = defaultdict(set)
157
+ class_folder = os.path.join(data_folder, "ImageSets", "Main")
158
+ for f in os.listdir(class_folder):
159
+ if not f.endswith(".txt") or len(f.split("_")) != 2:
160
+ continue
161
+ lines = open(os.path.join(class_folder, f), "r").read().split("\n")
162
+ name = f.split("_")[0]
163
+ for line in lines:
164
+ spans = line.strip().split(" ")
165
+ spans = list(filter(lambda x: x.strip() != "", spans))
166
+ if len(spans) != 2 or int(spans[1]) != 1:
167
+ continue
168
+ class_infos[spans[0]].add(name)
169
+ return class_infos
170
+
171
+
172
+ def get_annotation(data_folder):
173
+ anno_infos = dict()
174
+ anno_folder = os.path.join(data_folder, "Annotations")
175
+ for f in os.listdir(anno_folder):
176
+ if not f.endswith(".xml"):
177
+ continue
178
+ anno_file = os.path.join(anno_folder, f)
179
+ anno_tree = ET.parse(anno_file)
180
+ objects = []
181
+ for obj in anno_tree.findall("./object"):
182
+ info = {
183
+ "class": obj.findall("./name")[0].text,
184
+ "bbox": [
185
+ int(float(obj.findall("./bndbox/xmin")[0].text)),
186
+ int(float(obj.findall("./bndbox/ymin")[0].text)),
187
+ int(float(obj.findall("./bndbox/xmax")[0].text)),
188
+ int(float(obj.findall("./bndbox/ymax")[0].text)),
189
+ ],
190
+ }
191
+
192
+ if obj.findall("./pose"):
193
+ info["pose"] = obj.findall("./pose")[0].text
194
+ if obj.findall("./truncated"):
195
+ info["truncated"] = int(obj.findall("./truncated")[0].text)
196
+ if obj.findall("./difficult"):
197
+ info["difficult"] = int(obj.findall("./difficult")[0].text)
198
+ else:
199
+ info["difficult"] = 0
200
+ if obj.findall("./occluded"):
201
+ info["occluded"] = int(obj.findall("./occluded")[0].text)
202
+
203
+ if obj.findall("./actions"):
204
+ info["action"] = [
205
+ action.tag
206
+ for action in obj.findall("./actions/")
207
+ if int(action.text) == 1
208
+ ][0]
209
+
210
+ objects.append(info)
211
+ anno_info = {
212
+ "image": anno_tree.findall("./filename")[0].text,
213
+ "height": int(anno_tree.findall("./size/height")[0].text),
214
+ "width": int(anno_tree.findall("./size/width")[0].text),
215
+ "segmented": int(anno_tree.findall("./segmented")[0].text),
216
+ "objects": objects,
217
+ }
218
+ stem, suffix = os.path.splitext(f)
219
+ anno_infos[stem] = anno_info
220
+
221
+ return anno_infos
222
+
223
+
224
+ class PASCALConfig(datasets.BuilderConfig):
225
+ def __init__(self, data_name, task_name, **kwargs):
226
+ """
227
+
228
+ Args:
229
+ **kwargs: keyword arguments forwarded to super.
230
+ """
231
+ super().__init__(**kwargs)
232
+ assert data_name in ["voc2007", "voc2012"] and task_name in [
233
+ "action",
234
+ "layout",
235
+ "main",
236
+ "segmentation",
237
+ ]
238
+ assert not (data_name == "voc2007" and task_name == "action")
239
+ self.data_name = data_name
240
+ self.task_name = task_name
241
+
242
+
243
+ class PASCALDataset(datasets.GeneratorBasedBuilder):
244
+
245
+ BUILDER_CONFIGS = [
246
+ PASCALConfig(
247
+ name="voc2007_layout",
248
+ version=datasets.Version("1.0.0", ""),
249
+ description="voc2007 layout dataset",
250
+ data_name="voc2007",
251
+ task_name="layout",
252
+ ),
253
+ PASCALConfig(
254
+ name="voc2007_main",
255
+ version=datasets.Version("1.0.0", ""),
256
+ description="voc2007 main dataset",
257
+ data_name="voc2007",
258
+ task_name="main",
259
+ ),
260
+ PASCALConfig(
261
+ name="voc2007_segmentation",
262
+ version=datasets.Version("1.0.0", ""),
263
+ description="voc2007 segmentation dataset",
264
+ data_name="voc2007",
265
+ task_name="segmentation",
266
+ ),
267
+ PASCALConfig(
268
+ name="voc2012_action",
269
+ version=datasets.Version("1.0.0", ""),
270
+ description="voc2012 action dataset",
271
+ data_name="voc2012",
272
+ task_name="action",
273
+ ),
274
+ PASCALConfig(
275
+ name="voc2012_layout",
276
+ version=datasets.Version("1.0.0", ""),
277
+ description="voc2012 layout dataset",
278
+ data_name="voc2012",
279
+ task_name="layout",
280
+ ),
281
+ PASCALConfig(
282
+ name="voc2012_main",
283
+ version=datasets.Version("1.0.0", ""),
284
+ description="voc2012 main dataset",
285
+ data_name="voc2012",
286
+ task_name="main",
287
+ ),
288
+ PASCALConfig(
289
+ name="voc2012_segmentation",
290
+ version=datasets.Version("1.0.0", ""),
291
+ description="voc2012 segmentation dataset",
292
+ data_name="voc2012",
293
+ task_name="segmentation",
294
+ ),
295
+ ]
296
+
297
+ def _info(self):
298
+ return datasets.DatasetInfo(
299
+ description=_DESCRIPTION,
300
+ features=_DATASET_FEATURES[self.config.task_name],
301
+ # No default supervised_keys (as we have to pass both question
302
+ # and context as input).
303
+ supervised_keys=None,
304
+ homepage="https://fuliucansheng.github.io/",
305
+ citation=_CITATION,
306
+ )
307
+
308
+ def _split_generators(self, dl_manager):
309
+ downloaded_files = dl_manager.download_and_extract(_URLS[self.config.data_name])
310
+
311
+ return [
312
+ datasets.SplitGenerator(
313
+ name=datasets.Split.TRAIN,
314
+ gen_kwargs={"filepath": downloaded_files, "split": "train"},
315
+ ),
316
+ datasets.SplitGenerator(
317
+ name=datasets.Split.VALIDATION,
318
+ gen_kwargs={"filepath": downloaded_files, "split": "val"},
319
+ ),
320
+ datasets.SplitGenerator(
321
+ name=datasets.Split.TEST,
322
+ gen_kwargs={"filepath": downloaded_files, "split": "test"},
323
+ ),
324
+ ]
325
+
326
+ def _generate_examples(self, filepath, split):
327
+ """This function returns the examples in the raw (text) form."""
328
+ logging.info("generating examples from = %s, split = %s", filepath, split)
329
+ data_folder = os.path.join(filepath, os.listdir(filepath)[0])
330
+ anno_infos = get_annotation(data_folder)
331
+ class_infos = get_main_classes(data_folder)
332
+ data_file = os.path.join(
333
+ data_folder, "ImageSets", self.config.task_name.capitalize(), f"{split}.txt"
334
+ )
335
+ with open(data_file, encoding="utf-8") as f:
336
+ for id_, line in enumerate(f):
337
+ line = line.strip()
338
+ if line.count(" ") > 0:
339
+ line = line.split(" ")[0]
340
+ anno_info = anno_infos.get(line)
341
+ if anno_info is None:
342
+ continue
343
+
344
+ image = os.path.join(data_folder, "JPEGImages", anno_info["image"])
345
+ if not os.path.exists(image):
346
+ continue
347
+
348
+ classes = (
349
+ [CLASS_DICT[c] for c in class_infos.get(line.strip())]
350
+ if line.strip() in class_infos
351
+ else []
352
+ )
353
+
354
+ example = {
355
+ "id": id_,
356
+ "image": Image.open(os.path.abspath(image)),
357
+ "height": anno_info["height"],
358
+ "width": anno_info["width"],
359
+ "classes": classes,
360
+ }
361
+
362
+ objects_info = anno_info["objects"]
363
+
364
+ if self.config.task_name == "action":
365
+ example["objects"] = [
366
+ {
367
+ "bboxes": object_info["bbox"],
368
+ "classes": object_info["action"],
369
+ "difficult": object_info["difficult"],
370
+ }
371
+ for object_info in objects_info
372
+ if "action" in object_info
373
+ ]
374
+ if len(example["objects"]) == 0 and split != "test":
375
+ continue
376
+
377
+ if self.config.task_name == "layout":
378
+ example["objects"] = [
379
+ {"bboxes": object_info["bbox"], "classes": object_info["pose"], "difficult": object_info["difficult"],}
380
+ for object_info in objects_info
381
+ if "pose" in object_info
382
+ ]
383
+ if len(example["objects"]) == 0 and split != "test":
384
+ continue
385
+
386
+ if self.config.task_name == "main":
387
+ example["objects"] = [
388
+ {"bboxes": object_info["bbox"], "classes": object_info["class"], "difficult": object_info["difficult"],}
389
+ for object_info in objects_info
390
+ if "class" in object_info
391
+ ]
392
+ if len(example["objects"]) == 0 and split != "test":
393
+ continue
394
+
395
+ if self.config.task_name == "segmentation":
396
+ example["class_gt_image"] = Image.open(os.path.abspath(
397
+ os.path.join(
398
+ data_folder,
399
+ "SegmentationClass",
400
+ anno_info["image"].replace(".jpg", ".png"),
401
+ )
402
+ ))
403
+ example["object_gt_image"] = Image.open(os.path.abspath(
404
+ os.path.join(
405
+ data_folder,
406
+ "SegmentationObject",
407
+ anno_info["image"].replace(".jpg", ".png"),
408
+ )
409
+ ))
410
+
411
+ yield id_, example
voc2007.tar.gz ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:68a097378cacb0c2a0c3823999ecbb5fb1f7633a5115bf37421ac8c8f23c5fa0
3
+ size 877627496
voc2012.tar.gz ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5d7b734550f56aacd52fd39f20cc2087b5282743f44bff9bd365a56412b136dd
3
+ size 3760275485