haodoz0118 commited on
Commit
a864039
·
verified ·
1 Parent(s): 72f78dd

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +112 -0
  2. animation0.gif +3 -0
  3. dsprites-color.py +100 -0
README.md ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ ---
4
+ # Dataset Card for Color dSprites
5
+
6
+ ## Dataset Description
7
+
8
+ The **Color dSprites dataset** is a **synthetic 2D shapes dataset** designed for benchmarking algorithms in **disentangled representation learning** and **unsupervised representation learning**.
9
+
10
+ It is a variant of the original **dSprites dataset** introduced in the β-VAE paper. In this version, each object is randomly colored per sample, while the background remains black. This allows researchers to evaluate **robustness to color variation** and assess how well models can learn disentangled representations under color transformations.
11
+
12
+ The dataset consists of procedurally generated images of 2D sprites, under controlled variations of **6 known factors of variation**:
13
+
14
+ - Object color (1 value in original dSprites, but here a random RGB color ∈ [0.5,1.0] is applied to each object at runtime)
15
+ - Object shape (3 values: square, ellipse, heart)
16
+ - Object scale (6 values)
17
+ - Object orientation (40 values)
18
+ - Object position X (32 values)
19
+ - Object position Y (32 values)
20
+
21
+ All possible combinations of these factors are present exactly once, generating a total of **737,280 images** at a resolution of **64×64 pixels**.
22
+
23
+ Each image is provided along with:
24
+
25
+ - **Discrete latent classes** (indices for each factor)
26
+ - **Continuous latent values**
27
+ - **Actual object color used (colorRGB field)**
28
+
29
+ The dataset is commonly used for **benchmarking disentanglement learning**, and can be used in conjunction with other variants:
30
+
31
+ - [randall-lab/dsprites (default, grayscale)](https://huggingface.co/datasets/randall-lab/dsprites)
32
+ - [randall-lab/dsprites-noisy](https://huggingface.co/datasets/randall-lab/dsprites-noisy)
33
+ - [randall-lab/dsprites-scream](https://huggingface.co/datasets/randall-lab/dsprites-scream)
34
+
35
+ These variants allow systematic testing under **different nuisance variations** (color, noise, background texture, abstract factors).
36
+
37
+ ![Dataset Visualization](https://huggingface.co/datasets/randall-lab/dsprites-color/blob/main/animation0.gif)
38
+
39
+ ## Dataset Source
40
+ - **Homepage**: [https://github.com/google-research/disentanglement_lib/](https://github.com/google-research/disentanglement_lib/tree/master)
41
+ - **License**: Apache License 2.0
42
+ - **Paper**: Francesco Locatello et al. _Challenging Common Assumptions in the Unsupervised Learning of Disentangled Representations_. ICML 2019.
43
+
44
+ ## Dataset Structure
45
+ |Factors|Possible Classes (Indices)|Values|
46
+ |---|---|---|
47
+ |color|white=0 (original label, fixed)|Random RGB color ∈ [0.5,1.0] stored in `colorRGB`|
48
+ |shape|square=0, ellipse=1, heart=2|1.0, 2.0, 3.0 (categorical)|
49
+ |scale|0,...,5|[0.5, 1.0] linearly spaced (6 values)|
50
+ |orientation|0,...,39|[0, 2π] radians (40 values)|
51
+ |posX|0,...,31|[0, 1] normalized position (32 values)|
52
+ |posY|0,...,31|[0, 1] normalized position (32 values)|
53
+
54
+ **Note:** In this Color variant, the `color` and `colorValue` fields remain 0 to match the original dSprites format. The **actual applied color** is provided in `colorRGB`, a list of `[R, G, B]` values ∈ [0.5,1.0].
55
+
56
+ Each image corresponds to a unique combination of these 6 factors. The images are stored in a **row-major order** (fastest-changing factor is `posY`, slowest-changing factor is `color`).
57
+
58
+ ### Why no train/test split?
59
+ The Color dSprites dataset does not provide an official train/test split. It is designed for **representation learning research**, where the goal is to learn disentangled and interpretable latent factors. Since the dataset is a complete Cartesian product of all factor combinations, models typically require access to the full dataset to explore factor-wise variations.
60
+
61
+ ## Example Usage
62
+ Below is a quick example of how to load this dataset via the Hugging Face Datasets library:
63
+ ```python
64
+ from datasets import load_dataset
65
+
66
+ # Load the dataset
67
+ dataset = load_dataset("randall-lab/dsprites-color", split="train", trust_remote_code=True)
68
+
69
+ # Access a sample from the dataset
70
+ example = dataset[0]
71
+ image = example["image"]
72
+ label = example["label"] # [color_idx, shape_idx, scale_idx, orientation_idx, posX_idx, posY_idx]
73
+ label_values = example["label_values"] # corresponding continuous values
74
+ colorRGB = example["colorRGB"] # Actual RGB color used in this sample
75
+
76
+ # Label Classes
77
+ color = example["color"] # 0
78
+ shape = example["shape"] # 0-2
79
+ scale = example["scale"] # 0-5
80
+ orientation = example["orientation"] # 0-39
81
+ posX = example["posX"] # 0-31
82
+ posY = example["posY"] # 0-31
83
+
84
+ # Label Values
85
+ color_value = example["colorValue"] # 1
86
+ shape_value = example["shapeValue"] # 1.0, 2.0, 3.0
87
+ scale_value = example["scaleValue"] # [0.5, 1]
88
+ orientation_value = example["orientationValue"] # [0, 2π]
89
+ posX_value = example["posXValue"] # [0, 1]
90
+ posY_value = example["posYValue"] # [0, 1]
91
+
92
+ # Actual color applied to object
93
+ print(f"Actual RGB color: {colorRGB}")
94
+
95
+ image.show() # Display the image
96
+ print(f"Label (factors): {label}")
97
+ print(f"Label values (factors): {label_values}")
98
+ ```
99
+ If you are using colab, you should update datasets to avoid errors
100
+ ```
101
+ pip install -U datasets
102
+ ```
103
+ ## Citation
104
+ ```
105
+ @inproceedings{locatello2019challenging,
106
+ title={Challenging Common Assumptions in the Unsupervised Learning of Disentangled Representations},
107
+ author={Locatello, Francesco and Bauer, Stefan and Lucic, Mario and Raetsch, Gunnar and Gelly, Sylvain and Sch{\"o}lkopf, Bernhard and Bachem, Olivier},
108
+ booktitle={International Conference on Machine Learning},
109
+ pages={4114--4124},
110
+ year={2019}
111
+ }
112
+ ```
animation0.gif ADDED

Git LFS Details

  • SHA256: e6417e22e079f9b307199cf46347469f3f8fdfee45900591b90d532a50027b5e
  • Pointer size: 130 Bytes
  • Size of remote file: 59.1 kB
dsprites-color.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ "Color dSprites dataset: procedurally generated 2D shapes dataset with known ground-truth factors, "
16
+ "commonly used for disentangled representation learning. "
17
+ "This is the ColorDSprites variant, where each object is randomly colored per sample, "
18
+ "while background remains black. "
19
+ "Factors: color (1), shape (3), scale (6), orientation (40), position X (32), position Y (32). "
20
+ "Images are 64x64 RGB."
21
+ ),
22
+ features=datasets.Features(
23
+ {
24
+ "image": datasets.Image(), # (64, 64), grayscale
25
+ "index": datasets.Value("int32"), # index of the image
26
+ "label": datasets.Sequence(datasets.Value("int32")), # 6 factor indices (classes)
27
+ "label_values": datasets.Sequence(datasets.Value("float32")), # 6 factor continuous values
28
+ "color": datasets.Value("int32"), # color index (always 0)
29
+ "shape": datasets.Value("int32"), # shape index (0-2)
30
+ "scale": datasets.Value("int32"), # scale index (0-5)
31
+ "orientation": datasets.Value("int32"), # orientation index (0-39)
32
+ "posX": datasets.Value("int32"), # posX index (0-31)
33
+ "posY": datasets.Value("int32"), # posY index (0-31)
34
+ "colorValue": datasets.Value("float64"), # color index (always 0)
35
+ "shapeValue": datasets.Value("float64"), # shape index (0-2)
36
+ "scaleValue": datasets.Value("float64"), # scale index (0-5)
37
+ "orientationValue": datasets.Value("float64"), # orientation index (0-39)
38
+ "posXValue": datasets.Value("float64"), # posX index (0-31)
39
+ "posYValue": datasets.Value("float64"), # posY index (0-31)
40
+ }
41
+ ),
42
+ supervised_keys=("image", "label"),
43
+ homepage="https://github.com/google-research/disentanglement_lib/tree/master",
44
+ license="apache-2.0",
45
+ citation="""@inproceedings{locatello2019challenging,
46
+ title={Challenging Common Assumptions in the Unsupervised Learning of Disentangled Representations},
47
+ author={Locatello, Francesco and Bauer, Stefan and Lucic, Mario and Raetsch, Gunnar and Gelly, Sylvain and Sch{\"o}lkopf, Bernhard and Bachem, Olivier},
48
+ booktitle={International Conference on Machine Learning},
49
+ pages={4114--4124},
50
+ year={2019}
51
+ }""",
52
+ )
53
+
54
+ def _split_generators(self, dl_manager):
55
+ npz_path = dl_manager.download(_DSPRITES_URL)
56
+
57
+ return [
58
+ datasets.SplitGenerator(
59
+ name=datasets.Split.TRAIN,
60
+ gen_kwargs={"npz_path": npz_path},
61
+ ),
62
+ ]
63
+
64
+ def _generate_examples(self, npz_path):
65
+ # Load npz
66
+ data = np.load(npz_path, allow_pickle=True)
67
+ images = data["imgs"] # shape: (737280, 64, 64), uint8
68
+ latents_classes = data["latents_classes"] # shape: (737280, 6), int64
69
+ latents_values = data["latents_values"] # shape: (737280, 6), float64
70
+
71
+ # Iterate over images
72
+ for idx in range(len(images)):
73
+ img = images[idx] # (64, 64), uint8
74
+ img = img.astype(np.float32) / 1.0
75
+ color_rgb = np.random.uniform(0.5, 1.0, size=(3,))
76
+ img_rgb = (img[..., None] * color_rgb) * 255
77
+ img_pil = Image.fromarray(img_rgb.astype(np.uint8), mode="RGB")
78
+
79
+ factors_classes = latents_classes[idx].tolist() # [color_idx, shape_idx, scale_idx, orientation_idx, posX_idx, posY_idx]
80
+ factors_values = latents_values[idx].tolist()
81
+
82
+ yield idx, {
83
+ "image": img_pil,
84
+ "index": idx,
85
+ "label": factors_classes,
86
+ "label_values": factors_values,
87
+ "color": factors_classes[0], # always 0
88
+ "shape": factors_classes[1],
89
+ "scale": factors_classes[2],
90
+ "orientation": factors_classes[3],
91
+ "posX": factors_classes[4],
92
+ "posY": factors_classes[5],
93
+ "colorValue": factors_values[0], # always 0.0
94
+ "shapeValue": factors_values[1],
95
+ "scaleValue": factors_values[2],
96
+ "orientationValue": factors_values[3],
97
+ "posXValue": factors_values[4],
98
+ "posYValue": factors_values[5],
99
+ "colorRGB": color_rgb.tolist(), # [R, G, B], ∈ [0.5,1.0]
100
+ }