Update hugging_face.py
Browse files- hugging_face.py +97 -50
hugging_face.py
CHANGED
@@ -7,6 +7,8 @@ import os
|
|
7 |
from typing import List
|
8 |
import datasets
|
9 |
import logging
|
|
|
|
|
10 |
|
11 |
# TODO: Add BibTeX citation
|
12 |
# Find for instance the citation on arxiv or on the dataset repo/website
|
@@ -34,73 +36,118 @@ _LICENSE = ""
|
|
34 |
# TODO: Add link to the official dataset URLs here
|
35 |
# The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
|
36 |
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
|
37 |
-
_URL = "https://data.mendeley.com/datasets/5ty2wb6gvg/1"
|
38 |
_URLS = {
|
39 |
-
"
|
40 |
-
"dev": _URL + "dev-v1.1.json",
|
41 |
}
|
42 |
|
43 |
|
44 |
# TODO: Name of the dataset usually matches the script name with CamelCase instead of snake_case
|
45 |
-
class
|
46 |
"""TODO: Short description of my dataset."""
|
47 |
|
48 |
_URLS = _URLS
|
49 |
VERSION = datasets.Version("1.1.0")
|
50 |
|
51 |
def _info(self):
|
52 |
-
raise ValueError('woops!')
|
53 |
return datasets.DatasetInfo(
|
54 |
description=_DESCRIPTION,
|
55 |
-
features=datasets.Features(
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
"
|
61 |
-
"
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
70 |
citation=_CITATION,
|
71 |
)
|
72 |
|
73 |
-
def _split_generators(self, dl_manager
|
74 |
-
|
75 |
-
|
76 |
|
77 |
return [
|
78 |
-
datasets.SplitGenerator(
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
]
|
81 |
|
82 |
-
def _generate_examples(self,
|
83 |
-
"""
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
from typing import List
|
8 |
import datasets
|
9 |
import logging
|
10 |
+
import xml.etree.ElementTree as ET
|
11 |
+
import os
|
12 |
|
13 |
# TODO: Add BibTeX citation
|
14 |
# Find for instance the citation on arxiv or on the dataset repo/website
|
|
|
36 |
# TODO: Add link to the official dataset URLs here
|
37 |
# The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
|
38 |
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
|
|
|
39 |
_URLS = {
|
40 |
+
"dataset": "https://prod-dcd-datasets-cache-zipfiles.s3.eu-west-1.amazonaws.com/5ty2wb6gvg-1.zip"
|
|
|
41 |
}
|
42 |
|
43 |
|
44 |
# TODO: Name of the dataset usually matches the script name with CamelCase instead of snake_case
|
45 |
+
class RDD2020_Dataset(datasets.GeneratorBasedBuilder):
|
46 |
"""TODO: Short description of my dataset."""
|
47 |
|
48 |
_URLS = _URLS
|
49 |
VERSION = datasets.Version("1.1.0")
|
50 |
|
51 |
def _info(self):
|
|
|
52 |
return datasets.DatasetInfo(
|
53 |
description=_DESCRIPTION,
|
54 |
+
features=datasets.Features({
|
55 |
+
"image_id": datasets.Value("string"),
|
56 |
+
"country": datasets.Value("string"),
|
57 |
+
"type": datasets.Value("string"),
|
58 |
+
"image_resolution": datasets.Features({
|
59 |
+
"width": datasets.Value("int32"),
|
60 |
+
"height": datasets.Value("int32"),
|
61 |
+
"depth": datasets.Value("int32"),
|
62 |
+
}),
|
63 |
+
"image_path": datasets.Value("string"),
|
64 |
+
"pics_array": datasets.Array3D(shape=(None, None, 3), dtype="uint8"),
|
65 |
+
"crack_type": datasets.Sequence(datasets.Value("string")),
|
66 |
+
"crack_coordinates": datasets.Sequence(datasets.Features({
|
67 |
+
"x_min": datasets.Value("int32"),
|
68 |
+
"x_max": datasets.Value("int32"),
|
69 |
+
"y_min": datasets.Value("int32"),
|
70 |
+
"y_max": datasets.Value("int32"),
|
71 |
+
})),
|
72 |
+
}),
|
73 |
+
homepage='https://data.mendeley.com/datasets/5ty2wb6gvg/1',
|
74 |
citation=_CITATION,
|
75 |
)
|
76 |
|
77 |
+
def _split_generators(self, dl_manager):
|
78 |
+
"""This method downloads/extracts the data and defines the splits."""
|
79 |
+
data_dir = dl_manager.download_and_extract(_URLS["dataset"])
|
80 |
|
81 |
return [
|
82 |
+
datasets.SplitGenerator(
|
83 |
+
name=datasets.Split.TRAIN,
|
84 |
+
gen_kwargs={
|
85 |
+
"images_dir": os.path.join(data_dir, "train"),
|
86 |
+
"annotations_dir": os.path.join(data_dir, "train", "annotations"),
|
87 |
+
"split": "train",
|
88 |
+
},
|
89 |
+
),
|
90 |
+
datasets.SplitGenerator(
|
91 |
+
name=datasets.Split.TEST,
|
92 |
+
gen_kwargs={
|
93 |
+
"images_dir": os.path.join(data_dir, "test1"),
|
94 |
+
"annotations_dir": os.path.join(data_dir, "test1", "annotations"),
|
95 |
+
"split": "test1",
|
96 |
+
},
|
97 |
+
),
|
98 |
+
datasets.SplitGenerator(
|
99 |
+
name=datasets.Split.TEST,
|
100 |
+
gen_kwargs={
|
101 |
+
"images_dir": os.path.join(data_dir, "test2"),
|
102 |
+
"annotations_dir": os.path.join(data_dir, "test2", "annotations"),
|
103 |
+
"split": "test2",
|
104 |
+
},
|
105 |
+
),
|
106 |
]
|
107 |
|
108 |
+
def _generate_examples(self, images_dir, annotations_dir, split):
|
109 |
+
"""Yields examples as (key, example) tuples."""
|
110 |
+
for image_file in os.listdir(images_dir):
|
111 |
+
if not image_file.endswith('.jpg'):
|
112 |
+
continue
|
113 |
+
image_id = image_file.split('.')[0]
|
114 |
+
annotation_file = image_id + '.xml'
|
115 |
+
annotation_path = os.path.join(annotations_dir, annotation_file)
|
116 |
+
|
117 |
+
if not os.path.exists(annotation_path):
|
118 |
+
continue
|
119 |
+
|
120 |
+
tree = ET.parse(annotation_path)
|
121 |
+
root = tree.getroot()
|
122 |
+
|
123 |
+
country = split.capitalize()
|
124 |
+
image_path = os.path.join(images_dir, image_file)
|
125 |
+
crack_type = []
|
126 |
+
crack_coordinates = []
|
127 |
+
|
128 |
+
for obj in root.findall('object'):
|
129 |
+
crack_type.append(obj.find('name').text)
|
130 |
+
bndbox = obj.find('bndbox')
|
131 |
+
coordinates = {
|
132 |
+
"x_min": int(bndbox.find('xmin').text),
|
133 |
+
"x_max": int(bndbox.find('xmax').text),
|
134 |
+
"y_min": int(bndbox.find('ymin').text),
|
135 |
+
"y_max": int(bndbox.find('ymax').text),
|
136 |
+
}
|
137 |
+
crack_coordinates.append(coordinates)
|
138 |
+
|
139 |
+
# Assuming images are of uniform size, you might want to adjust this or extract from image directly
|
140 |
+
image_resolution = {"width": 600, "height": 600, "depth": 3} if country != "India" else {"width": 720,
|
141 |
+
"height": 720,
|
142 |
+
"depth": 3}
|
143 |
+
yield image_id, {
|
144 |
+
"image_id": image_id,
|
145 |
+
"country": country,
|
146 |
+
"type": split,
|
147 |
+
"image_resolution": image_resolution,
|
148 |
+
"image_path": image_path,
|
149 |
+
"crack_type": crack_type,
|
150 |
+
"crack_coordinates": crack_coordinates,
|
151 |
+
}
|
152 |
+
|
153 |
+
|