|
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 |
|
|