Create dsprites.py
Browse files- dsprites.py +79 -0
dsprites.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import datasets
|
| 3 |
+
from sklearn.model_selection import train_test_split
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class DSprites(datasets.GeneratorBasedBuilder):
|
| 7 |
+
"""TODO: Short description of my dataset."""
|
| 8 |
+
|
| 9 |
+
VERSION = datasets.Version("1.0.0")
|
| 10 |
+
|
| 11 |
+
def _info(self):
|
| 12 |
+
features = datasets.Features(
|
| 13 |
+
{
|
| 14 |
+
"image": datasets.Image(),
|
| 15 |
+
"orientation": datasets.Value("float"),
|
| 16 |
+
"shape": datasets.ClassLabel(names=["square", "ellipse", "heart"]),
|
| 17 |
+
"scale": datasets.Value("float"),
|
| 18 |
+
"color": datasets.ClassLabel(names=["white"]),
|
| 19 |
+
"position_x": datasets.Value("float"),
|
| 20 |
+
"position_y": datasets.Value("float"),
|
| 21 |
+
}
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
homepage = "https://github.com/deepmind/dsprites-dataset"
|
| 25 |
+
license = "zlib/libpng"
|
| 26 |
+
return datasets.DatasetInfo(
|
| 27 |
+
description="""dSprites is a dataset of 2D shapes procedurally generated from 6 ground truth independent latent factors. These factors are color, shape, scale, rotation, x and y positions of a sprite.
|
| 28 |
+
All possible combinations of these latents are present exactly once, generating N = 737280 total images.""",
|
| 29 |
+
features=features,
|
| 30 |
+
supervised_keys=("image", "shape"),
|
| 31 |
+
homepage=homepage,
|
| 32 |
+
license=license,
|
| 33 |
+
citation="""@misc{dsprites17,
|
| 34 |
+
author = {Loic Matthey and Irina Higgins and Demis Hassabis and Alexander Lerchner},
|
| 35 |
+
title = {dSprites: Disentanglement testing Sprites dataset},
|
| 36 |
+
howpublished= {https://github.com/deepmind/dsprites-dataset/},
|
| 37 |
+
year = "2017"}""",
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
def _split_generators(self, dl_manager):
|
| 41 |
+
archive = dl_manager.download(
|
| 42 |
+
"https://github.com/google-deepmind/dsprites-dataset/raw/refs/heads/master/dsprites_ndarray_co1sh3sc6or40x32y32_64x64.npz"
|
| 43 |
+
)
|
| 44 |
+
return [
|
| 45 |
+
datasets.SplitGenerator(
|
| 46 |
+
name=datasets.Split.TRAIN,
|
| 47 |
+
gen_kwargs={"archive": archive, "split": "train"},
|
| 48 |
+
),
|
| 49 |
+
datasets.SplitGenerator(
|
| 50 |
+
name=datasets.Split.TEST,
|
| 51 |
+
gen_kwargs={"archive": archive, "split": "test"},
|
| 52 |
+
),
|
| 53 |
+
]
|
| 54 |
+
|
| 55 |
+
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
| 56 |
+
def _generate_examples(self, archive, split):
|
| 57 |
+
dataset_zip = np.load(archive, allow_pickle=True)
|
| 58 |
+
images = dataset_zip["imgs"]
|
| 59 |
+
latents_values = dataset_zip["latents_values"]
|
| 60 |
+
|
| 61 |
+
# Split the indices for train and test
|
| 62 |
+
indices = np.arange(len(images))
|
| 63 |
+
train_indices, test_indices = train_test_split(indices, test_size=0.3, random_state=42)
|
| 64 |
+
|
| 65 |
+
if split == "train":
|
| 66 |
+
selected_indices = train_indices
|
| 67 |
+
elif split == "test":
|
| 68 |
+
selected_indices = test_indices
|
| 69 |
+
|
| 70 |
+
for key in selected_indices:
|
| 71 |
+
yield int(key), { # Ensure the key is a Python native int
|
| 72 |
+
"image": images[key],
|
| 73 |
+
"color": int(latents_values[key, 0]) - 1,
|
| 74 |
+
"shape": int(latents_values[key, 1]) - 1,
|
| 75 |
+
"scale": latents_values[key, 2],
|
| 76 |
+
"orientation": latents_values[key, 3],
|
| 77 |
+
"position_x": latents_values[key, 4],
|
| 78 |
+
"position_y": latents_values[key, 5],
|
| 79 |
+
}
|