Semantic segmentation parity#1283
Conversation
|
@tkelestemur thank you for contributing this feature to mujoco warp! @StafaH @btaba would it make sense to combine this feature (in a potentially breaking way) with the existing segmentation api? @tkelestemur instead of adding a separate notebook for this feature, can we add an example to the existing tutorial notebook? |
|
Hi @tkelestemur, thanks for the PR! In this case I believe this new feature should not be implemented in MJWarp, and can be implemented at the framework level that you use downstream. The existing segmentation output provides enough information to perform post processing downstream e.g. by implementing semantic segmentation as an extension in Mjlab with a kernel that converts the existing integer id to another value inplace. |
|
@StafaH The existing segmentation buffer is sufficient to reconstruct semantic output for geom hits and background, but not for flex hits, because all flex hits are collapsed to -2 and lose the underlying |
|
I agree, we should fix existing segmentation output. I think what @thowell mentioned is correct, we should focus on having the existing segmentation output match MuJoCo exactly instead of making a seperate output, and we can update the tests to check that this segmentation output matches exactly the C MuJoCo output (similar to what was done for depth recently). WDYT @thowell? @tkelestemur this might also require opening an issue and a PR in mjlab to fix any changes that happen downstream there. |
|
@StafaH Sounds good -- I'll update this PR to use the existing segmentation API and also open an issue and a PR in mjlab. |
| seg = rc.seg_data.numpy() | ||
| self.assertTrue(np.any(seg >= 0), "Expected geom hits from auto-detected seg") | ||
| self.assertGreater(np.unique(seg).shape[0], 1) | ||
| seg = wp.zeros((1, 32, 32, 2), dtype=int) |
There was a problem hiding this comment.
can we create seg from the render context?
There was a problem hiding this comment.
Updated to size the segmentation output from rc.cam_res instead of hardcoding the resolution. Is that what you meant?
There was a problem hiding this comment.
lets change this back to what you had before wp.zeros((1, 32, 32, 2), dtype=int). thanks!
| "media.show_image(depth_grid, cmap='gray', vmin=0, vmax=1)" | ||
| ] | ||
| }, | ||
| { |
There was a problem hiding this comment.
i think we can simplify this and make it similar to the depth render example (essentially a comment plus 2 lines of code and then an additional segmentation image to display.
There was a problem hiding this comment.
Simplified the tutorial example to extend the existing rendering cell with a small segmentation extraction/display block. Lmk how it looks.
|
the updated pr looks great! added a few comments. please let us known if you have any questions. thanks! |
|
Thanks @thowell I've addressed your reviews. I also opened an downstream PR for mjlab here: mujocolab/mjlab#911 |
| "segmentation_grid = segmentation_data.numpy()[..., 0].reshape(4, 4, CAM_RES[1], CAM_RES[0])\n", | ||
| "segmentation_grid = segmentation_grid.transpose(0, 2, 1, 3)\n", | ||
| "segmentation_grid = segmentation_grid.reshape(4 * CAM_RES[0], 4 * CAM_RES[1])\n", | ||
| "media.show_image(segmentation_grid, cmap='viridis', vmin=-1, vmax=max(1, int(segmentation_grid.max())))\n" |
StafaH
left a comment
There was a problem hiding this comment.
This is shaping up nicely. Left some comments.
|
Also might be worth having the seg test xml include 1 flex object for completeness |
StafaH
left a comment
There was a problem hiding this comment.
Thanks @tkelestemur, left one last comment, but otherwise LGTM
|
@tkelestemur some of the checks are not passing, please take a look. might just need to sync with main. thanks! |
|
there is a segmentation fault with one of the checks, please take a look. thanks! https://github.com/google-deepmind/mujoco_warp/actions/runs/24784925329/job/72532705830?pr=1283 |
|
@thowell on it! |
|
@thowell I pushed a fix for the Linux CPU segfault in Root cause was that
I considered a smaller I rechecked the targeted regressions locally on macOS and on Linux CPU (
All of those pass with this change, so this should address the CI segfault without broadening the PR beyond the failing path. |
|
|
||
| # Warp exposes mesh group-root lookup as a kernel builtin in this version. | ||
| @wp.kernel | ||
| def _compute_mesh_group_roots( |
There was a problem hiding this comment.
nit: the _ is not necessary
There was a problem hiding this comment.
@tkelestemur can we update this as @StafaH suggests? thanks!
|
@tkelestemur thank you for this contribution! |


Summary
This PR adds first-class semantic segmentation to MJWarp's renderer.
mjw.get_semantic_segmentation(...), returning per-pixel(object_id, object_type)pairs aligned with MuJoCo segmentation semantics.mjw.get_segmentation(...)API and buffer for backward compatibility.(geom_id, mjOBJ_GEOM)and flex hits as(flex_id, mjOBJ_FLEX), instead of reducing flex hits to the legacy-2sentinel only.PS: The new notebook imports
mujoco_warpdirectly from the local checkout and disables IPython autoreload for the session, since Warp kernels require file-backed Python source in notebook environments.Aside from the regular tests, I've also run
pytest contrib/kernel_analyzer/kernel_analyzer/ast_analyzer_test.py.