Skip to main content

depthpro-mlx

One photo → metric depth → 3D, dolly zoom, refocus and Vision Pro spatial photos. Fully on your Mac.

Apple's Depth Pro ported to MLX, plus cinematic effects that only work because the depth is in meters, with no GPU server and no PyTorch.

Photo to Depth Pro depth map to 3D parallax video, incense village in Vietnam

CI HF weights Apple Silicon Python

Effects

All of these come from one ordinary photo.

Refocus after the shot Dolly zoom ("vertigo")
Focus pull across a lantern street in Hoi An with round bokeh Dolly zoom: a gate of the Imperial City keeps its size while the gardens behind its arches swell
A thin-lens focus pull. Because depth is metric, the label is the real focus distance. Lights bloom into round bokeh like a fast lens. Hitchcock's camera move: pull back while zooming in, so the subject keeps its size and the background swells. Normally this needs a dolly track.
Rack focus from leaves to a red-shanked douc langur 3D parallax at Hai Van pass, leaves in front of the bay
Rack focus from foreground leaves to a red-shanked douc langur, 4.9 m → 18.9 m. 3D parallax with depth-aware disocclusion filling.

Spatial photos for Apple Vision Pro and iPhone

--spatial renders a left and a right eye 63 mm apart, the average human interpupillary distance, and writes a stereo-pair HEIC with per-eye camera intrinsics and extrinsics, the format Photos uses for spatial photos. Because the depth is metric, the stereo depth matches what you would have seen standing there. Relative-depth models can only guess this scale.

Wiggle preview (left ↔ right eye)
Left/right eye wiggle stereogram of the incense village Left/right eye wiggle stereogram of a bamboo bicycle in Hoi An

AirDrop spatial.heic to a Vision Pro or iPhone 15 Pro or later to view it in 3D. A parallel side-by-side stereo_sbs.jpg is written too.

Measure real distances in a photo

Metric depth turns every photo into something you can measure. In the viewer, press Measure and click two points:

Measuring the rear wheel of a bamboo bicycle as 66 cm

Sanity check: the rear wheel of this bamboo mountain bike measures 66 cm, and a 26″ MTB wheel with its tire is 66–67 cm across. That comes from a single photo with no reference object and no LiDAR; the focal length was estimated by the model too. Measurements parallel to the image are the reliable kind; separations along the viewing direction pick up depth noise, which grows with distance. Points at nearly the same depth are treated as one plane for that reason. Measurements are shareable: viewer.html?measure=u1,v1,u2,v2.

Video: stable metric depth and cinematic mode

Feed it a video. Depth Pro runs per frame with one focal length for the whole clip (so meters stay meters), and the depth is stabilized along optical flow, blended only where the flow is forward-backward consistent, so moving people don't leave ghosts. Then "cinematic mode" refocuses the footage like a focus puller: track keeps a subject sharp, pull racks from near to far.

Cinematic mode · Hà Nội train street Video depth · the train leaves Tracking focus · basket maker
Focus pulls from a tourist at 0.9 m to the passing train at 3.8 m Stabilized depth of a crowd on motorbikes as the train leaves Focus follows a craftsman weaving a basket
depthpro-mlx clip.mp4 --duration 6 --cinematic track --focus 0.45,0.55

Interactive 3D in the browser

▶ Try the live demo: nine scenes from Vietnam. Drag, move the mouse, or tilt your phone; press Depth or Measure.

--web writes a single self-contained viewer.html (WebGL2, no dependencies, ~3 MB) with the photo, depth and background layer embedded. It works offline from file:// and is easy to share; tools/media/build_demo.py rebuilds the demo site.

No smearing: an inpainted background layer

Moving the camera reveals things the photo never saw. Instead of stretching the subject over them, the foreground near every depth edge is removed and LaMa paints in what is behind it. That gives a second layer with its own color and depth, drawn behind the photo by every renderer: video, dolly zoom, stereo and the web viewer. Only real depth discontinuities count, so gently sloping ground is left alone. With the layer the camera can also move about 1.7× further.

Why

  • First MLX port of Depth Pro. Numerically matches the official PyTorch model (AbsRel 1.7e-6 in fp32) and runs 19% faster than PyTorch on MPS.
  • Metric depth, sharp edges. Depth in meters, with hair, leaves and fur resolved. That is what makes the 3D look real and the bokeh fall off correctly.
  • Photo → effects in one command. Point cloud (.ply), 3D parallax, dolly zoom and refocus videos, all rendered locally.
  • No smearing. A LaMa-inpainted background layer fills what the camera uncovers.
  • Video too. Stable metric depth per frame and a cinematic-mode refocus for any clip.

Quickstart

pip install "depthpro-mlx[inpaint]"
depthpro-mlx photo.jpg --effects all --spatial --web --ply

