File size: 2,068 Bytes
39cc14c
 
 
 
 
 
 
 
 
 
 
6ae11b3
39cc14c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from pathlib import Path
from typing import List
import datasets
import pickle

logger = datasets.logging.get_logger(__name__)


class Fairface(datasets.GeneratorBasedBuilder):

    _HOMEPAGE = "https://huggingface.co/datasets/nateraw/fairface/"
    _URL = "https://huggingface.co/datasets/nateraw/fairface/resolve/main/"
    _URLS = {
        "train": _URL + "train.pt",
        "dev": _URL + "val.pt",
    }
    _DESCRIPTION = "The Fairface dataset"
    _CITATION = None

    def _info(self):
        return datasets.DatasetInfo(
            description=self._DESCRIPTION,
            features=datasets.Features(
                {
                    'img_bytes': datasets.Value('binary'),
                    'age': datasets.features.ClassLabel(names=['0-2', '3-9', '10-19', '20-29', '30-39', '40-49', '50-59', '60-69', 'more than 70']),
                    "gender": datasets.features.ClassLabel(names=['Female', 'Male']),
                    'race': datasets.features.ClassLabel(names=['Black', 'East Asian', 'Indian', 'Latino_Hispanic', 'Middle Eastern', 'Southeast Asian', 'White'])
                }
            ),
            supervised_keys=('img_bytes', 'age'),
            homepage=self._HOMEPAGE,
            citation=self._CITATION,
        )
        
    def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
        downloaded_files = dl_manager.download_and_extract(self._URLS)
        return [
            datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["train"]}),
            datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files["dev"]})
        ]

    def _generate_examples(self, filepath):
        """This function returns the examples in the raw (text) form."""
        logger.info("generating examples from = %s", filepath)

        with Path(filepath).open('rb') as f:
            examples = pickle.load(f)

        for i, ex in enumerate(examples):
            _id = ex.pop('_id')
            yield _id, ex