mpi3d-realistic / mpi3d-realistic.py
haodoz0118's picture
Upload 3 files
a4f512a verified
import datasets
import numpy as np
from PIL import Image
_MPI3D_URL = "https://huggingface.co/datasets/waleedgondal/mpi3d/resolve/main/mpi3d_realistic.npz"
class MPI3DRealistic(datasets.GeneratorBasedBuilder):
"""MPI3D Realistic dataset: 6x6x2x3x3x40x40 factor combinations, 64x64 RGB images."""
VERSION = datasets.Version("1.0.0")
def _info(self):
return datasets.DatasetInfo(
description=(
"MPI3D Realistic dataset: high-fidelity photorealistic synthetic images of physical 3D objects, "
"rendered with a physically-based renderer under controlled variations of 7 known factors of variation. "
"Images are 64x64 RGB (downsampled from original resolution for benchmarking). "
"Factors: object color (6), object shape (6), object size (2), camera height (3), "
"background color (3), robotic arm DOF1 (40), robotic arm DOF2 (40). "
"Images were generated using CAD models and realistic lighting/materials to closely match real-world images. "
"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-5)
"shape": datasets.Value("int32"), # object shape index (0-5)
"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: (1036800, 64, 64, 3)
factor_sizes = np.array([6, 6, 2, 3, 3, 40, 40])
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],
}