mariosasko commited on
Commit
96b39ed
1 Parent(s): 077dc5b

Make dataset streamable

Browse files
Files changed (1) hide show
  1. illustrated_ads.py +38 -30
illustrated_ads.py CHANGED
@@ -14,10 +14,10 @@
14
  """Dataset of illustrated and non illustrated 19th Century newspaper ads."""
15
 
16
  import ast
 
17
  import pandas as pd
18
  import datasets
19
  from PIL import Image
20
- from pathlib import Path
21
 
22
  # TODO: Add BibTeX citation
23
  # Find for instance the citation on arxiv or on the dataset repo/website
@@ -46,6 +46,19 @@ _LICENSE = "Public Domain"
46
 
47
  _URLS = "https://zenodo.org/record/5838410/files/images.zip?download=1"
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
  # TODO: Name of the dataset usually match the script name with CamelCase instead of snake_case
51
  class IllustratedAds(datasets.GeneratorBasedBuilder):
@@ -94,48 +107,43 @@ class IllustratedAds(datasets.GeneratorBasedBuilder):
94
  )
95
 
96
  def _split_generators(self, dl_manager):
97
- data_dir = dl_manager.download_and_extract(_URLS)
98
- return [
99
- datasets.SplitGenerator(
100
- name=datasets.Split.TRAIN,
101
- gen_kwargs={
102
- "data_dir": Path(data_dir),
103
- },
104
- ),
105
- ]
106
-
107
- def _generate_examples(self, data_dir):
108
- dtypes = {
109
- "page_seq_num": "int64",
110
- "edition_seq_num": "int64",
111
- "batch": "string",
112
- "lccn": "string",
113
- "score": "float64",
114
- "place_of_publication": "string",
115
- "name": "string",
116
- "publisher": "string",
117
- "url": "string",
118
- "page_url": "string",
119
- }
120
  df_labels = pd.read_csv(
121
- "https://zenodo.org/record/5838410/files/ads.csv?download=1", index_col=0
122
  )
123
  df_metadata = pd.read_csv(
124
- "https://zenodo.org/record/5838410/files/sample.csv?download=1",
125
  index_col=0,
126
- dtype=dtypes,
127
  )
128
  df_metadata["file"] = df_metadata.filepath.str.replace("/", "_")
129
  df_metadata = df_metadata.set_index("file", drop=True)
130
  df = df_labels.join(df_metadata)
131
  df = df.reset_index()
132
- data = df.to_dict(orient="records")
133
- for id_, row in enumerate(data):
 
 
 
 
 
 
 
 
 
 
 
134
  box = ast.literal_eval(row["box"])
135
  row["box"] = box
136
  row.pop("filepath")
137
  ocr = " ".join(ast.literal_eval(row["ocr"]))
138
  row["ocr"] = ocr
139
  image = row["file"]
140
- row["image"] = Image.open(Path(data_dir / image))
141
  yield id_, row
 
14
  """Dataset of illustrated and non illustrated 19th Century newspaper ads."""
15
 
16
  import ast
17
+ import os
18
  import pandas as pd
19
  import datasets
20
  from PIL import Image
 
21
 
22
  # TODO: Add BibTeX citation
23
  # Find for instance the citation on arxiv or on the dataset repo/website
 
46
 
47
  _URLS = "https://zenodo.org/record/5838410/files/images.zip?download=1"
48
 
49
+ _DTYPES = {
50
+ "page_seq_num": "int64",
51
+ "edition_seq_num": "int64",
52
+ "batch": "string",
53
+ "lccn": "string",
54
+ "score": "float64",
55
+ "place_of_publication": "string",
56
+ "name": "string",
57
+ "publisher": "string",
58
+ "url": "string",
59
+ "page_url": "string",
60
+ }
61
+
62
 
63
  # TODO: Name of the dataset usually match the script name with CamelCase instead of snake_case
64
  class IllustratedAds(datasets.GeneratorBasedBuilder):
 
107
  )
108
 
109
  def _split_generators(self, dl_manager):
110
+ images = dl_manager.download_and_extract(_URLS)
111
+ annotations = dl_manager.download(
112
+ [
113
+ "https://zenodo.org/record/5838410/files/ads.csv?download=1",
114
+ "https://zenodo.org/record/5838410/files/sample.csv?download=1"
115
+ ]
116
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  df_labels = pd.read_csv(
118
+ annotations[0], index_col=0
119
  )
120
  df_metadata = pd.read_csv(
121
+ annotations[1],
122
  index_col=0,
123
+ dtype=_DTYPES,
124
  )
125
  df_metadata["file"] = df_metadata.filepath.str.replace("/", "_")
126
  df_metadata = df_metadata.set_index("file", drop=True)
127
  df = df_labels.join(df_metadata)
128
  df = df.reset_index()
129
+ annotations = df.to_dict(orient="records")
130
+ return [
131
+ datasets.SplitGenerator(
132
+ name=datasets.Split.TRAIN,
133
+ gen_kwargs={
134
+ "images": images,
135
+ "annotations": annotations,
136
+ },
137
+ ),
138
+ ]
139
+
140
+ def _generate_examples(self, images, annotations):
141
+ for id_, row in enumerate(annotations):
142
  box = ast.literal_eval(row["box"])
143
  row["box"] = box
144
  row.pop("filepath")
145
  ocr = " ".join(ast.literal_eval(row["ocr"]))
146
  row["ocr"] = ocr
147
  image = row["file"]
148
+ row["image"] = os.path.join(images, image)
149
  yield id_, row