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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
|
| 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 |
+
# TODO: Address all TODOs and remove all explanatory comments
|
| 15 |
+
"""TODO: Add a description here."""
|
| 16 |
+
|
| 17 |
+
import csv
|
| 18 |
+
import os
|
| 19 |
+
import numpy as np
|
| 20 |
+
from PIL import Image
|
| 21 |
+
|
| 22 |
+
import datasets
|
| 23 |
+
|
| 24 |
+
# TODO: Add BibTeX citation
|
| 25 |
+
# Find for instance the citation on arxiv or on the dataset repo/website
|
| 26 |
+
_CITATION = """\
|
| 27 |
+
@InProceedings{huggingface:dataset,
|
| 28 |
+
title = {A great new dataset},
|
| 29 |
+
author={huggingface, Inc.
|
| 30 |
+
},
|
| 31 |
+
year={2020}
|
| 32 |
+
}
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
# TODO: Add description of the dataset here
|
| 36 |
+
# You can copy an official description
|
| 37 |
+
_DESCRIPTION = """\
|
| 38 |
+
This new dataset is designed to solve this great NLP task and is crafted with a lot of care.
|
| 39 |
+
"""
|
| 40 |
+
|
| 41 |
+
# TODO: Add a link to an official homepage for the dataset here
|
| 42 |
+
_HOMEPAGE = ""
|
| 43 |
+
|
| 44 |
+
# TODO: Add the licence for the dataset here if you can find it
|
| 45 |
+
_LICENSE = ""
|
| 46 |
+
|
| 47 |
+
# TODO: Add link to the official dataset URLs here
|
| 48 |
+
# The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
|
| 49 |
+
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
|
| 50 |
+
_URLS = {
|
| 51 |
+
"sample": "http://hyperion.bbirke.de/data/docbank/sample.zip",
|
| 52 |
+
"full": "",
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
_FEATURES = datasets.Features(
|
| 56 |
+
{
|
| 57 |
+
"id": datasets.Value("string"),
|
| 58 |
+
"tokens": datasets.Sequence(datasets.Value("string")),
|
| 59 |
+
"bboxes": datasets.Sequence(datasets.Sequence(datasets.Value("int64"))),
|
| 60 |
+
"RGBs": datasets.Sequence(datasets.Sequence(datasets.Value("int64"))),
|
| 61 |
+
"fonts": datasets.Sequence(datasets.Value("string")),
|
| 62 |
+
"image": datasets.Array3D(shape=(3, 224, 224), dtype="uint8"),
|
| 63 |
+
"original_image": datasets.features.Image(),
|
| 64 |
+
"labels": datasets.Sequence(datasets.features.ClassLabel(
|
| 65 |
+
names=['abstract', 'author', 'caption', 'date', 'equation', 'figure', 'footer', 'list', 'paragraph',
|
| 66 |
+
'reference', 'section', 'table', 'title']
|
| 67 |
+
))
|
| 68 |
+
# These are the features of your dataset like images, labels ...
|
| 69 |
+
}
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
_DEFUNCT_FILE_IDS = [
|
| 73 |
+
'126.tar_1706.03360.gz_dispersion_v2_7', '119.tar_1606.07466.gz_20160819Draft_8',
|
| 74 |
+
'167.tar_1412.4821.gz_IDM_TD_Paper_16', '17.tar_1701.07437.gz_muon-beam-dump_final_2',
|
| 75 |
+
'31.tar_1702.04307.gz_held-karp_21', '7.tar_1401.4493.gz_ReversibleNoise_2'
|
| 76 |
+
]
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def load_image(image_path, size=None):
|
| 80 |
+
image = Image.open(image_path).convert("RGB")
|
| 81 |
+
w, h = image.size
|
| 82 |
+
if size is not None:
|
| 83 |
+
# resize image
|
| 84 |
+
image = image.resize((size, size))
|
| 85 |
+
image = np.asarray(image)
|
| 86 |
+
image = image[:, :, ::-1] # flip color channels from RGB to BGR
|
| 87 |
+
image = image.transpose(2, 0, 1) # move channels to first dimension
|
| 88 |
+
return image, (w, h)
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def normalize_bbox(bbox, size):
|
| 92 |
+
return [
|
| 93 |
+
int(1000 * int(bbox[0]) / size[0]),
|
| 94 |
+
int(1000 * int(bbox[1]) / size[1]),
|
| 95 |
+
int(1000 * int(bbox[2]) / size[0]),
|
| 96 |
+
int(1000 * int(bbox[3]) / size[1]),
|
| 97 |
+
]
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def simplify_bbox(bbox):
|
| 101 |
+
return [
|
| 102 |
+
min(bbox[0::2]),
|
| 103 |
+
min(bbox[1::2]),
|
| 104 |
+
max(bbox[2::2]),
|
| 105 |
+
max(bbox[3::2]),
|
| 106 |
+
]
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
def merge_bbox(bbox_list):
|
| 110 |
+
x0, y0, x1, y1 = list(zip(*bbox_list))
|
| 111 |
+
return [min(x0), min(y0), max(x1), max(y1)]
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
# TODO: Name of the dataset usually matches the script name with CamelCase instead of snake_case
|
| 115 |
+
class Docbank(datasets.GeneratorBasedBuilder):
|
| 116 |
+
"""TODO: Short description of my dataset."""
|
| 117 |
+
|
| 118 |
+
VERSION = datasets.Version("1.0.0")
|
| 119 |
+
|
| 120 |
+
# This is an example of a dataset with multiple configurations.
|
| 121 |
+
# If you don't want/need to define several sub-sets in your dataset,
|
| 122 |
+
# just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
|
| 123 |
+
|
| 124 |
+
# If you need to make complex sub-parts in the datasets with configurable options
|
| 125 |
+
# You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
|
| 126 |
+
# BUILDER_CONFIG_CLASS = MyBuilderConfig
|
| 127 |
+
|
| 128 |
+
# You will be able to load one or the other configurations in the following list with
|
| 129 |
+
# data = datasets.load_dataset('my_dataset', 'first_domain')
|
| 130 |
+
# data = datasets.load_dataset('my_dataset', 'second_domain')
|
| 131 |
+
BUILDER_CONFIGS = [
|
| 132 |
+
datasets.BuilderConfig(name="sample", version=VERSION,
|
| 133 |
+
description="This part of my dataset covers a first domain"),
|
| 134 |
+
datasets.BuilderConfig(name="full", version=VERSION,
|
| 135 |
+
description="This part of my dataset covers a second domain"),
|
| 136 |
+
]
|
| 137 |
+
|
| 138 |
+
DEFAULT_CONFIG_NAME = "small" # It's not mandatory to have a default configuration. Just use one if it make sense.
|
| 139 |
+
|
| 140 |
+
def _info(self):
|
| 141 |
+
# TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
|
| 142 |
+
|
| 143 |
+
return datasets.DatasetInfo(
|
| 144 |
+
# This is the description that will appear on the datasets page.
|
| 145 |
+
description=_DESCRIPTION,
|
| 146 |
+
# This defines the different columns of the dataset and their types
|
| 147 |
+
features=_FEATURES, # Here we define them above because they are different between the two configurations
|
| 148 |
+
# If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
|
| 149 |
+
# specify them. They'll be used if as_supervised=True in builder.as_dataset.
|
| 150 |
+
# supervised_keys=("sentence", "label"),
|
| 151 |
+
# Homepage of the dataset for documentation
|
| 152 |
+
homepage=_HOMEPAGE,
|
| 153 |
+
# License for the dataset if available
|
| 154 |
+
license=_LICENSE,
|
| 155 |
+
# Citation for the dataset
|
| 156 |
+
citation=_CITATION,
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
def _split_generators(self, dl_manager):
|
| 160 |
+
# TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
|
| 161 |
+
# If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
|
| 162 |
+
|
| 163 |
+
# dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
|
| 164 |
+
# It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
|
| 165 |
+
# By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
|
| 166 |
+
urls = _URLS[self.config.name]
|
| 167 |
+
data_dir = dl_manager.download_and_extract(urls)
|
| 168 |
+
with open(os.path.join(data_dir, "train.csv")) as f:
|
| 169 |
+
files_train = [{'id': row['id'], 'filepath_txt': os.path.join(data_dir, row['filepath_txt']),
|
| 170 |
+
'filepath_img': os.path.join(data_dir, row['filepath_img'])} for row in
|
| 171 |
+
csv.DictReader(f, skipinitialspace=True)]
|
| 172 |
+
with open(os.path.join(data_dir, "test.csv")) as f:
|
| 173 |
+
files_test = [{'id': row['id'], 'filepath_txt': os.path.join(data_dir, row['filepath_txt']),
|
| 174 |
+
'filepath_img': os.path.join(data_dir, row['filepath_img'])} for row in
|
| 175 |
+
csv.DictReader(f, skipinitialspace=True)]
|
| 176 |
+
with open(os.path.join(data_dir, "validation.csv")) as f:
|
| 177 |
+
files_validation = [{'id': row['id'], 'filepath_txt': os.path.join(data_dir, row['filepath_txt']),
|
| 178 |
+
'filepath_img': os.path.join(data_dir, row['filepath_img'])} for row in
|
| 179 |
+
csv.DictReader(f, skipinitialspace=True)]
|
| 180 |
+
return [
|
| 181 |
+
datasets.SplitGenerator(
|
| 182 |
+
name=datasets.Split.TRAIN,
|
| 183 |
+
# These kwargs will be passed to _generate_examples
|
| 184 |
+
gen_kwargs={
|
| 185 |
+
"filepath": files_train,
|
| 186 |
+
"split": "train",
|
| 187 |
+
},
|
| 188 |
+
),
|
| 189 |
+
datasets.SplitGenerator(
|
| 190 |
+
name=datasets.Split.VALIDATION,
|
| 191 |
+
# These kwargs will be passed to _generate_examples
|
| 192 |
+
gen_kwargs={
|
| 193 |
+
"filepath": files_validation,
|
| 194 |
+
"split": "validation",
|
| 195 |
+
},
|
| 196 |
+
),
|
| 197 |
+
datasets.SplitGenerator(
|
| 198 |
+
name=datasets.Split.TEST,
|
| 199 |
+
# These kwargs will be passed to _generate_examples
|
| 200 |
+
gen_kwargs={
|
| 201 |
+
"filepath": files_test,
|
| 202 |
+
"split": "test"
|
| 203 |
+
},
|
| 204 |
+
),
|
| 205 |
+
]
|
| 206 |
+
|
| 207 |
+
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
| 208 |
+
def _generate_examples(self, filepath, split):
|
| 209 |
+
# TODO: This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
|
| 210 |
+
# The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
|
| 211 |
+
#print(filepath)
|
| 212 |
+
for key, f in enumerate(filepath):
|
| 213 |
+
#print(f)
|
| 214 |
+
f_id = f['id']
|
| 215 |
+
f_fp_txt = f['filepath_txt']
|
| 216 |
+
f_fp_img = f['filepath_img']
|
| 217 |
+
tokens = []
|
| 218 |
+
bboxes = []
|
| 219 |
+
rgbs = []
|
| 220 |
+
fonts = []
|
| 221 |
+
labels = []
|
| 222 |
+
|
| 223 |
+
image, size = load_image(f_fp_img, size=224)
|
| 224 |
+
original_image, _ = load_image(f_fp_img)
|
| 225 |
+
|
| 226 |
+
try:
|
| 227 |
+
with open(f_fp_txt, newline='', encoding='utf-8') as csvfile:
|
| 228 |
+
reader = csv.reader(csvfile, delimiter='\t', quotechar=' ')
|
| 229 |
+
for row in reader:
|
| 230 |
+
#if f_id == '121.tar_1606.08710.gz_mutualEnergy_05_77':
|
| 231 |
+
# print(row[0])
|
| 232 |
+
tokens.append(row[0])
|
| 233 |
+
bboxes.append(normalize_bbox(row[1:5], size))
|
| 234 |
+
rgbs.append(row[5:8])
|
| 235 |
+
fonts.append(row[8])
|
| 236 |
+
labels.append(row[9])
|
| 237 |
+
except:
|
| 238 |
+
continue
|
| 239 |
+
|
| 240 |
+
yield key, {
|
| 241 |
+
"id": f_id,
|
| 242 |
+
"tokens": tokens,
|
| 243 |
+
"bboxes": bboxes,
|
| 244 |
+
"RGBs": rgbs,
|
| 245 |
+
"fonts": fonts,
|
| 246 |
+
"image": image,
|
| 247 |
+
"original_image": original_image,
|
| 248 |
+
"labels": labels
|
| 249 |
+
}
|
| 250 |
+
|
| 251 |
+
|