The first run downloads the converted weights (1.9 GB) from the Hub, plus LaMa (208 MB) for the background layer. Without the inpaint extra everything still works, with a blurred fill instead of an inpainted layer (--inpaint off forces that). Outputs land in photo_depthpro/:

File What
depth.png / depth.npy colorized depth / raw depth in meters
pointcloud.ply colored point cloud (open in MeshLab, Blender, Preview)
3d.mp4 parallax 3D video (--effects orbit, the default)
dolly_zoom.mp4 vertigo shot (--effects dolly), best with a clear subject and a background that isn't too far away
refocus.mp4 focus pull near → far (--effects refocus)
spatial.heic, stereo_sbs.jpg, wiggle.webp spatial photo, side-by-side stereo, wiggle preview (--spatial, needs the Swift toolchain for the HEIC)
viewer.html interactive 3D viewer, single file (--web)
video input: depth.mp4, cinematic.mp4, depth.npz stabilized depth video (one colour scale for the clip), refocused video, raw depth in meters

Add --gif for GIF copies.

Docker (Linux servers)

docker run --rm -v "$PWD:/data" -v depthpro-cache:/cache \
  ghcr.io/ntoantoan/depthpro-mlx photo.jpg --web --effects refocus

Images for linux/amd64 and linux/arm64 run MLX's CPU backend; :latest-cuda uses MLX's CUDA backend (amd64, NVIDIA GPU, --gpus all; experimental, built but not yet tested on a GPU). Build locally with docker build -t depthpro-mlx ..

Things to know:

  • On a Mac, don't use Docker. Containers run in a Linux VM without Metal. Natively, Metal takes 3 s per photo; MLX's CPU backend needs ~2.5 min (measured on M4 Pro CPU cores, the same float32 low-memory path the image uses).
  • CPU inference runs in float32 and needs about 8 GB of RAM, so give the container ≥ 10 GB (Docker Desktop: Settings → Resources). It peaks at 7.6 GB thanks to strip-wise decoding; a naive float32 run needs 15 GB.
  • The image replaces the reference BLAS bundled with mlx-cpu by OpenBLAS: float32 matmul goes from ~7 to ~400 GFLOP/s.
  • spatial.heic needs macOS (ImageIO). In the container --spatial writes the side-by-side and wiggle previews only.

Python

import numpy as np
from PIL import Image
from depthpro_mlx import load_model, predict

model = load_model()
image = np.array(Image.open("photo.jpg").convert("RGB"))
out = predict(model, image)          # pass f_px=... if you know the focal length
out["depth"]                         # (H, W) float32, meters
out["focallength_px"]                # estimated when not given

iPhone photos carry the focal length in EXIF; the CLI reads it automatically for true metric scale.

Swan boats, Ninh Bình Long corridor, Imperial City, Huế Douc langurs, Sơn Trà
3D parallax of abandoned swan boats 3D parallax of a long wooden corridor in the Imperial City of Hue 3D parallax of douc langurs on a branch

Benchmarks

MacBook Pro M4 Pro (24 GB), 3024×2268 input, median of 5 runs:

Backend Precision Time Peak memory Error vs. PyTorch fp32 (AbsRel)
PyTorch MPS (official) fp32 4.60 s 12.8 GB –
PyTorch MPS (official) fp16 3.76 s 7.0 GB –
MLX (this repo) fp32 4.17 s 15.0 GB 1.7e-6
MLX (this repo) fp16 3.06 s 7.5 GB 6.2e-4
MLX (this repo) bf16 3.06 s 7.5 GB 2.9e-3

Depth Pro runs a DINOv2-L ViT over 37 overlapping 384 px tiles, so it is compute-bound. In our tests, 8-bit and 4-bit weight quantization saved memory but ran slower (3.4 s).

Reproduce:

bash third_party/get_reference.sh            # clones apple/ml-depth-pro + checkpoint
uv sync --group reference
uv run python tools/bench/bench_torch.py --save outputs/ref_depth_fp32.npy
uv run python -m depthpro_mlx.model.convert   # torch checkpoint -> weights/depth_pro.safetensors
uv run python tools/bench/parity.py --dtype float16

