SAM 3D Body (ViT-H, body decoder) — ONNX
This is an ONNX conversion of Meta's SAM 3D Body (facebook/sam-3d-body-vith), built for
KaleidoLab's pose editor. It estimates the 3D pose of one person per run
from a cropped image and the person's box. The person box comes from a separate detector; KaleidoLab uses RTMO.
SAM 3D Body is licensed under the SAM License (see LICENSE). This conversion is a derivative of the
SAM Materials and is distributed under the same agreement. Anyone who uses or redistributes it agrees to that license.
Source
| Item | Value |
|---|---|
| Checkpoint | facebook/sam-3d-body-vith @ f7f4ba4f67739e297f291639afb7c59041745367 |
model.ckpt SHA-256 | 3b1cb897f4bbd977bf81cbb0b30780a9582681ac642ee112865790ceb4d66056 |
assets/mhr_model.pt SHA-256 | 352e271a6c42729c68554ceaea0c955e866970160c31e35506d782dc0f7377bc |
| Code | facebookresearch/sam-3d-body @ b5c765a0d89d789985e186d396315e7590887b94 |
What was changed
The network weights are unchanged. Only the parts that cannot be exported to ONNX were rewritten so that they compute the same values:
- MHR body model. The original is a TorchScript module that uses a sparse matrix and skins all 18,439 vertices.
export/mhr_lite.pyreimplements only what is needed for the 70 keypoints in plain PyTorch: forward kinematics of the 127 joints, plus skinning and pose correctives for the 468 vertices those keypoints use. It follows the math of pymomentum (MIT) and MHR (Apache-2.0). Its keypoints match the TorchScript module within 1e-6. - ONNX-incompatible ops.
export/sam3db_onnx_patches.pyrewrites the following without changing results:- assignments to
.../boolean indices, rewritten as permutations; - the antialiased resize of the camera-ray field, replaced by an exact precomputed grid, because that field is linear in the pixel coordinates.
- assignments to
- Body decoder only. The optional hand-refinement decoder, the detector and the FOV estimator are not included.
- Weight storage. Weights are stored as float16 and cast to float32 at the start of the graph, so computation stays in float32. This keeps the file under 2 GB.
- DirectML.
Reshapenodes haveallowzeroset to 0, because DirectML rejectsallowzero=1. No shape in this graph contains 0, so the meaning is the same.
Compared with the original PyTorch model on the same input, the 2D keypoints differ by at most 0.4 px (on an 836 px image), and the 3D keypoints by at most 0.6 mm. This holds on both the CPU and DirectML execution providers. One person takes about 2 s on CPU and 0.2 s on DirectML (RTX 3070).
Export: torch 2.11.0, onnx 1.23.2, onnxscript 0.7.2 (torch.onnx.export(..., dynamo=True, opset_version=18)),
onnxruntime 1.31.0. The scripts are in export/. sam3db_export.py expects a checkout of the code revision above
with two local edits:
.cuda()calls replaced so that it runs on CPU;- the input-range
assertinprompt_encoder.pyremoved.
File
| File | Bytes | SHA-256 |
|---|---|---|
sam-3d-body-vith-body.onnx | 1,424,013,951 | c13492cf96932c6fab67bdab3ce9a983f81743e270ba1adae128d7ff4dff4378 |
Inputs (one person)
| Name | Shape | Meaning |
|---|---|---|
image | [1, 3, 512, 512] | RGB crop in 0–1. Warp the image with affine (bilinear, zero border). |
bbox_center | [1, 2] | Crop center in image pixels. |
bbox_scale | [1, 2] | Crop size in image pixels (square). |
affine | [1, 2, 3] | Image → crop transform: [[k, 0, 256 - k*cx], [0, k, 256 - k*cy]] with k = 512 / size. |
image_size | [1, 2] | Original image width, height. |
camera | [3, 3] | Intrinsics. The default used by SAM 3D Body is f = sqrt(w² + h²), c = (w/2, h/2). |
Crop from a person box x1, y1, x2, y2:
- Take the center
cand the size(x2-x1, y2-y1) × 1.25. - Widen the size to an aspect ratio of 0.75, then make it square.
Outputs
| Name | Shape | Meaning |
|---|---|---|
keypoints_2d | [1, 70, 2] | MHR-70 keypoints in original image pixels. Order: body 0–14 (COCO order without wrists), feet 15–20, right hand 21–41, left hand 42–62, extra 63–69. |
keypoints_3d | [1, 70, 3] | Same keypoints in camera space (m, before cam_t). |
cam_t | [1, 3] | Camera translation. |
focal_length | [1] | Focal length used. |
joint_coords | [1, 127, 3] | MHR skeleton joint positions (m). |
joint_rotations | [1, 127, 3, 3] | MHR global joint rotations. |
global_rot | [1, 3] | Root rotation (Euler ZYX). |
body_pose | [1, 133] | MHR body pose parameters. |
License
SAM License. See LICENSE. Notable terms:
- commercial use is allowed;
- redistribution must include a copy of the license;
- use must comply with trade controls and must not be for military, weapons or other prohibited purposes.
The scripts in export/ are provided under the same terms.