haodoz0118 commited on
Commit
2ba8564
·
verified ·
1 Parent(s): f093dfa

Upload 2 files

Browse files
Files changed (2) hide show
  1. README.md +105 -0
  2. dsprites-scream.py +119 -0
README.md ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ ---
4
+ # Dataset Card for Scream dSprites
5
+
6
+ ## Dataset Description
7
+
8
+ The **Scream 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 **Scream** variant, a random patch of a **Scream painting image** is used as the background for each sample, and the object sprite is embedded onto the patch by **inverting the pixel colors** of the object region.
11
+
12
+ This allows researchers to evaluate **robustness to complex background textures** and assess how well models can learn disentangled representations when the background introduces structured noise.
13
+
14
+ The dataset consists of procedurally generated images of 2D sprites, under controlled variations of **6 known factors of variation**:
15
+
16
+ - Object color (1 value in original dSprites, here fixed but inverted on background)
17
+ - Object shape (3 values: square, ellipse, heart)
18
+ - Object scale (6 values)
19
+ - Object orientation (40 values)
20
+ - Object position X (32 values)
21
+ - Object position Y (32 values)
22
+
23
+ All possible combinations of these factors are present exactly once, generating a total of **737,280 images** at a resolution of **64×64 pixels**. Each image is provided along with **Discrete latent classes** (indices for each factor), **Continuous latent values**.
24
+
25
+ These variants allow systematic testing under **different nuisance variations** (color, noise, background texture, abstract factors).
26
+ ![Dataset Visualization](https://huggingface.co/datasets/randall-lab/dsprites-scream/resolve/main/animation0.gif)
27
+
28
+ ### Scream background visualization
29
+
30
+ ![Scream background](https://huggingface.co/datasets/randall-lab/dsprites-scream/resolve/main/scream.jpg)
31
+
32
+ You can also explore other variants:
33
+
34
+ - [randall-lab/dsprites (default, grayscale)](https://huggingface.co/datasets/randall-lab/dsprites)
35
+ - [randall-lab/dsprites-color](https://huggingface.co/datasets/randall-lab/dsprites-color)
36
+ - [randall-lab/dsprites-noisy](https://huggingface.co/datasets/randall-lab/dsprites-noisy)
37
+
38
+ ## Dataset Source
39
+ - **Homepage**: [https://github.com/google-research/disentanglement_lib/](https://github.com/google-research/disentanglement_lib/tree/master)
40
+ - **License**: Apache License 2.0
41
+ - **Paper**: Francesco Locatello et al. _Challenging Common Assumptions in the Unsupervised Learning of Disentangled Representations_. ICML 2019.
42
+
43
+ ## Dataset Structure
44
+ |Factors|Possible Classes (Indices)|Values|
45
+ |---|---|---|
46
+ |color|white=0 (original label, fixed)|Inverted object pixels on scream background|
47
+ |shape|square=0, ellipse=1, heart=2|1.0, 2.0, 3.0 (categorical)|
48
+ |scale|0,...,5|[0.5, 1.0] linearly spaced (6 values)|
49
+ |orientation|0,...,39|[0, 2π] radians (40 values)|
50
+ |posX|0,...,31|[0, 1] normalized position (32 values)|
51
+ |posY|0,...,31|[0, 1] normalized position (32 values)|
52
+
53
+ **Note:** In this Scream variant, the `color` and `colorValue` fields remain 0 to match the original dSprites format. The **object pixels** are inverted on top of a random Scream background patch.
54
+
55
+ 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`).
56
+
57
+ ### Why no train/test split?
58
+ The Scream 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.
59
+
60
+ ## Example Usage
61
+ Below is a quick example of how to load this dataset via the Hugging Face Datasets library:
62
+ ```python
63
+ from datasets import load_dataset
64
+ # Load the dataset
65
+ dataset = load_dataset("randall-lab/dsprites-scream", split="train", trust_remote_code=True)
66
+ # Access a sample from the dataset
67
+ example = dataset[0]
68
+ image = example["image"]
69
+ label = example["label"] # [color_idx, shape_idx, scale_idx, orientation_idx, posX_idx, posY_idx]
70
+ label_values = example["label_values"] # corresponding continuous values
71
+
72
+ # Label Classes
73
+ color = example["color"] # 0
74
+ shape = example["shape"] # 0-2
75
+ scale = example["scale"] # 0-5
76
+ orientation = example["orientation"] # 0-39
77
+ posX = example["posX"] # 0-31
78
+ posY = example["posY"] # 0-31
79
+ # Label Values
80
+ color_value = example["colorValue"] # 1
81
+ shape_value = example["shapeValue"] # 1.0, 2.0, 3.0
82
+ scale_value = example["scaleValue"] # [0.5, 1]
83
+ orientation_value = example["orientationValue"] # [0, 2π]
84
+ posX_value = example["posXValue"] # [0, 1]
85
+ posY_value = example["posYValue"] # [0, 1]
86
+
87
+ image.show() # Display the image
88
+ print(f"Label (factors): {label}")
89
+ print(f"Label values (factors): {label_values}")
90
+ ```
91
+ If you are using colab, you should update datasets to avoid errors
92
+ ```
93
+ pip install -U datasets
94
+ ```
95
+ ## Citation
96
+ ```
97
+ @inproceedings{locatello2019challenging,
98
+ title={Challenging Common Assumptions in the Unsupervised Learning of Disentangled Representations},
99
+ author={Locatello, Francesco and Bauer, Stefan and Lucic, Mario and Raetsch, Gunnar and Gelly, Sylvain and Sch{\"o}lkopf, Bernhard and Bachem, Olivier},
100
+ booktitle={International Conference on Machine Learning},
101
+ pages={4114--4124},
102
+ year={2019}
103
+ }
104
+ ```
105
+
dsprites-scream.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datasets
2
+ import numpy as np
3
+ from PIL import Image
4
+
5
+ _DSPRITES_URL = "https://github.com/google-deepmind/dsprites-dataset/raw/refs/heads/master/dsprites_ndarray_co1sh3sc6or40x32y32_64x64.npz"
6
+ _SCREAM_URL = "https://huggingface.co/datasets/randall-lab/dsprites-scream/resolve/main/scream.jpg"
7
+
8
+ class DSprites(datasets.GeneratorBasedBuilder):
9
+ """Scream dSprites dataset: 3x6x40x32x32 factor combinations, 64x64 RGB images with Scream background."""
10
+
11
+ VERSION = datasets.Version("1.0.0")
12
+
13
+ def _info(self):
14
+ return datasets.DatasetInfo(
15
+ description=(
16
+ "Scream dSprites dataset: procedurally generated 2D shapes dataset with known ground-truth factors, "
17
+ "commonly used for disentangled representation learning. "
18
+ "This is the ScreamDSprites variant, where each object is embedded onto a random Scream image patch "
19
+ "with the object pixels inverted. "
20
+ "Factors: color (1), shape (3), scale (6), orientation (40), position X (32), position Y (32). "
21
+ "Images are 64x64 RGB."
22
+ ),
23
+ features=datasets.Features(
24
+ {
25
+ "image": datasets.Image(), # (64, 64), RGB
26
+ "index": datasets.Value("int32"), # index of the image
27
+ "label": datasets.Sequence(datasets.Value("int32")), # 6 factor indices (classes)
28
+ "label_values": datasets.Sequence(datasets.Value("float32")), # 6 factor continuous values
29
+ "color": datasets.Value("int32"), # color index (always 0)
30
+ "shape": datasets.Value("int32"), # shape index (0-2)
31
+ "scale": datasets.Value("int32"), # scale index (0-5)
32
+ "orientation": datasets.Value("int32"), # orientation index (0-39)
33
+ "posX": datasets.Value("int32"), # posX index (0-31)
34
+ "posY": datasets.Value("int32"), # posY index (0-31)
35
+ "colorValue": datasets.Value("float64"), # color index (always 0)
36
+ "shapeValue": datasets.Value("float64"), # shape index (0-2)
37
+ "scaleValue": datasets.Value("float64"), # scale index (0-5)
38
+ "orientationValue": datasets.Value("float64"), # orientation index (0-39)
39
+ "posXValue": datasets.Value("float64"), # posX index (0-31)
40
+ "posYValue": datasets.Value("float64"), # posY index (0-31)
41
+ }
42
+ ),
43
+ supervised_keys=("image", "label"),
44
+ homepage="https://github.com/google-research/disentanglement_lib/tree/master",
45
+ license="apache-2.0",
46
+ citation="""@inproceedings{locatello2019challenging,
47
+ title={Challenging Common Assumptions in the Unsupervised Learning of Disentangled Representations},
48
+ author={Locatello, Francesco and Bauer, Stefan and Lucic, Mario and Raetsch, Gunnar and Gelly, Sylvain and Sch{\"o}lkopf, Bernhard and Bachem, Olivier},
49
+ booktitle={International Conference on Machine Learning},
50
+ pages={4114--4124},
51
+ year={2019}
52
+ }""",
53
+ )
54
+
55
+ def _split_generators(self, dl_manager):
56
+ npz_path = dl_manager.download(_DSPRITES_URL)
57
+ scream_path = dl_manager.download_and_extract(_SCREAM_URL)
58
+
59
+ return [
60
+ datasets.SplitGenerator(
61
+ name=datasets.Split.TRAIN,
62
+ gen_kwargs={"npz_path": npz_path, "scream_path": scream_path},
63
+ ),
64
+ ]
65
+
66
+ def _generate_examples(self, npz_path, scream_path):
67
+ # Load dSprites data
68
+ data = np.load(npz_path, allow_pickle=True)
69
+ images = data["imgs"] # shape: (737280, 64, 64), uint8
70
+ latents_classes = data["latents_classes"] # shape: (737280, 6), int64
71
+ latents_values = data["latents_values"] # shape: (737280, 6), float64
72
+
73
+ # Load Scream image once
74
+ scream_img = Image.open(scream_path).convert("RGB")
75
+ scream_img = scream_img.resize((350, 274))
76
+ scream = np.array(scream_img).astype(np.float32) / 255.0 # (H, W, 3)
77
+
78
+ # Iterate over images
79
+ for idx in range(len(images)):
80
+ img = images[idx].astype(np.float32) # (64, 64), float32
81
+
82
+ # Random scream patch
83
+ x_crop = np.random.randint(0, scream.shape[0] - 64)
84
+ y_crop = np.random.randint(0, scream.shape[1] - 64)
85
+ background_patch = scream[x_crop:x_crop + 64, y_crop:y_crop + 64] # (64, 64, 3)
86
+
87
+ # Create mask
88
+ mask = img == 1
89
+ mask = mask[..., None] # (64, 64, 1)
90
+
91
+ # Invert object region
92
+ output_img = np.copy(background_patch)
93
+ output_img[mask.squeeze()] = 1.0 - background_patch[mask.squeeze()]
94
+
95
+ # Convert to RGB PIL
96
+ img_pil = Image.fromarray((output_img * 255).astype(np.uint8), mode="RGB")
97
+
98
+ # Labels
99
+ factors_classes = latents_classes[idx].tolist()
100
+ factors_values = latents_values[idx].tolist()
101
+
102
+ yield idx, {
103
+ "image": img_pil,
104
+ "index": idx,
105
+ "label": factors_classes,
106
+ "label_values": factors_values,
107
+ "color": factors_classes[0],
108
+ "shape": factors_classes[1],
109
+ "scale": factors_classes[2],
110
+ "orientation": factors_classes[3],
111
+ "posX": factors_classes[4],
112
+ "posY": factors_classes[5],
113
+ "colorValue": factors_values[0],
114
+ "shapeValue": factors_values[1],
115
+ "scaleValue": factors_values[2],
116
+ "orientationValue": factors_values[3],
117
+ "posXValue": factors_values[4],
118
+ "posYValue": factors_values[5],
119
+ }