File size: 4,192 Bytes
0b00e69
 
 
 
2c364b4
0b00e69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import datasets
import numpy as np
from PIL import Image

_MPI3D_URL = "https://huggingface.co/datasets/randall-lab/mpi3d-complex/resolve/main/real3d_complicated_shapes_ordered.npz"

class MPI3DComplex(datasets.GeneratorBasedBuilder):
    """MPI3D Complex dataset: 4x4x2x3x3x40x40 factor combinations, 64x64 RGB images."""

    VERSION = datasets.Version("1.0.0")

    def _info(self):
        return datasets.DatasetInfo(
            description=(
                "MPI3D Complex dataset: real-world images of complex everyday objects (coffee-cup, tennis-ball, "
                "croissant, beer-cup) manipulated by a robotic platform under controlled variations of 7 known factors. "
                "Images are 64x64 RGB (downsampled). "
                "Factors: object color (4), object shape (4), object size (2), camera height (3), "
                "background color (3), robotic arm DOF1 (40), robotic arm DOF2 (40). "
                "Images were captured using real cameras, introducing realistic noise and lighting conditions. "
                "The images are ordered as the Cartesian product of the factors in row-major order."
            ),
            features=datasets.Features(
                {
                    "image": datasets.Image(),    # (64, 64, 3)
                    "index": datasets.Value("int32"),  # index of the image
                    "label": datasets.Sequence(datasets.Value("int32")),  # 7 factor indices
                    "color": datasets.Value("int32"),  # object color index (0-3)
                    "shape": datasets.Value("int32"),  # object shape index (0-3)
                    "size": datasets.Value("int32"),  # object size index (0-1)
                    "height": datasets.Value("int32"),  # camera height index (0-2)
                    "background": datasets.Value("int32"),  # background color index (0-2)
                    "dof1": datasets.Value("int32"),  # robotic arm DOF1 index (0-39)
                    "dof2": datasets.Value("int32"),  # robotic arm DOF2 index (0-39)
                }
            ),
            supervised_keys=("image", "label"),
            homepage="https://github.com/rr-learning/disentanglement_dataset",
            license="Creative Commons Attribution 4.0 International",
            citation="""@article{gondal2019transfer,
  title={On the transfer of inductive bias from simulation to the real world: a new disentanglement dataset},
  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},
  journal={Advances in Neural Information Processing Systems},
  volume={32},
  year={2019}
}""",
        )

    def _split_generators(self, dl_manager):
        npz_path = dl_manager.download(_MPI3D_URL)

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={"npz_path": npz_path},
            ),
        ]

    def _generate_examples(self, npz_path):
        # Load npz
        data = np.load(npz_path)
        images = data["images"]  # shape: (460800, 64, 64, 3)

        factor_sizes = np.array([4, 4, 2, 3, 3, 40, 40])  # key change for complex
        factor_bases = np.cumprod([1] + list(factor_sizes[::-1]))[::-1][1:]

        def index_to_factors(index):
            factors = []
            for base, size in zip(factor_bases, factor_sizes):
                factor = (index // base) % size
                factors.append(int(factor))
            return factors

        # Iterate over images
        for idx in range(len(images)):
            img = images[idx]
            img_pil = Image.fromarray(img)

            factors = index_to_factors(idx)

            yield idx, {
                "image": img_pil,
                "index": idx,
                "label": factors,
                "color": factors[0],
                "shape": factors[1],
                "size": factors[2],
                "height": factors[3],
                "background": factors[4],
                "dof1": factors[5],
                "dof2": factors[6],
            }