| import datasets |
| import numpy as np |
| from PIL import Image |
|
|
| _DSPRITES_URL = "https://github.com/google-deepmind/dsprites-dataset/raw/refs/heads/master/dsprites_ndarray_co1sh3sc6or40x32y32_64x64.npz" |
|
|
| class DSprites(datasets.GeneratorBasedBuilder): |
| """dSprites dataset: 3x6x40x32x32 factor combinations, 64x64 binary images.""" |
|
|
| VERSION = datasets.Version("1.0.0") |
|
|
| def _info(self): |
| return datasets.DatasetInfo( |
| description=( |
| "Noisy dSprites dataset: procedurally generated 2D shapes dataset with known ground-truth factors, " |
| "commonly used for disentangled representation learning. " |
| "This is the NoisyDSprites variant, where each background is randomly noised, " |
| "while background remains black. " |
| "Factors: color (1), shape (3), scale (6), orientation (40), position X (32), position Y (32). " |
| "Images are 64x64 RGB." |
| ), |
| features=datasets.Features( |
| { |
| "image": datasets.Image(), |
| "index": datasets.Value("int32"), |
| "label": datasets.Sequence(datasets.Value("int32")), |
| "label_values": datasets.Sequence(datasets.Value("float32")), |
| "color": datasets.Value("int32"), |
| "shape": datasets.Value("int32"), |
| "scale": datasets.Value("int32"), |
| "orientation": datasets.Value("int32"), |
| "posX": datasets.Value("int32"), |
| "posY": datasets.Value("int32"), |
| "colorValue": datasets.Value("float64"), |
| "shapeValue": datasets.Value("float64"), |
| "scaleValue": datasets.Value("float64"), |
| "orientationValue": datasets.Value("float64"), |
| "posXValue": datasets.Value("float64"), |
| "posYValue": datasets.Value("float64"), |
| } |
| ), |
| supervised_keys=("image", "label"), |
| homepage="https://github.com/google-research/disentanglement_lib/tree/master", |
| license="apache-2.0", |
| citation="""@inproceedings{locatello2019challenging, |
| title={Challenging Common Assumptions in the Unsupervised Learning of Disentangled Representations}, |
| author={Locatello, Francesco and Bauer, Stefan and Lucic, Mario and Raetsch, Gunnar and Gelly, Sylvain and Sch{\"o}lkopf, Bernhard and Bachem, Olivier}, |
| booktitle={International Conference on Machine Learning}, |
| pages={4114--4124}, |
| year={2019} |
| }""", |
| ) |
|
|
| def _split_generators(self, dl_manager): |
| npz_path = dl_manager.download(_DSPRITES_URL) |
|
|
| return [ |
| datasets.SplitGenerator( |
| name=datasets.Split.TRAIN, |
| gen_kwargs={"npz_path": npz_path}, |
| ), |
| ] |
|
|
| def _generate_examples(self, npz_path): |
| |
| data = np.load(npz_path, allow_pickle=True) |
| images = data["imgs"] |
| latents_classes = data["latents_classes"] |
| latents_values = data["latents_values"] |
|
|
| |
| for idx in range(len(images)): |
| img = images[idx] |
| img = img.astype(np.float32) / 1.0 |
| noise = np.random.uniform(0, 1, size=(64, 64, 3)) |
| img_rgb = np.minimum(img[..., None] + noise, 1.0) * 255 |
| img_pil = Image.fromarray(img_rgb.astype(np.uint8), mode="RGB") |
|
|
| factors_classes = latents_classes[idx].tolist() |
| factors_values = latents_values[idx].tolist() |
|
|
| yield idx, { |
| "image": img_pil, |
| "index": idx, |
| "label": factors_classes, |
| "label_values": factors_values, |
| "color": factors_classes[0], |
| "shape": factors_classes[1], |
| "scale": factors_classes[2], |
| "orientation": factors_classes[3], |
| "posX": factors_classes[4], |
| "posY": factors_classes[5], |
| "colorValue": factors_values[0], |
| "shapeValue": factors_values[1], |
| "scaleValue": factors_values[2], |
| "orientationValue": factors_values[3], |
| "posXValue": factors_values[4], |
| "posYValue": factors_values[5], |
| } |
|
|