ShixuanAn commited on
Commit
8fde02c
1 Parent(s): f5000d3

Update hugging_face.py

Browse files
Files changed (1) hide show
  1. 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
- "train": _URL + "train-v1.1.json",
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 SquadDataset(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
- raise ValueError('woops!')
53
  return datasets.DatasetInfo(
54
  description=_DESCRIPTION,
55
- features=datasets.Features(
56
- {
57
- "id": datasets.Value("string"),
58
- "title": datasets.Value("string"),
59
- "context": datasets.Value("string"),
60
- "question": datasets.Value("string"),
61
- "answers": datasets.features.Sequence(
62
- {"text": datasets.Value("string"), "answer_start": datasets.Value("int32"), }
63
- ),
64
- }
65
- ),
66
- # No default supervised_keys (as we have to pass both question
67
- # and context as input).
68
- supervised_keys=None,
69
- homepage="https://rajpurkar.github.io/SQuAD-explorer/",
 
 
 
 
 
70
  citation=_CITATION,
71
  )
72
 
73
- def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
74
- urls_to_download = self._URLS
75
- downloaded_files = dl_manager.download_and_extract(urls_to_download)
76
 
77
  return [
78
- datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["train"]}),
79
- datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files["dev"]}),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  ]
81
 
82
- def _generate_examples(self, filepath):
83
- """This function returns the examples in the raw (text) form."""
84
- logging.info("generating examples from = %s", filepath)
85
- with open(filepath) as f:
86
- squad = json.load(f)
87
- for article in squad["data"]:
88
- title = article.get("title", "").strip()
89
- for paragraph in article["paragraphs"]:
90
- context = paragraph["context"].strip()
91
- for qa in paragraph["qas"]:
92
- question = qa["question"].strip()
93
- id_ = qa["id"]
94
-
95
- answer_starts = [answer["answer_start"] for answer in qa["answers"]]
96
- answers = [answer["text"].strip() for answer in qa["answers"]]
97
-
98
- # Features currently used are "context", "question", and "answers".
99
- # Others are extracted here for the ease of future expansions.
100
- yield id_, {
101
- "title": title,
102
- "context": context,
103
- "question": question,
104
- "id": id_,
105
- "answers": {"answer_start": answer_starts, "text": answers, },
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
+