Repository navigation
Description
Part of #2382
Problem / Background
Port SAM 3D Objects (sam3d_objects, Meta): reconstruct a 3D object from one image and an object mask, returning pose, a sparse voxel latent, a textured triangle mesh and 3D Gaussians. mlxcel has no geometry output of any kind, so this issue adds the mesh and Gaussian output surface on top of the dense-prediction infrastructure, plus the model itself. It conditions on DINOv2 features and, when no point map is supplied, estimates one with MoGe-3, so it depends on both of those issues.
Upstream reference
- Model directory:
mlx_vlm/models/sam3d_objects(README). PR: feat: SAM 3D Objects for MLX Neywa-Labs/mlx-vlm#2282. - Bundle:
mlx-community/sam-3d-objects-bf16(weights plus metadata, loaded from a local directory byPipeline.from_pretrained). - Stages (README code map):
processing.pybuilds object crops and full-image views, normalizes the point map (+X left, +Y up, +Z forward, NaN invalid) and decodes pose;depth.pyestimates a point map through the shared MoGe-3 model when the bundle has a depth model andestimate_depthis on;vision.pywraps the shared DINOv2 (ViT-L with registers, also importing itsAttentionandMlp) and embeds point maps;flow.pyStructureFlow(sparse structure and pose,ss) andLatentFlow(sparse latent features,slat);pipeline.pyintegrates both stages (Pipeline._run);decoders.pyoccupancy, Gaussian and mesh heads;mesh.pyplusmesh_tables.pyFlexiCubes triangle extraction;sparse.pygrid topology, sparse conv, pooling and window attention. - Sampling defaults in
config.py#L80-L84:ss_steps25,slat_steps25,ss_guidance7.0,slat_guidance1.0. - CLI
generate.py#L114-L195:--image,--mask(alpha used when omitted),--pointmap(safetensors withpoints),--seed42,--ss-steps,--slat-steps,--formats gaussian|gaussian_4|mesh(default gaussian and mesh),--no-depth,--output DIR; writesresult.safetensorsand PLY/OBJ.write_gaussiansconverts scales to log scale and opacities to logits;write_objwrites RGB vertex colors and 1-based faces. Geometry stays in object-local coordinates;pose(WXYZ quaternion, rotation matrix, translation, scale) is returned separately.
Current Behavior
- No mesh or point-cloud writer and no
OutputKindfor geometry insrc/models/registry.rs:42-48. model_typesam3d_objectsis rejected byget_model_type(src/models/detection.rs:1149).mlxcel predict, the dense writer and interpolation come from feat: Add dense-prediction output surface: mlxcel predict, map and keypoint files #2349, DINOv2 from feat: Port DINOv2 backbone (dinov2, dinov2_with_registers) for dense models #2350, MoGe-3 from feat: Port MoGe-3 (moge3) monocular geometry: point, depth, normal maps #2355.
Proposed Solution
- Output surface in
src/vision/dense/geometry_io.rs:write_obj(path, &Mesh)(vertices, zero-based faces converted to 1-based,v x y z r g bfrom the first three of six vertex-color channels) andwrite_gaussian_ply(path, &Gaussians)(binary little-endian f32 PLY with fieldsx y z nx ny nz f_dc_0 f_dc_1 f_dc_2 opacity scale_0 scale_1 scale_2 rot_0 rot_1 rot_2 rot_3, normals zero, opacity clipped to [1e-7, 1 - 1e-7] then logit, scales as log), matching upstreamwrite_obj/write_gaussiansfield order.Mesh { vertices: (N, 3), faces: (F, 3) u32, vertex_colors: (N, 6) },Gaussians { positions, sh_dc, scales, rotations, opacities }. AddOutputKind::Meshto the registry and extendDenseOutputfrom feat: Add dense-prediction output surface: mlxcel predict, map and keypoint files #2349 withmeshes: Vec<(String, Mesh)>andgaussians: Vec<(String, Gaussians)>so the writer emitsmesh.obj,gaussian.plyandgaussian_4.ply. - CLI: add
--mask <PATH>and--pointmap <PATH>tomlxcel predict(used only by families that declare them; others reject them). Options through--opts:seed,ss_steps,slat_steps,formats,estimate_depth(default true). - Model in
src/vision/dense/sam3d_objects/(config.rs,bundle.rs,processing.rs,vision.rs,flow.rs,sparse.rs,decoders.rs,mesh.rs,mesh_tables.rs,pipeline.rs,mod.rs), registered assam3d_objects. Conditioners reuseDinov2(Attention,Mlp) from feat: Port DINOv2 backbone (dinov2, dinov2_with_registers) for dense models #2350; depth estimation callsMoge3::estimatefrom feat: Port MoGe-3 (moge3) monocular geometry: point, depth, normal maps #2355 and converts its camera axes while preserving metric scale. result.safetensorsholdspose.*,coords,latents,pointmap_conditioned;meta.jsonrecords seeds, steps and whether a point map conditioned the run.
Scope
In scope: mesh and Gaussian writers, OutputKind::Mesh, the --mask / --pointmap flags, the full model, docs (docs/dense-prediction.md geometry section, docs/supported-models.md).
Out of scope: JSONL batch mode and astream (follow-up), scene placement by applying pose to exported geometry, conversion tooling (convert.py), a server endpoint.
Implementation Notes
- Reuse: feat: Add dense-prediction output surface: mlxcel predict, map and keypoint files #2349 writer, interpolation and conv-permutation guard; feat: Port DINOv2 backbone (dinov2, dinov2_with_registers) for dense models #2350 DINOv2; feat: Port MoGe-3 (moge3) monocular geometry: point, depth, normal maps #2355 MoGe-3. Upstream shares identical DINOv2 backbones only after checking parameter equality; do the same rather than assuming.
- Sparse ops: upstream
sam3d_objects/sparse.pyis a separate module from MoGe-3's submanifold conv; port it here and do not force-merge the two. - Edge cases: mask shape differing from the image errors; foreground spanning fewer than two rows or columns errors (upstream rule); no mask and no alpha errors; NaN in a supplied point map marks invalid points;
estimate_depthfalse or no depth model in the bundle uses the trained point-map dropout path and setspointmap_conditionedfalse. - Determinism: same seed gives the same occupancy grid; flow noise uses the MLX RNG in upstream draw order so parity runs compare.
Acceptance Criteria
-
mlxcel predict -m models/sam-3d-objects-bf16 --image chair.png --mask chair_mask.png -o out/writesmesh.obj,gaussian.ply,result.safetensorsandmeta.json; the OBJ opens in a standard viewer and the PLY loads in a Gaussian-splat viewer. - RGBA input without
--maskuses the alpha channel;--opts '{"estimate_depth":false}'setspointmap_conditionedfalse. - Parity with seed 42 and default steps: identical occupancy
coordsto upstream,latentsrelative RMS below 1e-2, mesh vertex count within 1% and Chamfer distance below 1e-3 of the object scale. - Unit tests: OBJ writer emits 1-based faces and RGB colors; PLY writer round-trips log-scale and logit fields against upstream
write_gaussianson a 3-point fixture; mask validation; FlexiCubes extraction on a sphere SDF fixture; each fails on current main. -
mlxcel archlistssam3d_objectswith output kindmesh;docs/supported-models.mdupdated.
Verification
cargo build --release --features metal,accelerate
cargo test --workspace --profile test-fast --features metal,accelerate sam3d_objects
cargo test --workspace --profile test-fast --features metal,accelerate geometry_io
cargo test --workspace --profile test-fast --features metal,accelerate
cargo clippy --workspace --all-targets -- -D warnings
cargo fmt --check
./target/release/mlxcel predict -m models/sam-3d-objects-bf16 --image /path/to/chair.png --mask /path/to/chair_mask.png -o /tmp/sam3d-outDependencies
Blocked by #2349 (dense output surface), #2350 (DINOv2) and #2355 (MoGe-3 depth estimation). Re-scopes the 3D-geometry output entry of #524.
Activity
fix: borrow fallible Objects arrays through their MLX values
fix: resolve Objects test compilation and lint diagnostics
test: authenticate full Objects reference and capture receipts
fix: admit the actual Objects depth grid and trace sampling
fix: preserve Objects joint sampling evaluation boundaries
fix: synchronize ROCm LayerNorm mean snapshot
update: cover ROCm LayerNorm mean synchronization
update: capture default Objects conditioning boundaries
fix: initialize parity server model path directly
feat: add SAM 3D Objects reconstruction and geometry exports (lablup#…