Datasets:
michelecafagna26
commited on
Commit
•
ce2759c
1
Parent(s):
6aaced5
Upload hl.py
Browse files
hl.py
ADDED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
3 |
+
# you may not use this file except in compliance with the License.
|
4 |
+
# You may obtain a copy of the License at
|
5 |
+
#
|
6 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
7 |
+
#
|
8 |
+
# Unless required by applicable law or agreed to in writing, software
|
9 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
10 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
11 |
+
# See the License for the specific language governing permissions and
|
12 |
+
# limitations under the License.
|
13 |
+
"""High-Level dataset."""
|
14 |
+
|
15 |
+
|
16 |
+
import json
|
17 |
+
from pathlib import Path
|
18 |
+
|
19 |
+
import datasets
|
20 |
+
|
21 |
+
|
22 |
+
_CITATION = """\
|
23 |
+
@misc{}
|
24 |
+
"""
|
25 |
+
|
26 |
+
_DESCRIPTION = """\
|
27 |
+
High-level Dataset
|
28 |
+
"""
|
29 |
+
|
30 |
+
# github link
|
31 |
+
_HOMEPAGE = ""
|
32 |
+
|
33 |
+
_LICENSE = "Apache 2.0"
|
34 |
+
|
35 |
+
_URL = "https://huggingface.co/datasets/michelecafagna26/hl/main/data/images.zip"
|
36 |
+
|
37 |
+
|
38 |
+
class HL(datasets.GeneratorBasedBuilder):
|
39 |
+
"""High Level Dataset."""
|
40 |
+
|
41 |
+
VERSION = datasets.Version("1.0.0")
|
42 |
+
|
43 |
+
def _info(self):
|
44 |
+
features = datasets.Features(
|
45 |
+
{
|
46 |
+
"file_name": datasets.Value("string"),
|
47 |
+
"image": datasets.Image(),
|
48 |
+
"scene": datasets.Sequence(datasets.Value("string")),
|
49 |
+
"action": datasets.Sequence(datasets.Value("string")),
|
50 |
+
"rationale": datasets.Sequence(datasets.Value("string")),
|
51 |
+
"object": datasets.Sequence(datasets.Value("string")),
|
52 |
+
# "captions": {
|
53 |
+
# "scene": datasets.Sequence(datasets.Value("string")),
|
54 |
+
# "action": datasets.Sequence(datasets.Value("string")),
|
55 |
+
# "rationale": datasets.Sequence(datasets.Value("string")),
|
56 |
+
# "object": datasets.Sequence(datasets.Value("string")),
|
57 |
+
# },
|
58 |
+
"confidence": {
|
59 |
+
"scene": datasets.Sequence(datasets.Value("float32")),
|
60 |
+
"action": datasets.Sequence(datasets.Value("float32")),
|
61 |
+
"rationale": datasets.Sequence(datasets.Value("float32")),
|
62 |
+
"object": datasets.Sequence(datasets.Value("float32")),
|
63 |
+
}
|
64 |
+
# "purity": {
|
65 |
+
# "scene": datasets.Sequence(datasets.Value("float32")),
|
66 |
+
# "action": datasets.Sequence(datasets.Value("float32")),
|
67 |
+
# "rationale": datasets.Sequence(datasets.Value("float32")),
|
68 |
+
# },
|
69 |
+
# "diversity": {
|
70 |
+
# "scene": datasets.Value("float32"),
|
71 |
+
# "action": datasets.Value("float32"),
|
72 |
+
# "rationale": datasets.Value("float32"),
|
73 |
+
# },
|
74 |
+
}
|
75 |
+
)
|
76 |
+
return datasets.DatasetInfo(
|
77 |
+
description=_DESCRIPTION,
|
78 |
+
features=features,
|
79 |
+
homepage=_HOMEPAGE,
|
80 |
+
license=_LICENSE,
|
81 |
+
citation=_CITATION,
|
82 |
+
)
|
83 |
+
|
84 |
+
def _split_generators(self, dl_manager):
|
85 |
+
archive = dl_manager.download(_URL)
|
86 |
+
return [
|
87 |
+
datasets.SplitGenerator(
|
88 |
+
name=datasets.Split.TRAIN,
|
89 |
+
gen_kwargs={
|
90 |
+
"annotation_file_path": "data/annotations/train.jsonl",
|
91 |
+
"images": dl_manager.iter_archive(archive),
|
92 |
+
},
|
93 |
+
),
|
94 |
+
datasets.SplitGenerator(
|
95 |
+
name=datasets.Split.TEST,
|
96 |
+
gen_kwargs={
|
97 |
+
"annotation_file_path": "data/annotations/test.jsonl",
|
98 |
+
"images": dl_manager.iter_archive(archive),
|
99 |
+
},
|
100 |
+
),
|
101 |
+
]
|
102 |
+
|
103 |
+
def _generate_examples(self, annotation_file_path, images):
|
104 |
+
idx = 0
|
105 |
+
|
106 |
+
with open(annotation_file_path, "r") as fp:
|
107 |
+
metadata = {json.loads(item)['file_name']: json.loads(item) for item in fp}
|
108 |
+
|
109 |
+
# This loop relies on the ordering of the files in the archive:
|
110 |
+
# Annotation files come first, then the images.
|
111 |
+
for img_file_path, img_obj in images:
|
112 |
+
|
113 |
+
file_name = Path(img_file_path).name
|
114 |
+
|
115 |
+
yield idx, {
|
116 |
+
"file_name": file_name,
|
117 |
+
"image": {"path": img_file_path, "bytes": img_obj.read()},
|
118 |
+
"scene": metadata[file_name]['captions']['scene'],
|
119 |
+
"action": metadata[file_name]['captions']['action'],
|
120 |
+
"rationale": metadata[file_name]['captions']['rationale'],
|
121 |
+
"object": metadata[file_name]['captions']['object'],
|
122 |
+
"confidence": metadata[file_name]['captions']['confidence'],
|
123 |
+
}
|
124 |
+
idx += 1
|