Instructions to use byoussef/DepthPro2-litert with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- LiteRT
How to use byoussef/DepthPro2-litert with LiteRT:
# No code snippets available yet for this library. # To use this model, check the repository files and the library's documentation. # Want to help? PRs adding snippets are welcome at: # https://github.com/huggingface/huggingface.js
- Notebooks
- Google Colab
- Kaggle
|
Download README.md from byoussef/DepthPro2-litert: direct link, hf CLI and curl.
- Browser
- Download file 11.1 kB
-
https://huggingface.co/byoussef/DepthPro2-litert/resolve/main/README.md
- Command line
-
hf download hf://byoussef/DepthPro2-litert/README.md
-
curl -L -o README.md https://huggingface.co/byoussef/DepthPro2-litert/resolve/main/README.md
11.1 kB
| license: other | |
| license_name: apple-amlr | |
| license_link: https://github.com/apple-aiml-research/ml-depth-pro-2/blob/main/LICENSE_MODEL | |
| pipeline_tag: depth-estimation | |
| library_name: litert | |
| base_model: apple/DepthPro2 | |
| base_model_relation: quantized | |
| tags: | |
| - depth-estimation | |
| - metric-depth | |
| - monocular-depth | |
| - video-depth-estimation | |
| - litert | |
| - tflite | |
| - webgpu | |
| # Depth Pro 2 for LiteRT | |
| [Depth Pro 2](https://huggingface.co/apple/DepthPro2) converted to LiteRT (`.tflite`) to run on a GPU: in a browser with [LiteRT.js](https://ai.google.dev/edge/litert/web) on WebGPU, or with LiteRT in Python. Each size comes as a photo file and a video file. The video file also looks at the 3 frames before the current one, so its depth flickers less. | |
| Try it in the [demo](https://huggingface.co/spaces/byoussef/DepthPro2-WebGPU), on a photo or your camera. | |
| This is an unofficial conversion. I am not affiliated with Apple, and Apple does not endorse it. | |
| ## Files | |
| | Model | Photo file | Video file | Photo file size (MB) | | |
| |:---:|---|---|---:| | |
| | S | `depthpro2-s_480x854.tflite` | `depthpro2-s_480x854_w4.tflite` | 98 | | |
| | B | `depthpro2-b_480x854.tflite` | `depthpro2-b_480x854_w4.tflite` | 387 | | |
| | L | `depthpro2-l_480x854.tflite` | `depthpro2-l_480x854_w4.tflite` | 1337 | | |
| | H+ | `depthpro2-h_480x854.tflite` | `depthpro2-h_480x854_w4.tflite` | 3490 | | |
| | Field of view | `depthpro2-fov_480x832.tflite` | | 95 | | |
| Larger models are sharper and slower. A video file is about 1 to 2 MB larger than its photo file. S and B run in a browser. L and H+ do not load in LiteRT.js 2.6.0, which copies the whole file into its WebAssembly memory and aborts: use them outside a browser, as in Python below. | |
| - **Depth.** Input `frame`: float32 (1, 3, 480, 854), RGB in [0, 1]. Output `inverse_depth`: (1, 480, 854). Depth in meters is `f_px / (854 * inverse_depth)`, where `f_px` is the focal length in pixels of the 854x480 frame. | |
| - **Video.** A video file also takes a state, its memory of the frames before (`valid`, `keys`, `values`, `prefixes`), and returns the next one (`next_valid`, `next_keys`, `next_values`, `next_prefixes`). Start from zeros, then give each frame the `next_*` outputs of the frame before. | |
| - **Field of view.** For a photo without a focal length. Input: the frame resized to 864x480, then its middle 832 columns. Output: the horizontal and vertical field of view in degrees. `f_px = 0.5 * 480 / tan(vertical / 2)`. | |
| ## In a browser | |
| ```html | |
| <canvas id="frame" width="854" height="480"></canvas> | |
| <script type="module"> | |
| import {loadLiteRt, loadAndCompile, Tensor} from 'https://cdn.jsdelivr.net/npm/@litertjs/core@2.6.0/+esm'; | |
| const repo = 'https://huggingface.co/byoussef/DepthPro2-litert/resolve/main/'; | |
| await loadLiteRt('https://cdn.jsdelivr.net/npm/@litertjs/core@2.6.0/wasm/'); | |
| const model = await loadAndCompile(repo + 'depthpro2-s_480x854.tflite', {accelerator: 'webgpu'}); | |
| // A 16:9 photo drawn on the canvas, then as RGB planes in [0, 1]. | |
| const photo = await createImageBitmap(await (await fetch('photo.jpg')).blob()); | |
| const context = document.getElementById('frame').getContext('2d'); | |
| context.drawImage(photo, 0, 0, 854, 480); | |
| const pixels = context.getImageData(0, 0, 854, 480).data; | |
| const frame = new Float32Array(3 * 480 * 854); | |
| for (let i = 0; i < 480 * 854; i++) { | |
| for (let c = 0; c < 3; c++) frame[c * 480 * 854 + i] = pixels[4 * i + c] / 255; | |
| } | |
| const [output] = await model.run(new Tensor(frame, [1, 3, 480, 854])); | |
| const inverseDepth = (await output.moveTo('wasm')).toTypedArray(); // 480 * 854 values | |
| </script> | |
| ``` | |
| For video, use the video (`_w4`) file and feed each frame's `next_*` outputs into the next frame. They stay on the GPU: | |
| ```js | |
| const video = await loadAndCompile(repo + 'depthpro2-s_480x854_w4.tflite', {accelerator: 'webgpu'}); | |
| const names = ['valid', 'keys', 'values', 'prefixes']; | |
| let state = {}; | |
| for (const {name, shape} of video.getInputDetails()) { | |
| if (names.includes(name)) state[name] = new Tensor(new Float32Array(shape.reduce((a, b) => a * b)), Array.from(shape)); | |
| } | |
| async function next(frame) { // RGB planes of one frame, as above | |
| const input = new Tensor(frame, [1, 3, 480, 854]); | |
| const outputs = await video.run({frame: input, ...state}); | |
| const onHost = await outputs.inverse_depth.moveTo('wasm'); | |
| const inverseDepth = onHost.toTypedArray().slice(); | |
| input.delete(); | |
| onHost.delete(); | |
| for (const name of names) state[name].delete(); | |
| state = Object.fromEntries(names.map((name) => [name, outputs[`next_${name}`]])); | |
| return inverseDepth; | |
| } | |
| ``` | |
| - **Precision.** LiteRT.js computes in float16 when the GPU has `shader-f16`. Add `gpuOptions: {precision: 'fp32'}` next to `accelerator` for float32. Always run the field-of-view model in float32: in float16 its focal length moves by up to 2%. | |
| - **Browsers.** Chrome and Firefox work. Safari 27.2 returns wrong depth. | |
| - **Memory.** LiteRT.js aborts once it holds about 700 MB of models, such as B in float32 and in float16 at once. Call `model.delete()` on a model you no longer need before compiling the next. | |
| ## In Python | |
| `pip install ai-edge-litert huggingface_hub numpy pillow` | |
| ```python | |
| import numpy as np | |
| from ai_edge_litert.compiled_model import CompiledModel | |
| from ai_edge_litert.hardware_accelerator import HardwareAccelerator | |
| from ai_edge_litert.options import Options | |
| from huggingface_hub import hf_hub_download | |
| from PIL import Image, ImageOps | |
| REPO = "byoussef/DepthPro2-litert" | |
| options = Options.create() | |
| options.hardware_accelerators = HardwareAccelerator.GPU | |
| options.gpu_options.enforce_f32 = True | |
| def run(name, frame, size): | |
| model = CompiledModel.from_file(hf_hub_download(REPO, name), options=options) | |
| inputs, outputs = model.create_input_buffers(0), model.create_output_buffers(0) | |
| inputs[0].write(frame) | |
| model.run_by_index(0, inputs, outputs) | |
| return outputs[0].read(size, np.float32) | |
| def planes(image): # RGB in [0, 1], shaped (1, 3, H, W) | |
| return np.asarray(image, np.float32).transpose(2, 0, 1)[None] / 255 | |
| # The frame: the photo scaled and center-cropped to 854x480. Bilinear on purpose: it is what | |
| # the original code resizes with, and a sharper filter moves the focal length below by 4%. | |
| photo = Image.open("photo.jpg").convert("RGB") | |
| frame = ImageOps.fit(photo, (854, 480), Image.Resampling.BILINEAR) | |
| inverse_depth = run("depthpro2-s_480x854.tflite", planes(frame), 480 * 854).reshape(480, 854) | |
| # The focal length, for a photo that comes without one. | |
| crop = frame.resize((864, 480), Image.Resampling.BILINEAR).crop((16, 0, 848, 480)) | |
| horizontal, vertical = run("depthpro2-fov_480x832.tflite", planes(crop), 2) # degrees | |
| focal = 0.5 * 480 / np.tan(np.radians(vertical) / 2) # pixels of the 854x480 frame | |
| depth = focal / (854 * inverse_depth) # meters; infinite where the inverse depth is 0, as in the sky | |
| ``` | |
| For video, two sets of output buffers take turns: one frame's outputs are the next frame's inputs, so the state stays on the GPU. | |
| ```python | |
| video = CompiledModel.from_file(hf_hub_download(REPO, "depthpro2-s_480x854_w4.tflite"), options=options) | |
| signature = video.get_signature_by_index(0)["key"] | |
| shapes = {name: details["shape"] for name, details in video.get_input_tensor_details(signature).items()} | |
| produced = list(video.get_output_tensor_details(signature)) | |
| inputs = video.create_input_buffers(0) | |
| for buffer, (name, shape) in zip(inputs, shapes.items()): | |
| if name != "frame": | |
| buffer.write(np.zeros(shape, np.float32)) # an empty state | |
| frame_buffer = inputs[list(shapes).index("frame")] | |
| turns = [video.create_output_buffers(0), video.create_output_buffers(0)] | |
| for index, image in enumerate(frames): # 854x480 PIL images | |
| frame_buffer.write(planes(image)) | |
| outputs = turns[index % 2] | |
| video.run_by_index(0, inputs, outputs) | |
| inverse_depth = outputs[produced.index("inverse_depth")].read(480 * 854, np.float32) | |
| inputs = [frame_buffer if name == "frame" else outputs[produced.index(f"next_{name}")] for name in shapes] | |
| ``` | |
| - **Float16.** Without the `enforce_f32` line, LiteRT computes in float16, with the times below. Keep that line for the field-of-view file, as in a browser. On an RTX PRO 6000 some float16 runs failed with a lost Vulkan device, so try it on your GPU first. | |
| - **CPU.** Replace the two GPU lines with `options.cpu_options.num_threads = 8`. A photo then takes 0.3 s with S and 8 s with H+. | |
| ## Speed and accuracy | |
| Time for one photo at 480x854, after the first run. A video frame takes 1.3 to 1.6 times as long. | |
| | GPU | Runtime | Model | float32 (ms) | float16 (ms) | | |
| |:---|:---|:---:|---:|---:| | |
| | Apple M1 | LiteRT.js (Chrome) | S | 503 | 224 | | |
| | Apple M1 | LiteRT.js (Chrome) | B | 1317 | 546 | | |
| | Intel Xe-LP (laptop) | LiteRT.js (Chrome) | S | 520 | 270 | | |
| | Intel Xe-LP (laptop) | LiteRT.js (Chrome) | B | 1440 | 700 | | |
| | RTX 3090 | LiteRT (Python) | S | 34 | 22 | | |
| | RTX 3090 | LiteRT (Python) | B | 62 | 36 | | |
| | RTX 3090 | LiteRT (Python) | L | 174 | 84 | | |
| | RTX 3090 | LiteRT (Python) | H+ | 326 | 142 | | |
| In float32 the output matches the original PyTorch model to within 0.01% of its largest value. In float16 a typical pixel's inverse depth is off by 0.2% to 1.2%, depending on the GPU and the model. | |
| ## What is different from the original | |
| These files are Model Derivatives of Apple's Depth Pro 2 under its license. The weights are Apple's, and in float32 the function is the same. The changes: | |
| - One input size per file. | |
| - Video in the live mode only, over 4 frames. The buffered mode, which predicts clips of 16 frames, is not exported. | |
| - The network is rewritten for LiteRT's GPU delegates and for float16. The docstring of [`export.py`](https://huggingface.co/byoussef/DepthPro2-litert/blob/main/export.py), the script that made the files, explains each change. | |
| To make the files again (it needs [uv](https://docs.astral.sh/uv/)): | |
| ```bash | |
| uv run export.py depth -m depthpro2-s --no-center --groups 8 -i photo.jpg | |
| uv run export.py depth -m depthpro2-b -i photo.jpg | |
| uv run export.py depth -m depthpro2-l -i photo.jpg | |
| uv run export.py depth -m depthpro2-h -i photo.jpg # takes 18 GB of memory | |
| uv run export.py fov -s 480x832 -i photo.jpg | |
| uv run export.py stream -m depthpro2-s --no-center --groups 8 --cross-groups 32 -i video.mp4 | |
| uv run export.py stream -m depthpro2-b -i video.mp4 | |
| uv run export.py stream -m depthpro2-l -i video.mp4 | |
| uv run export.py stream -m depthpro2-h -i video.mp4 | |
| ``` | |
| ## License | |
| Apple Machine Learning Research Model is licensed under the Apple Machine Learning Research Model License Agreement. The agreement is in [LICENSE](https://huggingface.co/byoussef/DepthPro2-litert/blob/main/LICENSE). It allows research use only: non-commercial scientific research and academic development. | |
| ## Citation | |
| ```bibtex | |
| @article{DepthPro2:2026:arxiv, | |
| title = {Depth Pro 2: Sharp and Temporally Consistent Metric Monocular Depth}, | |
| author = {Peiyun Hu and Marko Mihajlovic and Lyndon Duong and Lahav Lipson and Wei Dong and Ama\"{e}l Delaunoy and Stephan R. Richter and Vladlen Koltun}, | |
| journal = {arXiv preprint arXiv:2611.26603}, | |
| year = {2026}, | |
| url = {https://arxiv.org/abs/2611.26603}, | |
| } | |
| ``` | |