haodoz0118 commited on
Commit
1b78494
·
verified ·
1 Parent(s): 9ea7e16

Update dsprites.py

Browse files
Files changed (1) hide show
  1. dsprites.py +73 -57
dsprites.py CHANGED
@@ -1,79 +1,95 @@
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
  }
 
 
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
  }