Upload RDD_2020.py
Browse files- RDD_2020.py +153 -0
RDD_2020.py
ADDED
@@ -0,0 +1,153 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# TODO: Address all TODOs and remove all explanatory comments
|
2 |
+
"""TODO: Add a description here."""
|
3 |
+
|
4 |
+
import csv
|
5 |
+
import json
|
6 |
+
import os
|
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
|
15 |
+
_CITATION = """\
|
16 |
+
@InProceedings{huggingface:dataset,
|
17 |
+
title = {A great new dataset},
|
18 |
+
author={Shixuan An
|
19 |
+
},
|
20 |
+
year={2024}
|
21 |
+
}
|
22 |
+
"""
|
23 |
+
|
24 |
+
# TODO: Add description of the dataset here
|
25 |
+
# You can copy an official description
|
26 |
+
_DESCRIPTION = """\
|
27 |
+
This new dataset is designed to solve this great NLP task and is crafted with a lot of care.
|
28 |
+
"""
|
29 |
+
|
30 |
+
# TODO: Add a link to an official homepage for the dataset here
|
31 |
+
_HOMEPAGE = ""
|
32 |
+
|
33 |
+
# TODO: Add the licence for the dataset here if you can find it
|
34 |
+
_LICENSE = ""
|
35 |
+
|
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 |
+
|