How it works

  1. Depth. Depth Pro builds a 3-level image pyramid, encodes 5×5 + 3×3 + 1 overlapping tiles with a shared DINOv2-L, fuses them with a DPT-style decoder, and predicts canonical inverse depth plus the field of view. Scaling the output by the focal length gives metric depth.
  2. 3D. Pixels are unprojected with the pinhole model. Points in a thin band around depth discontinuities are dropped, since they carry foreground color at background depth ("flying pixels").
  3. Render. A z-buffered point splatter with 2× supersampling. Disocclusions are filled by a depth-aware push-pull: color and depth are packed into one key, a max-pyramid is built, and holes take the farthest surface on their border, so the background never borrows the subject's color. Camera radius is capped so near/far parallax stays under 3.5% of the frame, and the zoom-in is derived from the largest shift so the frame never leaves the photo.
  4. Dolly zoom. The camera pulls back by b while the focal length scales by (z_s + b) / z_s, keeping the subject at depth z_s fixed in size. b is capped so near content never shrinks below 85%.
  5. Refocus. Thin-lens circle of confusion r = k·|1/z − 1/z_f| on a precomputed stack of disk-blurred images in linear light, with a highlight boost for bokeh. The focus sweeps uniformly in diopters.
  6. Spatial photo. Two parallel pinhole cameras at ±31.5 mm render the eyes; a small Swift helper writes them with ImageIO as a StereoPair group with SimplifiedPinhole intrinsics and extrinsics, compiled once and cached in ~/.cache/depthpro-mlx.
  7. Web viewer. Two grid meshes (photo, and background layer behind it) unprojected from 16-bit inverse depth packed into PNGs, drawn with WebGL2. Depth is compressed around the subject (z' = pivot·(z/pivot)^γ) so a leaf at 40 cm in front of a sky doesn't take the whole parallax budget. Where a photo triangle stretches across an edge (detected from screen-space UV derivatives) it fades out and the inpainted layer shows through.
  8. Background layer. Depth steps larger than 6% between pixels 2 px apart mark discontinuities; their far-side depth is spread over 2.5% of the width, and foreground pixels nearer than it form the mask. LaMa (ONNX, 512 px tiles with feathered overlaps, at ~1024 px) fills it; the layer's depth is the far side, smoothed within the mask.
  9. Video. Focal length = median of Depth Pro's estimates on 5 frames, fixed for the clip. Log-depth is blended with the previous frame warped by DIS optical flow, weight 0.6 × (1.5 − forward-backward error in px, clipped to 0..1). Cinematic track follows the median depth around the focus point with a lagged (EMA) focus puller.

Development

See CONTRIBUTING.md: uv sync, then uv run pytest -m "not slow" (no weights needed) and uv run ruff check. CI runs on Apple Silicon GitHub runners.

Credits

  • Depth Pro by Bochkovskii et al., Apple. Weights are redistributed under Apple's license. This project is not affiliated with Apple.
  • LaMa by Suvorov et al. (Apache-2.0), via the ONNX port Carve/LaMa-ONNX.
  • Demo photos and videos: Wikimedia Commons contributors, see assets/CREDITS.md.
@inproceedings{Bochkovskii2024:arxiv,
  author    = {Aleksei Bochkovskii and Ama\"{e}l Delaunoy and Hugo Germain and Marcel Santos and
               Yichao Zhou and Stephan R. Richter and Vladlen Koltun},
  title     = {Depth Pro: Sharp Monocular Metric Depth in Less Than a Second},
  booktitle = {International Conference on Learning Representations},
  year      = {2025},
  url       = {https://arxiv.org/abs/2410.02073},
}

License

Component License
Code in this repository (and the Docker image's own layers) MIT (the model files are a port of Apple's sample code, used under its terms)
Depth Pro weights (downloaded from the Hub) Apple's Depth Pro license, redistributed unchanged except for the format conversion
LaMa weights (inpaint extra) Apache-2.0
Demo photos and videos CC0 / CC BY / CC BY-SA, see assets/CREDITS.md

The Depth Pro weights are not under an OSI-approved license. Apple grants a personal, non-exclusive license to use, modify and redistribute them, and does not explicitly address commercial use. If you plan to ship them in a product, read Apple's terms yourself and, if in doubt, ask a lawyer. This project is not affiliated with or endorsed by Apple.

Metadata

Release files for depthpro-mlx 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for depthpro-mlx 0.3.0
File Size Uploaded Provenance Actions
depthpro_mlx-0.3.0.tar.gz 53.7 kB Without provenance Details for depthpro_mlx-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for depthpro-mlx 0.3.0
File Interpreter ABI Platform Actions
depthpro_mlx-0.3.0-py3-none-any.whl Python 3 none any Details for depthpro_mlx-0.3.0-py3-none-any.whl

Total release size: 106.8 kB

Release files / depthpro_mlx-0.3.0.tar.gz

Download URL depthpro_mlx-0.3.0.tar.gz
Size 53.7 kB
Tags Source
SHA-256 checksum
How to use checksums
4e1c074038f898d0723bda514112d37784a57eee6577585276d7924c0cd38d2b
BLAKE2b-256 checksum
How to use checksums
12571d5276bcf3aa0e875e7976bf9158ad08069ded8f35a3dda81f404373dc91
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.12

Release files / depthpro_mlx-0.3.0-py3-none-any.whl

Download URL depthpro_mlx-0.3.0-py3-none-any.whl
Size 53.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5eaa8b5b85949e27d6903fd0e8a488561c76abfeed0fdf5e5e3a5b7d25966c25
BLAKE2b-256 checksum
How to use checksums
cb6f60b521f331c77ee400f79bad1918bcd2e8321f7c2d75a98d845fcaf02c98
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.12

Security for depthpro-mlx 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page