Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

render new marker offsets, add option to visualize marker error #63

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified demos/demo_viz.p
Binary file not shown.
22 changes: 18 additions & 4 deletions demos/viz_usage.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion stac_mjx/compute_stac.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@

print(f"offset optimization finished in {time.time()-s}")

return mjx_model, mjx_data
return mjx_model, mjx_data, offset_opt_param

Check warning on line 175 in stac_mjx/compute_stac.py

View check run for this annotation

Codecov / codecov/patch

stac_mjx/compute_stac.py#L175

Added line #L175 was not covered by tests


def pose_optimization(
Expand Down
66 changes: 52 additions & 14 deletions stac_mjx/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
name=key,
type="sphere",
size=[0.005],
rgba="0 0 0 1",
rgba="0 0 0 0.8",
pos=pos,
group=3,
)
Expand Down Expand Up @@ -259,7 +259,7 @@
print(f"Standard deviation: {std}")

print("starting offset optimization")
mjx_model, mjx_data = compute_stac.offset_optimization(
mjx_model, mjx_data, self._offsets = compute_stac.offset_optimization(

Check warning on line 262 in stac_mjx/controller.py

View check run for this annotation

Codecov / codecov/patch

stac_mjx/controller.py#L262

Added line #L262 was not covered by tests
mjx_model,
mjx_data,
kp_data,
Expand Down Expand Up @@ -390,7 +390,9 @@
x = x.reshape(-1, x.shape[-1])
q = q.reshape(-1, q.shape[-1])
else:
offsets = op.get_site_pos(mjx_model, self._body_site_idxs).copy()
offsets = (

Check warning on line 393 in stac_mjx/controller.py

View check run for this annotation

Codecov / codecov/patch

stac_mjx/controller.py#L393

Added line #L393 was not covered by tests
self._offsets
charles-zhng marked this conversation as resolved.
Show resolved Hide resolved
) # op.get_site_pos(mjx_model, self._body_site_idxs).copy()

kp_data = kp_data.reshape(-1, kp_data.shape[-1])

Expand Down Expand Up @@ -468,6 +470,7 @@
camera: Union[int, str] = 0,
height: int = 1200,
width: int = 1920,
show_marker_error: bool = False,
):
"""Creates rendering using the instantiated model, given the user's qposes and kp_data.

Expand All @@ -481,6 +484,7 @@
camera (Union[int, str], optional): Mujoco camera name. Defaults to 0.
height (int, optional): Height in pixels. Defaults to 1200.
width (int, optional): Width in pixels. Defaults to 1920.
show_marker_error (bool, optional): Show distance between marker and keypoint. Defaults to False.

Raises:
ValueError: qposes and kp_data must have same length (shape[0])
Expand All @@ -506,28 +510,61 @@
render_mj_model, body_site_idxs, keypoint_site_idxs = (
self._create_keypoint_sites()
)
render_mj_model.site_pos[body_site_idxs] = offsets

# Add body sites for new offset positions
for (key, v), pos in zip(

Check warning on line 515 in stac_mjx/controller.py

View check run for this annotation

Codecov / codecov/patch

stac_mjx/controller.py#L515

Added line #L515 was not covered by tests
self.cfg.model.KEYPOINT_MODEL_PAIRS.items(), offsets.reshape((-1, 3))
):
parent = self._root.find("body", v)
parent.add(

Check warning on line 519 in stac_mjx/controller.py

View check run for this annotation

Codecov / codecov/patch

stac_mjx/controller.py#L518-L519

Added lines #L518 - L519 were not covered by tests
"site",
name=key + "_new",
type="sphere",
size=[0.005],
rgba="0 0 0 1",
pos=pos,
group=2,
)

# tendons from new marker sites to kp
if show_marker_error:
for key, v in self.cfg.model.KEYPOINT_MODEL_PAIRS.items():

Check warning on line 531 in stac_mjx/controller.py

View check run for this annotation

Codecov / codecov/patch

stac_mjx/controller.py#L530-L531

Added lines #L530 - L531 were not covered by tests
# pos = utils.params["KEYPOINT_INITIAL_OFFSETS"][key]
rgba = self.cfg.model.KEYPOINT_COLOR_PAIRS[key]
charles-zhng marked this conversation as resolved.
Show resolved Hide resolved
tendon = self._root.tendon.add(

Check warning on line 534 in stac_mjx/controller.py

View check run for this annotation

Codecov / codecov/patch

stac_mjx/controller.py#L533-L534

Added lines #L533 - L534 were not covered by tests
"spatial",
name=key + "-" + v,
width="0.001",
rgba="255 0 0 1", # Red
limited=False,
)
tendon.add("site", site=key + "_kp")
tendon.add("site", site=key + "_new")

Check warning on line 542 in stac_mjx/controller.py

View check run for this annotation

Codecov / codecov/patch

stac_mjx/controller.py#L541-L542

Added lines #L541 - L542 were not covered by tests

physics = mjcf.Physics.from_mjcf_model(self._root)
render_mj_model = deepcopy(physics.model.ptr)

Check warning on line 545 in stac_mjx/controller.py

View check run for this annotation

Codecov / codecov/patch

stac_mjx/controller.py#L544-L545

Added lines #L544 - L545 were not covered by tests

scene_option = mujoco.MjvOption()
scene_option.geomgroup[1] = 0

Check warning on line 548 in stac_mjx/controller.py

View check run for this annotation

Codecov / codecov/patch

stac_mjx/controller.py#L548

Added line #L548 was not covered by tests
scene_option.geomgroup[2] = 1

scene_option.sitegroup[2] = 1

scene_option.sitegroup[3] = 1
scene_option.sitegroup[3] = 0

Check warning on line 553 in stac_mjx/controller.py

View check run for this annotation

Codecov / codecov/patch

stac_mjx/controller.py#L553

Added line #L553 was not covered by tests
jf514 marked this conversation as resolved.
Show resolved Hide resolved
scene_option.flags[enums.mjtVisFlag.mjVIS_TRANSPARENT] = True
scene_option.flags[enums.mjtVisFlag.mjVIS_LIGHT] = False
scene_option.flags[enums.mjtVisFlag.mjVIS_LIGHT] = True

Check warning on line 555 in stac_mjx/controller.py

View check run for this annotation

Codecov / codecov/patch

stac_mjx/controller.py#L555

Added line #L555 was not covered by tests
scene_option.flags[enums.mjtVisFlag.mjVIS_CONVEXHULL] = True
scene_option.flags[enums.mjtRndFlag.mjRND_SHADOW] = False
scene_option.flags[enums.mjtRndFlag.mjRND_REFLECTION] = False
scene_option.flags[enums.mjtRndFlag.mjRND_SKYBOX] = False
scene_option.flags[enums.mjtRndFlag.mjRND_FOG] = False

scene_option.flags[enums.mjtRndFlag.mjRND_SHADOW] = True
scene_option.flags[enums.mjtRndFlag.mjRND_REFLECTION] = True
scene_option.flags[enums.mjtRndFlag.mjRND_SKYBOX] = True
scene_option.flags[enums.mjtRndFlag.mjRND_FOG] = True

Check warning on line 560 in stac_mjx/controller.py

View check run for this annotation

Codecov / codecov/patch

stac_mjx/controller.py#L557-L560

Added lines #L557 - L560 were not covered by tests
mj_data = mujoco.MjData(render_mj_model)

mujoco.mj_kinematics(render_mj_model, mj_data)

renderer = mujoco.Renderer(render_mj_model, height=height, width=width)

# slice kp_data to match qposes length
# Slice kp_data to match qposes length
kp_data = kp_data[: qposes.shape[0]]

# Slice arrays to be the range that is being rendered
Expand All @@ -538,10 +575,11 @@
# render while stepping using mujoco
with imageio.get_writer(save_path, fps=self.cfg.model.RENDER_FPS) as video:
for qpos, kps in tqdm(zip(qposes, kp_data)):
# Set keypoints
# Set keypoints--they're in cartesian space, but since they're attached to the worldbody it's the same
render_mj_model.site_pos[keypoint_site_idxs] = np.reshape(kps, (-1, 3))
mj_data.qpos = qpos
mujoco.mj_forward(render_mj_model, mj_data)

mujoco.mj_fwdPosition(render_mj_model, mj_data)

Check warning on line 582 in stac_mjx/controller.py

View check run for this annotation

Codecov / codecov/patch

stac_mjx/controller.py#L582

Added line #L582 was not covered by tests

renderer.update_scene(mj_data, camera=camera, scene_option=scene_option)
pixels = renderer.render()
Expand Down
4 changes: 3 additions & 1 deletion stac_mjx/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
height: int = 1200,
width: int = 1920,
base_path=None,
show_marker_error=False,
):
"""Render forward kinematics from keypoint positions.

Expand Down Expand Up @@ -45,7 +46,7 @@
d = pickle.load(file)
qposes = np.array(d["qpos"])
kp_data = np.array(d["kp_data"])
kp_names = d["kp_names"]
kp_names = d["KP_NAMES"]

Check warning on line 49 in stac_mjx/viz.py

View check run for this annotation

Codecov / codecov/patch

stac_mjx/viz.py#L49

Added line #L49 was not covered by tests
charles-zhng marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this broken before?

offsets = d["offsets"]

# initialize STAC to create mj_model with scaling and marker body sites according to config
Expand All @@ -61,4 +62,5 @@
camera,
height,
width,
show_marker_error,
)
Loading