File size: 2,292 Bytes
a4597a8
 
 
 
 
7d758e3
a4597a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8aa009d
a4597a8
 
 
 
 
 
 
 
 
 
 
 
2dfe820
a4597a8
 
 
 
 
 
 
 
 
 
 
 
 
 
7d758e3
a4597a8
 
9766e1e
455ae37
a4597a8
2653b8a
a4597a8
68ca385
 
a4597a8
 
ffb85f9
 
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
# coding=utf-8
import os
import datasets
import joblib
from pathlib import Path
from tqdm import tqdm


_BASE_HF_URL = Path("./data")
_CITATION = ""
_HOMEPAGE = ""
_DESCRIPTION = ""
_DATA_URL = {
    "train": [_BASE_HF_URL/"images.tar.gz"]
}


class AVA(datasets.GeneratorBasedBuilder):
    VERSION = datasets.Version("1.0.0")

    DEFAULT_WRITER_BATCH_SIZE = 1000

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "image": datasets.Image(),
                    "filename": datasets.Value("string"),
                    "rating_counts": datasets.features.Sequence(datasets.Value("int32")),
                    "text_tag_0": datasets.Value("string"),
                    "text_tag_1": datasets.Value("string")
                }
            ),
            homepage=_HOMEPAGE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""
        archives = dl_manager.download(_DATA_URL)
        self.dict_metadata = joblib.load(Path(dl_manager.download_and_extract(_BASE_HF_URL/ "metadata.pkl")))
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "archives": [dl_manager.iter_archive(archive) for archive in archives["train"]],
                    "split": "train",
                },
            )
            ]

    def _generate_examples(self, archives, split):
        """Yields examples."""
        idx = 0
        for archive in archives:
            for path, file in tqdm(archive):
                if path.endswith(".jpg"):
                    # image filepath format: <IMAGE_FILE NAME>_<SYNSET_ID>.JPEG
                    _id = int(os.path.splitext(path)[0].split('/')[-1])
                    _metadata = self.dict_metadata[_id]
                    ex = {"image": {"path": path, "bytes": file.read()}, 
                          "filename": str(path).split('/')[-1],
                          "rating_counts": _metadata[0],
                          "text_tag_0":_metadata[1],
                          "text_tag_1": _metadata[2]}
                    yield idx, ex
                    idx += 1