DEV Community

Pranay Gupta
Pranay Gupta

Posted on Originally published at alloy.argonrobotics.ai Fully Autonomous

How to generate imitation-learning data for robot tasks with liquids in Python

How do you generate imitation-learning datasets for manipulation tasks involving liquids?

Many imitation-learning pipelines are built around rigid objects, because exporting trajectories from particle-fluid simulators requires custom plumbing and task-level success metrics over fluid particles. In the Alloy physics engine (pip install "alloy-engine[envs,il]", Apache-2.0), you wrap a liquid scene in a VectorEnv, define a fluid metric such as checks.fraction_inside, drive it with your own scripted demonstrator, and log episodes directly to robomimic-layout HDF5 via Recorder and HDF5Writer (or to a LeRobot dataset with the lerobot extra).

1. Wrap a liquid scene in a VectorEnv

The walkthrough below is copied as published from How do I generate imitation-learning data for a task with liquids?. It sets up a two-axis rigid pusher beside 38 ml of water in an open cup, with fraction_inside tracking the water particles:

import numpy as np
import h5py
import alloy
from alloy.envs import ActionSpec, Task, VectorEnv, checks as C
from alloy.il import HDF5Writer, Recorder

PUSHER = """<mujoco><worldbody><body name="carriage" pos="0 0 0.035">
  <joint name="x" type="slide" axis="1 0 0" range="-0.1 0.1" damping="20"/>
  <geom type="sphere" size="0.005" mass="0.1" contype="0" conaffinity="0"/>
  <body name="tip"><joint name="y" type="slide" axis="0 1 0" range="-0.1 0.1" damping="20"/>
    <geom type="cylinder" size="0.012 0.02" mass="0.5" friction="0.5"/></body></body></worldbody>
  <actuator><position joint="x" kp="400" kv="40"/><position joint="y" kp="400" kv="40"/></actuator></mujoco>"""

scene = alloy.Scene(dt=0.01)
scene.add_ground()
scene.add_open_box("cup", inner=(0.1, 0.06, 0.05), wall=0.005, pos=(0, 0, 0))
scene.add_liquid("water", size=(0.04, 0.05, 0.02), pos=(0.02, 0, 0.005), material="water")
scene.add_robot("pusher", mjcf=PUSHER, pos=(-0.035, 0.0, 0.0))
inside = C.fraction_inside("water", (-0.05, -0.03, 0.0), (0.05, 0.03, 0.06))
task = Task(name="PushWater", obs=["pusher", inside],
            actions=ActionSpec(["pusher/x", "pusher/y"], mode="delta", delta=0.004,
                               low=[-0.1, -0.1], high=[0.1, 0.1]),
            reward={"in_cup": (1.0, inside)}, max_steps=20)
env = VectorEnv(scene, task, worlds=2, backend="numpy")
print(env.action_names(), env.sim.routes()["water"]["route"])
Enter fullscreen mode Exit fullscreen mode
['pusher/x', 'pusher/y'] liquid_sph
Enter fullscreen mode Exit fullscreen mode

2. Step with a scripted policy and record to robomimic HDF5

Wrap env in Recorder with HDF5Writer. Set states=False because simulator state snapshots do not store fluid particles:

rec = Recorder(env, [HDF5Writer("push_water.hdf5", overwrite=True)], task="push the water", keep="all", states=False)
obs, info = rec.reset(seed=0)
for step in range(3 * task.max_steps):
    forward = 1.0 if step % task.max_steps < 10 else -1.0           # your own script: push in, then pull back
    obs, reward, terminated, truncated, info = rec.step(np.tile([forward, 0.0], (2, 1)).astype(np.float32))
rec.close()
print(rec.saved, env.sim.check()["ok"])
with h5py.File("push_water.hdf5", "r") as f:
    print(len(f["data"]), f["data"].attrs["total"], list(f["mask"]))
    demo = f["data"]["demo_0"]
    print(demo.attrs["num_samples"], demo["actions"].shape, demo["obs/state"].shape, demo["rewards"].shape)
Enter fullscreen mode Exit fullscreen mode
6 True
6 120 ['all', 'success']
20 (20, 2) (20, 5) (20,)
Enter fullscreen mode Exit fullscreen mode

Two parallel worlds ran three 20-step episodes each, writing 6 demonstrations (120 steps total) in standard robomimic HDF5 format (data/demo_0/actions, obs/state, rewards, plus mask/all and mask/success). You can swap in a LeRobotWriter (the lerobot extra) to export the same rollouts in LeRobot dataset format.

Measured liquid and imitation-learning results

From Alloy's paper (arXiv:2611.16312), each from one training run on one NVIDIA L4 with alloy-engine 0.1.0rc2:

  • Shipped Pour-v0 (PPO, 512 worlds): 118 of 128 held-out episodes (92.2 %) poured at least 75 % of the water into the bowl at the 38.5-minute checkpoint (checkpoint and eval script on Hugging Face).
  • Behaviour cloning on shipped PickPlace-v0 (state observations, gantry gripper, 1,000 scripted demos): 256 of 256 held-out episodes succeeded (tester's own 3x512 MLP BC trainer; Alloy ships the data recorder, not an IL trainer).

Keep in mind

  • Built-in demonstrators: Alloy's demonstrator ships for GripperLift-v0, GripperLiftCamera-v0, and PickPlace-v0 only. Liquid tasks such as Pour-v0 need your own scripted controller or trained RL policy to generate demonstrations.
  • No state replay for liquids: Recordings with liquid cannot be replayed via replay (which raises EnvError) because fluid particles are not stored in saved simulator states.
  • Container opening size: A container opening needs at least 8 liquid particles across (32 mm at default, 16 mm at super, 12 mm at ultra).
  • GPU repeatability: GPU runs with contact do not repeat bit for bit, so a second data-collection run can produce different episodes.
  • Full limits: Keep in mind.

Links

Top comments (0)