Update dsprites.py
Browse files- dsprites.py +73 -57
dsprites.py
CHANGED
|
@@ -1,79 +1,95 @@
|
|
| 1 |
-
import numpy as np
|
| 2 |
import datasets
|
| 3 |
-
|
|
|
|
| 4 |
|
|
|
|
| 5 |
|
| 6 |
class DSprites(datasets.GeneratorBasedBuilder):
|
| 7 |
-
"""
|
| 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=
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
)
|
| 39 |
|
| 40 |
def _split_generators(self, dl_manager):
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
)
|
| 44 |
return [
|
| 45 |
datasets.SplitGenerator(
|
| 46 |
name=datasets.Split.TRAIN,
|
| 47 |
-
gen_kwargs={"
|
| 48 |
-
),
|
| 49 |
-
datasets.SplitGenerator(
|
| 50 |
-
name=datasets.Split.TEST,
|
| 51 |
-
gen_kwargs={"archive": archive, "split": "test"},
|
| 52 |
),
|
| 53 |
]
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
images =
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
train_indices, test_indices = train_test_split(indices, test_size=0.3, random_state=42)
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
elif split == "test":
|
| 68 |
-
selected_indices = test_indices
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
"
|
| 73 |
-
"
|
| 74 |
-
"
|
| 75 |
-
"
|
| 76 |
-
"
|
| 77 |
-
"
|
| 78 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
}
|
|
|
|
|
|
|
| 1 |
import datasets
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
|
| 5 |
+
_DSPRITES_URL = "https://github.com/deepmind/dsprites-dataset/raw/master/dsprites_ndarray_co1sh3sc6or40x32x32_64x64.npz"
|
| 6 |
|
| 7 |
class DSprites(datasets.GeneratorBasedBuilder):
|
| 8 |
+
"""dSprites dataset: 3x6x40x32x32 factor combinations, 64x64 binary images."""
|
| 9 |
|
| 10 |
VERSION = datasets.Version("1.0.0")
|
| 11 |
|
| 12 |
def _info(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
return datasets.DatasetInfo(
|
| 14 |
+
description=(
|
| 15 |
+
"dSprites dataset: procedurally generated 2D shapes dataset with known ground-truth factors, "
|
| 16 |
+
"commonly used for disentangled representation learning. "
|
| 17 |
+
"Factors: color (1), shape (3), scale (6), orientation (40), position X (32), position Y (32). "
|
| 18 |
+
"Images are 64x64 binary black-and-white."
|
| 19 |
+
),
|
| 20 |
+
features=datasets.Features(
|
| 21 |
+
{
|
| 22 |
+
"image": datasets.Image(), # (64, 64), grayscale
|
| 23 |
+
"index": datasets.Value("int32"), # index of the image
|
| 24 |
+
"label": datasets.Sequence(datasets.Value("int32")), # 6 factor indices (classes)
|
| 25 |
+
"label_values": datasets.Sequence(datasets.Value("float32")), # 6 factor continuous values
|
| 26 |
+
"color": datasets.Value("int32"), # color index (always 0)
|
| 27 |
+
"shape": datasets.Value("int32"), # shape index (0-2)
|
| 28 |
+
"scale": datasets.Value("int32"), # scale index (0-5)
|
| 29 |
+
"orientation": datasets.Value("int32"), # orientation index (0-39)
|
| 30 |
+
"posX": datasets.Value("int32"), # posX index (0-31)
|
| 31 |
+
"posY": datasets.Value("int32"), # posY index (0-31)
|
| 32 |
+
"colorValue": datasets.Value("float64"), # color index (always 0)
|
| 33 |
+
"shapeValue": datasets.Value("float64"), # shape index (0-2)
|
| 34 |
+
"scaleValue": datasets.Value("float64"), # scale index (0-5)
|
| 35 |
+
"orientationValue": datasets.Value("float64"), # orientation index (0-39)
|
| 36 |
+
"posXValue": datasets.Value("float64"), # posX index (0-31)
|
| 37 |
+
"posYValue": datasets.Value("float64"), # posY index (0-31)
|
| 38 |
+
}
|
| 39 |
+
),
|
| 40 |
+
supervised_keys=("image", "label"),
|
| 41 |
+
homepage="https://github.com/google-deepmind/dsprites-dataset",
|
| 42 |
+
license="zlib/libpng License",
|
| 43 |
+
citation="""@inproceedings{higgins2017beta,
|
| 44 |
+
title={beta-vae: Learning basic visual concepts with a constrained variational framework},
|
| 45 |
+
author={Higgins, Irina and Matthey, Loic and Pal, Arka and Burgess, Christopher and Glorot, Xavier and Botvinick, Matthew and Mohamed, Shakir and Lerchner, Alexander},
|
| 46 |
+
booktitle={International conference on learning representations},
|
| 47 |
+
year={2017}
|
| 48 |
+
}""",
|
| 49 |
)
|
| 50 |
|
| 51 |
def _split_generators(self, dl_manager):
|
| 52 |
+
npz_path = dl_manager.download(_DSPRITES_URL)
|
| 53 |
+
|
|
|
|
| 54 |
return [
|
| 55 |
datasets.SplitGenerator(
|
| 56 |
name=datasets.Split.TRAIN,
|
| 57 |
+
gen_kwargs={"npz_path": npz_path},
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
),
|
| 59 |
]
|
| 60 |
|
| 61 |
+
def _generate_examples(self, npz_path):
|
| 62 |
+
# Load npz
|
| 63 |
+
data = np.load(npz_path, allow_pickle=True)
|
| 64 |
+
images = data["imgs"] # shape: (737280, 64, 64), uint8
|
| 65 |
+
latents_classes = data["latents_classes"] # shape: (737280, 6), int64
|
| 66 |
+
latents_values = data["latents_values"] # shape: (737280, 6), float64
|
| 67 |
+
|
| 68 |
+
# Iterate over images
|
| 69 |
+
for idx in range(len(images)):
|
| 70 |
+
img = images[idx] # (64, 64), uint8
|
| 71 |
|
| 72 |
+
# Convert to PIL image, keep grayscale mode
|
| 73 |
+
img_pil = Image.fromarray(img, mode="L")
|
|
|
|
| 74 |
|
| 75 |
+
factors_classes = latents_classes[idx].tolist() # [color_idx, shape_idx, scale_idx, orientation_idx, posX_idx, posY_idx]
|
| 76 |
+
factors_values = latents_values[idx].tolist()
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
yield idx, {
|
| 79 |
+
"image": img_pil,
|
| 80 |
+
"index": idx,
|
| 81 |
+
"label": factors_classes,
|
| 82 |
+
"label_values": factors_values,
|
| 83 |
+
"color": factors_classes[0], # always 0
|
| 84 |
+
"shape": factors_classes[1],
|
| 85 |
+
"scale": factors_classes[2],
|
| 86 |
+
"orientation": factors_classes[3],
|
| 87 |
+
"posX": factors_classes[4],
|
| 88 |
+
"posY": factors_classes[5],
|
| 89 |
+
"colorValue": factors_values[0], # always 0.0
|
| 90 |
+
"shapeValue": factors_values[1],
|
| 91 |
+
"scaleValue": factors_values[2],
|
| 92 |
+
"orientationValue": factors_values[3],
|
| 93 |
+
"posXValue": factors_values[4],
|
| 94 |
+
"posYValue": factors_values[5],
|
| 95 |
}
|