sombuck commited on
Commit
3b60118
1 Parent(s): 764e503

Create sample.py

Browse files
Files changed (1) hide show
  1. sample.py +77 -0
sample.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import csv
2
+
3
+ import datasets
4
+ from datasets import DatasetDict
5
+
6
+ LABELS = {"aerial", "interior", "exterior", "upshot", "skyline", "night"}
7
+
8
+ _DATA_URL = {
9
+ "train": [f"data/train_images.tar.gz" for i in range(5)],
10
+ "validation": ["data/validation_images.tar.gz"],
11
+ "test": ["data/test_images.tar.gz"],
12
+ }
13
+
14
+
15
+ class Sample(datasets.GeneratorBasedBuilder):
16
+ DEFAULT_WRITER_BATCH_SIZE = 1000
17
+
18
+ def _info(self):
19
+ return datasets.DatasetInfo(
20
+ description="A sample dataset to illustrate how to use HF APIs",
21
+ features=datasets.Features(
22
+ {
23
+ "image": datasets.Image(),
24
+ "label": datasets.ClassLabel(names=list(LABELS)),
25
+ }
26
+ ),
27
+ homepage="github.com/SOM-Enterprise/hf-dataset-sample-representation",
28
+ citation="None",
29
+ task_templates=[
30
+ datasets.ImageClassification(image_column="image", label_column="label")],
31
+ )
32
+
33
+ def _split_generators(self, dl_manager):
34
+ archives = dl_manager.download(_DATA_URL)
35
+ return [
36
+ datasets.SplitGenerator(
37
+ name=datasets.Split.TRAIN,
38
+ gen_kwargs={
39
+ "archives": [dl_manager.iter_archive(archive) for archive in archives["train"]],
40
+ "split": "train",
41
+ }
42
+ ),
43
+ datasets.SplitGenerator(
44
+ name=datasets.Split.VALIDATION,
45
+ gen_kwargs={
46
+ "archives": [dl_manager.iter_archive(archive) for archive in archives["validation"]],
47
+ "split": "validation",
48
+ }
49
+ ),
50
+ datasets.SplitGenerator(
51
+ name=datasets.Split.TEST,
52
+ gen_kwargs={
53
+ "archives": [dl_manager.iter_archive(archive) for archive in archives["test"]],
54
+ "split": "test",
55
+ }
56
+ )
57
+ ]
58
+
59
+ def _generate_examples(self, archives, split):
60
+ labels_dict = {}
61
+ with open('metadata.csv', newline='') as csvfile:
62
+ reader = csv.DictReader(csvfile)
63
+ for row in reader:
64
+ labels_dict[row['id']] = set(row['label'].split('|'))
65
+
66
+ idx = 0
67
+ for archive in archives:
68
+ for path, file in archive:
69
+ if path.endswith(".jpeg"):
70
+ if split != "test":
71
+ labels = labels_dict.get(path.split('/')[-1])
72
+ label = labels if labels else ['']
73
+ else:
74
+ label = -1
75
+ ex = {"image": {"path": path, "bytes": file.read()}, "label": label}
76
+ yield idx, ex
77
+ idx += 1