File size: 2,751 Bytes
3b60118
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import csv

import datasets
from datasets import DatasetDict

LABELS = {"aerial", "interior", "exterior", "upshot", "skyline", "night"}

_DATA_URL = {
    "train": [f"data/train_images.tar.gz" for i in range(5)],
    "validation": ["data/validation_images.tar.gz"],
    "test": ["data/test_images.tar.gz"],
}


class Sample(datasets.GeneratorBasedBuilder):
    DEFAULT_WRITER_BATCH_SIZE = 1000

    def _info(self):
        return datasets.DatasetInfo(
            description="A sample dataset to illustrate how to use HF APIs",
            features=datasets.Features(
                {
                    "image": datasets.Image(),
                    "label": datasets.ClassLabel(names=list(LABELS)),
                }
            ),
            homepage="github.com/SOM-Enterprise/hf-dataset-sample-representation",
            citation="None",
            task_templates=[
                datasets.ImageClassification(image_column="image", label_column="label")],
        )

    def _split_generators(self, dl_manager):
        archives = dl_manager.download(_DATA_URL)
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "archives": [dl_manager.iter_archive(archive) for archive in archives["train"]],
                    "split": "train",
                }
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "archives": [dl_manager.iter_archive(archive) for archive in archives["validation"]],
                    "split": "validation",
                }
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "archives": [dl_manager.iter_archive(archive) for archive in archives["test"]],
                    "split": "test",
                }
            )
        ]

    def _generate_examples(self, archives, split):
        labels_dict = {}
        with open('metadata.csv', newline='') as csvfile:
            reader = csv.DictReader(csvfile)
            for row in reader:
                labels_dict[row['id']] = set(row['label'].split('|'))

        idx = 0
        for archive in archives:
            for path, file in archive:
                if path.endswith(".jpeg"):
                    if split != "test":
                        labels = labels_dict.get(path.split('/')[-1])
                        label = labels if labels else ['']
                    else:
                        label = -1
                    ex = {"image": {"path": path, "bytes": file.read()}, "label": label}
                    yield idx, ex
                    idx += 1