haodoz0118 commited on
Commit
22a51fc
·
verified ·
1 Parent(s): f10d8a4

Upload mpi3d-toy.py

Browse files
Files changed (1) hide show
  1. mpi3d-toy.py +92 -0
mpi3d-toy.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datasets
2
+ import numpy as np
3
+ import os
4
+ from PIL import Image
5
+
6
+ _MPI3D_URL = "https://huggingface.co/datasets/waleedgondal/mpi3d/resolve/main/mpi3d_toy.npz"
7
+
8
+ class MPI3DToy(datasets.GeneratorBasedBuilder):
9
+ """MPI3D Toy dataset: 6x6x2x3x3x40x40 factor combinations, 64x64 RGB images."""
10
+
11
+ VERSION = datasets.Version("1.0.0")
12
+
13
+ def _info(self):
14
+ return datasets.DatasetInfo(
15
+ description=(
16
+ "MPI3D Toy dataset: synthetic images of physical 3D objects with "
17
+ "7 known factors of variation. "
18
+ "Images are 64x64 RGB. "
19
+ "Factors: object color (6), object shape (6), object size (2), camera height (3), "
20
+ "background color (3), robotic arm DOF1 (40), robotic arm DOF2 (40). "
21
+ "The images are ordered as the Cartesian product of the factors in row-major order."
22
+ ),
23
+ features=datasets.Features(
24
+ {
25
+ "image": datasets.Image(), # (64, 64, 3)
26
+ "index": datasets.Value("int32"), # index of the image
27
+ "label": datasets.Sequence(datasets.Value("int32")), # 7 factor indices
28
+ "color": datasets.Value("int32"), # object color index (0-5)
29
+ "shape": datasets.Value("int32"), # object shape index (0-5)
30
+ "size": datasets.Value("int32"), # object size index (0-1)
31
+ "height": datasets.Value("int32"), # camera height index (0-2)
32
+ "background": datasets.Value("int32"), # background color index (0-2)
33
+ "dof1": datasets.Value("int32"), # robotic arm DOF1 index (0-39)
34
+ "dof2": datasets.Value("int32"), # robotic arm DOF2 index (0-39)
35
+ }
36
+ ),
37
+ supervised_keys=("image", "label"),
38
+ homepage="https://github.com/rr-learning/disentanglement_dataset",
39
+ license="Creative Commons Attribution 4.0 International",
40
+ citation="""@article{gondal2019transfer,
41
+ title={On the transfer of inductive bias from simulation to the real world: a new disentanglement dataset},
42
+ author={Gondal, Muhammad Waleed and Wuthrich, Manuel and Miladinovic, Djordje and Locatello, Francesco and Breidt, Martin and Volchkov, Valentin and Akpo, Joel and Bachem, Olivier and Sch{\"o}lkopf, Bernhard and Bauer, Stefan},
43
+ journal={Advances in Neural Information Processing Systems},
44
+ volume={32},
45
+ year={2019}
46
+ }""",
47
+ )
48
+
49
+ def _split_generators(self, dl_manager):
50
+ npz_path = dl_manager.download(_MPI3D_URL)
51
+
52
+ return [
53
+ datasets.SplitGenerator(
54
+ name=datasets.Split.TRAIN,
55
+ gen_kwargs={"npz_path": npz_path},
56
+ ),
57
+ ]
58
+
59
+ def _generate_examples(self, npz_path):
60
+ # Load npz
61
+ data = np.load(npz_path)
62
+ images = data["images"] # shape: (1036800, 64, 64, 3)
63
+
64
+ factor_sizes = np.array([6, 6, 2, 3, 3, 40, 40])
65
+ factor_bases = np.cumprod([1] + list(factor_sizes[::-1]))[::-1][1:]
66
+
67
+ def index_to_factors(index):
68
+ factors = []
69
+ for base, size in zip(factor_bases, factor_sizes):
70
+ factor = (index // base) % size
71
+ factors.append(int(factor))
72
+ return factors
73
+
74
+ # Iterate over images
75
+ for idx in range(len(images)):
76
+ img = images[idx]
77
+ img_pil = Image.fromarray(img)
78
+
79
+ factors = index_to_factors(idx)
80
+
81
+ yield idx, {
82
+ "image": img_pil,
83
+ "index": idx,
84
+ "label": factors,
85
+ "color": factors[0],
86
+ "shape": factors[1],
87
+ "size": factors[2],
88
+ "height": factors[3],
89
+ "background": factors[4],
90
+ "dof1": factors[5],
91
+ "dof2": factors[6],
92
+ }