davanstrien HF staff commited on
Commit
f241e21
1 Parent(s): 8172af9

draft dataset

Browse files
data/images.zip ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:dcd764a5335d62b6dda2bb85d372d39c33c56f4c90ad19b4dd3cae638bd2d294
3
+ size 1193066001
data/train_80_percent.json ADDED
The diff for this file is too large to render. See raw diff
data/val_20_percent.json ADDED
The diff for this file is too large to render. See raw diff
loc_beyond_words.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2022 Daniel van Strien
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ """Beyond Words"""
15
+
16
+ import collections
17
+ import json
18
+ import os
19
+ from typing import Any, Dict, List
20
+ import datasets
21
+ from pathlib import Path
22
+
23
+ _CITATION = "TODO"
24
+
25
+
26
+ _DESCRIPTION = "TODO"
27
+
28
+
29
+ _HOMEPAGE = "TODO"
30
+
31
+
32
+ _LICENSE = "Public Domain Mark 1.0"
33
+
34
+
35
+ class BeyondWords(datasets.GeneratorBasedBuilder):
36
+ """Beyond Words Dataset"""
37
+
38
+ def _info(self):
39
+ features = datasets.Features(
40
+ {
41
+ "image_id": datasets.Value("int64"),
42
+ "image": datasets.Image(),
43
+ "width": datasets.Value("int32"),
44
+ "height": datasets.Value("int32"),
45
+ }
46
+ )
47
+
48
+ object_dict = {
49
+ "bw_id": datasets.Value("string"),
50
+ "category_id": datasets.ClassLabel(
51
+ names=[
52
+ "Photograph",
53
+ "Illustration",
54
+ "Map",
55
+ "Comics/Cartoon",
56
+ "Editorial Cartoon",
57
+ "Headline",
58
+ "Advertisement",
59
+ ]
60
+ ),
61
+ "image_id": datasets.Value("string"),
62
+ "id": datasets.Value("int64"),
63
+ "area": datasets.Value("int64"),
64
+ "bbox": datasets.Sequence(datasets.Value("float32"), length=4),
65
+ "iscrowd": datasets.Value(
66
+ "bool"
67
+ ), # always False for stuff segmentation task
68
+ }
69
+ features["objects"] = datasets.Sequence(object_dict)
70
+
71
+ return datasets.DatasetInfo(
72
+ description=_DESCRIPTION,
73
+ features=features,
74
+ homepage=_HOMEPAGE,
75
+ license=_LICENSE,
76
+ citation=_CITATION,
77
+ )
78
+
79
+ def _split_generators(self, dl_manager):
80
+ images = dl_manager.download_and_extract("data/images.zip")
81
+ training = dl_manager.download("data/train_80_percent.json")
82
+ validation = dl_manager.download("data/val_20_percent.json")
83
+ return [
84
+ datasets.SplitGenerator(
85
+ name=datasets.Split.TRAIN,
86
+ gen_kwargs={
87
+ "annotations_file": Path(training),
88
+ "image_dir": Path(images),
89
+ },
90
+ ),
91
+ datasets.SplitGenerator(
92
+ name=datasets.Split.VALIDATION,
93
+ gen_kwargs={
94
+ "annotations_file": Path(validation),
95
+ "image_dir": Path(images),
96
+ },
97
+ ),
98
+ ]
99
+
100
+ def _get_image_id_to_annotations_mapping(
101
+ self, annotations: List[Dict]
102
+ ) -> Dict[int, List[Dict[Any, Any]]]:
103
+ """
104
+ A helper function to build a mapping from image ids to annotations.
105
+ """
106
+ image_id_to_annotations = collections.defaultdict(list)
107
+ for annotation in annotations:
108
+ image_id_to_annotations[annotation["image_id"]].append(annotation)
109
+ return image_id_to_annotations
110
+
111
+ def _generate_examples(self, annotations_file, image_dir):
112
+ def _image_info_to_example(image_info, image_dir):
113
+ image = image_info["file_name"]
114
+ return {
115
+ "image_id": image_info["id"],
116
+ "image": os.path.join(image_dir, "images", image),
117
+ "width": image_info["width"],
118
+ "height": image_info["height"],
119
+ }
120
+
121
+ with open(annotations_file, encoding="utf8") as f:
122
+ annotation_data = json.load(f)
123
+ images = annotation_data["images"]
124
+ annotations = annotation_data["annotations"]
125
+ image_id_to_annotations = self._get_image_id_to_annotations_mapping(
126
+ annotations
127
+ )
128
+ for idx, image_info in enumerate(images):
129
+ example = _image_info_to_example(image_info, image_dir)
130
+
131
+ annotations = image_id_to_annotations[image_info["id"]]
132
+ objects = []
133
+ for annotation in annotations:
134
+ objects.append(annotation)
135
+ example["objects"] = objects
136
+ yield (idx, example)