| """Cartoonset-10k Data Set""" |
|
|
|
|
| from io import BytesIO |
| from typing import Optional |
|
|
| import tarfile |
| import pandas as pd |
|
|
|
|
| import datasets |
|
|
|
|
| _CITATION = r""" |
| @article{DBLP:journals/corr/abs-1711-05139, |
| author = {Amelie Royer and |
| Konstantinos Bousmalis and |
| Stephan Gouws and |
| Fred Bertsch and |
| Inbar Mosseri and |
| Forrester Cole and |
| Kevin Murphy}, |
| title = {{XGAN:} Unsupervised Image-to-Image Translation for many-to-many Mappings}, |
| journal = {CoRR}, |
| volume = {abs/1711.05139}, |
| year = {2017}, |
| url = {http://arxiv.org/abs/1711.05139}, |
| eprinttype = {arXiv}, |
| eprint = {1711.05139}, |
| timestamp = {Mon, 13 Aug 2018 16:47:38 +0200}, |
| biburl = {https://dblp.org/rec/journals/corr/abs-1711-05139.bib}, |
| bibsource = {dblp computer science bibliography, https://dblp.org} |
| } |
| """ |
|
|
| _DESCRIPTION = """\ |
| Cartoon Set is a collection of random, 2D cartoon avatar images. The cartoons vary in 10 artwork |
| categories, 4 color categories, and 4 proportion categories, with a total of ~1013 possible |
| combinations. We provide sets of 10k and 100k randomly chosen cartoons and labeled attributes. |
| """ |
|
|
| _DATA_URLS = { |
| "10k": "https://huggingface.co/datasets/cgarciae/cartoonset/resolve/1.0.0/data/cartoonset10k.tgz", |
| "100k": "https://huggingface.co/datasets/cgarciae/cartoonset/resolve/1.0.0/data/cartoonset100k.tgz", |
| } |
|
|
|
|
| class Cartoonset(datasets.GeneratorBasedBuilder): |
| """Cartoonset-10k Data Set""" |
|
|
| BUILDER_CONFIGS = [ |
| datasets.BuilderConfig( |
| name="10k", |
| version=datasets.Version("1.0.0", ""), |
| description="Loads the Cartoonset-10k Data Set (images only).", |
| ), |
| datasets.BuilderConfig( |
| name="10k+features", |
| version=datasets.Version("1.0.0", ""), |
| description="Loads the Cartoonset-10k Data Set (images and attributes).", |
| ), |
| datasets.BuilderConfig( |
| name="100k", |
| version=datasets.Version("1.0.0", ""), |
| description="Loads the Cartoonset-100k Data Set (images only).", |
| ), |
| datasets.BuilderConfig( |
| name="100k+features", |
| version=datasets.Version("1.0.0", ""), |
| description="Loads the Cartoonset-100k Data Set (images and attributes).", |
| ), |
| ] |
|
|
| DEFAULT_CONFIG_NAME = "10k" |
|
|
| def _info(self): |
| features = {"img_bytes": datasets.Value("binary")} |
|
|
| if self.config.name.endswith("+features"): |
| features.update( |
| { |
| "eye_angle": datasets.Value("int32"), |
| "eye_angle_num_categories": datasets.Value("int32"), |
| "eye_lashes": datasets.Value("int32"), |
| "eye_lashes_num_categories": datasets.Value("int32"), |
| "eye_lid": datasets.Value("int32"), |
| "eye_lid_num_categories": datasets.Value("int32"), |
| "chin_length": datasets.Value("int32"), |
| "chin_length_num_categories": datasets.Value("int32"), |
| "eyebrow_weight": datasets.Value("int32"), |
| "eyebrow_weight_num_categories": datasets.Value("int32"), |
| "eyebrow_shape": datasets.Value("int32"), |
| "eyebrow_shape_num_categories": datasets.Value("int32"), |
| "eyebrow_thickness": datasets.Value("int32"), |
| "eyebrow_thickness_num_categories": datasets.Value("int32"), |
| "face_shape": datasets.Value("int32"), |
| "face_shape_num_categories": datasets.Value("int32"), |
| "facial_hair": datasets.Value("int32"), |
| "facial_hair_num_categories": datasets.Value("int32"), |
| "hair": datasets.Value("int32"), |
| "hair_num_categories": datasets.Value("int32"), |
| "eye_color": datasets.Value("int32"), |
| "eye_color_num_categories": datasets.Value("int32"), |
| "face_color": datasets.Value("int32"), |
| "face_color_num_categories": datasets.Value("int32"), |
| "hair_color": datasets.Value("int32"), |
| "hair_color_num_categories": datasets.Value("int32"), |
| "glasses": datasets.Value("int32"), |
| "glasses_num_categories": datasets.Value("int32"), |
| "glasses_color": datasets.Value("int32"), |
| "glasses_color_num_categories": datasets.Value("int32"), |
| "eye_slant": datasets.Value("int32"), |
| "eye_slant_num_categories": datasets.Value("int32"), |
| "eyebrow_width": datasets.Value("int32"), |
| "eyebrow_width_num_categories": datasets.Value("int32"), |
| "eye_eyebrow_distance": datasets.Value("int32"), |
| "eye_eyebrow_distance_num_categories": datasets.Value("int32"), |
| } |
| ) |
|
|
| return datasets.DatasetInfo( |
| description=_DESCRIPTION, |
| features=datasets.Features(features), |
| supervised_keys=("img_bytes",), |
| homepage="https://www.cs.toronto.edu/~kriz/cifar.html", |
| citation=_CITATION, |
| ) |
|
|
| def _split_generators(self, dl_manager: datasets.DownloadManager): |
|
|
| url = _DATA_URLS[self.config.name.replace("+features", "")] |
| archive = dl_manager.download(url) |
|
|
| return [ |
| datasets.SplitGenerator( |
| name=datasets.Split.TRAIN, |
| gen_kwargs={ |
| "files": dl_manager.iter_archive(archive), |
| "split": "train", |
| }, |
| ), |
| ] |
|
|
| def _generate_examples(self, files, split): |
| """This function returns the examples in the raw (text) form.""" |
|
|
| if self.config.name.endswith("+features"): |
| return self._generate_examples_with_features(files, split) |
| else: |
| return self._generate_examples_without_features(files, split) |
|
|
| def _generate_examples_without_features(self, files, split): |
| path: str |
| file_obj: tarfile.ExFileObject |
| root: str |
| for path, file_obj in files: |
| root = path[:-4] |
|
|
| if path.endswith(".png"): |
| image = file_obj.read() |
|
|
| yield root, {"img_bytes": image} |
|
|
| def _generate_examples_with_features(self, files, split): |
| path: str |
| file_obj: tarfile.ExFileObject |
| outputs = {} |
| root: Optional[str] = None |
| for path, file_obj in files: |
| root = path[:-4] |
|
|
| if root not in outputs: |
| outputs[root] = {} |
|
|
| current_output = outputs[root] |
|
|
| if path.endswith(".png"): |
| image = file_obj.read() |
|
|
| current_output["img_bytes"] = image |
| else: |
| df = pd.read_csv( |
| BytesIO(file_obj.read()), |
| header=None, |
| names=["feature", "value", "num_categories"], |
| ) |
|
|
| for index, row in df.iterrows(): |
| current_output[row.feature] = row.value |
| current_output[f"{row.feature}_num_categories"] = row.num_categories |
|
|
| if "img_bytes" in current_output and len(current_output) > 1: |
| yield root, current_output |
| del outputs[root] |
| root = None |
|
|
| if len(outputs) > 0: |
| raise ValueError( |
| f"Unable to extract the following samples: {list(outputs)}" |
| ) |
|
|