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
Simpler README
Browse files
README.md
CHANGED
|
@@ -18,31 +18,27 @@ tags:
|
|
| 18 |
|
| 19 |
# Depth Pro 2 for LiteRT
|
| 20 |
|
| 21 |
-
[Depth Pro 2](https://huggingface.co/apple/DepthPro2) converted to LiteRT (`.tflite`)
|
| 22 |
|
| 23 |
-
Try it
|
| 24 |
|
| 25 |
-
This is an unofficial conversion. I am not affiliated with Apple and Apple does not endorse it.
|
| 26 |
|
| 27 |
## Files
|
| 28 |
|
| 29 |
-
|
|
| 30 |
-
|
|
| 31 |
-
|
|
| 32 |
-
|
|
| 33 |
-
|
|
| 34 |
-
|
|
| 35 |
-
|
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
- **
|
| 42 |
-
- **Video.** The `_w4` files run the live mode with a window of 4 frames: each frame attends to the 3 before it. LiteRT keeps nothing between runs, so the state is inputs and outputs. Next to `frame` they take `valid`, `keys`, `values` and `prefixes`, and next to `inverse_depth` they give `next_valid`, `next_keys`, `next_values` and `next_prefixes`. Start a video with the state all zeros, then give each frame the `next_*` outputs of the frame before. The state takes 57 MiB with S, 114 MiB with B, 304 MiB with L and 523 MiB with H+.
|
| 43 |
-
- **Video files on photos.** From an empty state a video file gives what the photo file gives in float32, to a few millionths of the largest value, so one file can do both. A frame takes 1.3 to 1.6 times as long as with the photo file.
|
| 44 |
-
- **Field of view.** For photos that come without a focal length. Input: the frame resized to 864x480 and cropped to its middle 832 columns. Output `fov`: the horizontal and vertical field of view in degrees, and `f_px = 0.5 * 480 / tan(vertical / 2)`.
|
| 45 |
-
- **Sizes.** Each size is sharper than the one before it, and slower. S and B run in a browser. L and H+ do not, in any browser: LiteRT.js 2.6.0 copies the whole file into its WebAssembly memory, which it caps at 2 GiB, and aborts while it loads L. They are for LiteRT outside the browser, as in the Python example below.
|
| 46 |
|
| 47 |
## In a browser
|
| 48 |
|
|
@@ -70,7 +66,7 @@ This is an unofficial conversion. I am not affiliated with Apple and Apple does
|
|
| 70 |
</script>
|
| 71 |
```
|
| 72 |
|
| 73 |
-
|
| 74 |
|
| 75 |
```js
|
| 76 |
const video = await loadAndCompile(repo + 'depthpro2-s_480x854_w4.tflite', {accelerator: 'webgpu'});
|
|
@@ -93,13 +89,14 @@ async function next(frame) { // RGB planes of one frame, as above
|
|
| 93 |
}
|
| 94 |
```
|
| 95 |
|
| 96 |
-
- **Precision.** LiteRT computes in float16 when the GPU has `shader-f16`
|
| 97 |
-
- **
|
| 98 |
-
- **
|
| 99 |
-
- **Memory.** LiteRT.js aborts once it holds about 700 MB of compiled models. To switch B between float32 and float16, call `model.delete()` on one before compiling the other.
|
| 100 |
|
| 101 |
## In Python
|
| 102 |
|
|
|
|
|
|
|
| 103 |
```python
|
| 104 |
import numpy as np
|
| 105 |
from ai_edge_litert.compiled_model import CompiledModel
|
|
@@ -141,7 +138,7 @@ focal = 0.5 * 480 / np.tan(np.radians(vertical) / 2) # pixels of the 854x480 fr
|
|
| 141 |
depth = focal / (854 * inverse_depth) # meters; infinite where the inverse depth is 0, as in the sky
|
| 142 |
```
|
| 143 |
|
| 144 |
-
|
| 145 |
|
| 146 |
```python
|
| 147 |
video = CompiledModel.from_file(hf_hub_download(REPO, "depthpro2-s_480x854_w4.tflite"), options=options)
|
|
@@ -163,57 +160,35 @@ for index, image in enumerate(frames): # 854x480 PIL images
|
|
| 163 |
inputs = [frame_buffer if name == "frame" else outputs[produced.index(f"next_{name}")] for name in shapes]
|
| 164 |
```
|
| 165 |
|
| 166 |
-
- **
|
| 167 |
-
- **
|
| 168 |
-
|
| 169 |
-
| Model | float32 (ms) | float16 (ms) | GPU memory in float32 (MiB) |
|
| 170 |
-
|:---:|---:|---:|---:|
|
| 171 |
-
| S | 33 | 22 | 378 |
|
| 172 |
-
| B | 60 | 36 | 869 |
|
| 173 |
-
| L | 168 | 79 | 2005 |
|
| 174 |
-
| H+ | 331 | 136 | 4137 |
|
| 175 |
-
|
| 176 |
-
Loading takes 1 s for S and 10 s for H+, and the first frame 3 to 5 times as long as the ones after it. Versions 2.3.0 and 2.4.0 of `ai-edge-litert` gave the same output, bit for bit, in the same time.
|
| 177 |
-
- **Video.** A frame of a video, with the state kept on the GPU, takes 1.3 to 1.5 times as long as a photo. On an RTX 3090:
|
| 178 |
-
|
| 179 |
-
| Model | photo, float32 (ms) | video, float32 (ms) | photo, float16 (ms) | video, float16 (ms) |
|
| 180 |
-
|:---:|---:|---:|---:|---:|
|
| 181 |
-
| S | 34 | 49 | 22 | 33 |
|
| 182 |
-
| B | 62 | 86 | 36 | 52 |
|
| 183 |
-
| L | 174 | 238 | 84 | 126 |
|
| 184 |
-
| H+ | 326 | 438 | 142 | 211 |
|
| 185 |
-
|
| 186 |
-
- **Float16.** `enforce_f32` keeps the GPU in float32. Without that line LiteRT computes in float16, with the times above. It ran every time on the RTX A6000 and the RTX 3090. On an RTX PRO 6000 it lost the Vulkan device in some runs, so try it on your GPU first.
|
| 187 |
-
- **CPU.** Replace the two GPU lines with `options.cpu_options.num_threads = 8`: a photo takes 0.3 s with S, 0.9 s with B, 3.3 s with L and 8 s with H+. Left at one thread, S takes 2 s.
|
| 188 |
|
| 189 |
## Speed and accuracy
|
| 190 |
|
| 191 |
-
|
| 192 |
|
| 193 |
-
| GPU |
|
| 194 |
-
|:---|:---
|
| 195 |
-
| Apple M1 |
|
| 196 |
-
| Apple M1 |
|
| 197 |
-
|
|
| 198 |
-
|
|
| 199 |
-
|
|
| 200 |
-
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
In float32 the output is within 0.01% of the PyTorch model's, relative to its largest value: the photo files against a photo, the video files against the original's live mode over 8 frames. In float16 the inverse depth is off by a median of 0.7% with S and 0.3% with B in Chrome on the M1, and of 1.0% with S and 1.2% with B on the Intel GPU, as the average of five photos. Over 31 frames of a video, the video files averaged 0.21% with S and 0.25% with B on the M1, and stayed under 0.4% on every frame. With LiteRT in Python, the median over a frame averaged 0.3% to 0.6% with the photo files on the RTX A6000, by size, and reached 1.4% on the worst of 40 frames; with the video files on the RTX 3090 it averaged 0.16% to 0.64% over 8 frames, by size.
|
| 205 |
|
| 206 |
## What is different from the original
|
| 207 |
|
| 208 |
-
These files are Model Derivatives of Apple's Depth Pro 2 under its license. The
|
| 209 |
|
| 210 |
- One input size per file.
|
| 211 |
-
- Video in the live mode only,
|
| 212 |
-
- The
|
| 213 |
-
- Three changes keep float16 from overflowing or losing precision, none of which changes float32 by more than rounding: layer normalization scales each row before squaring it, the residual stream and the first blocks' logits are divided by powers of two, and attention takes its keys and values relative to their means over the tokens. S is the exception to the last: its attention sums its values in 8 groups of keys instead, which was closer to float32 on the Intel GPU.
|
| 214 |
-
- In the video files the attention across frames sums its values in 8 groups of keys, 32 in S. In one sum, B's needs a texture taller than the 8192 rows that WebGPU guarantees, and gives wrong depth on a GPU at that limit.
|
| 215 |
|
| 216 |
-
|
| 217 |
|
| 218 |
```bash
|
| 219 |
uv run export.py depth -m depthpro2-s --no-center --groups 8 -i photo.jpg
|
|
@@ -227,8 +202,6 @@ uv run export.py stream -m depthpro2-l -i video.mp4
|
|
| 227 |
uv run export.py stream -m depthpro2-h -i video.mp4
|
| 228 |
```
|
| 229 |
|
| 230 |
-
`-s HxW` exports another size, and `-w` another window for `stream`. `--input uint8` exports a model that takes the photo's bytes, RGB in [0, 255], in place of floats: LiteRT's GPU accelerator copies a frame over 4 MB in slowly, which the floats of 480x854 are, and with bytes a frame took 2 to 5 ms less.
|
| 231 |
-
|
| 232 |
## License
|
| 233 |
|
| 234 |
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.
|
|
|
|
| 18 |
|
| 19 |
# Depth Pro 2 for LiteRT
|
| 20 |
|
| 21 |
+
[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.
|
| 22 |
|
| 23 |
+
Try it in the [demo](https://huggingface.co/spaces/byoussef/DepthPro2-WebGPU), on a photo or your camera.
|
| 24 |
|
| 25 |
+
This is an unofficial conversion. I am not affiliated with Apple, and Apple does not endorse it.
|
| 26 |
|
| 27 |
## Files
|
| 28 |
|
| 29 |
+
| Model | Photo file | Video file | Photo file size (MB) |
|
| 30 |
+
|:---:|---|---|---:|
|
| 31 |
+
| S | `depthpro2-s_480x854.tflite` | `depthpro2-s_480x854_w4.tflite` | 98 |
|
| 32 |
+
| B | `depthpro2-b_480x854.tflite` | `depthpro2-b_480x854_w4.tflite` | 387 |
|
| 33 |
+
| L | `depthpro2-l_480x854.tflite` | `depthpro2-l_480x854_w4.tflite` | 1337 |
|
| 34 |
+
| H+ | `depthpro2-h_480x854.tflite` | `depthpro2-h_480x854_w4.tflite` | 3490 |
|
| 35 |
+
| Field of view | `depthpro2-fov_480x832.tflite` | | 95 |
|
| 36 |
+
|
| 37 |
+
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.
|
| 38 |
+
|
| 39 |
+
- **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.
|
| 40 |
+
- **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.
|
| 41 |
+
- **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)`.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
## In a browser
|
| 44 |
|
|
|
|
| 66 |
</script>
|
| 67 |
```
|
| 68 |
|
| 69 |
+
For video, use the video (`_w4`) file and feed each frame's `next_*` outputs into the next frame. They stay on the GPU:
|
| 70 |
|
| 71 |
```js
|
| 72 |
const video = await loadAndCompile(repo + 'depthpro2-s_480x854_w4.tflite', {accelerator: 'webgpu'});
|
|
|
|
| 89 |
}
|
| 90 |
```
|
| 91 |
|
| 92 |
+
- **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%.
|
| 93 |
+
- **Browsers.** Chrome and Firefox work. Safari 27.2 returns wrong depth.
|
| 94 |
+
- **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.
|
|
|
|
| 95 |
|
| 96 |
## In Python
|
| 97 |
|
| 98 |
+
`pip install ai-edge-litert huggingface_hub numpy pillow`
|
| 99 |
+
|
| 100 |
```python
|
| 101 |
import numpy as np
|
| 102 |
from ai_edge_litert.compiled_model import CompiledModel
|
|
|
|
| 138 |
depth = focal / (854 * inverse_depth) # meters; infinite where the inverse depth is 0, as in the sky
|
| 139 |
```
|
| 140 |
|
| 141 |
+
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.
|
| 142 |
|
| 143 |
```python
|
| 144 |
video = CompiledModel.from_file(hf_hub_download(REPO, "depthpro2-s_480x854_w4.tflite"), options=options)
|
|
|
|
| 160 |
inputs = [frame_buffer if name == "frame" else outputs[produced.index(f"next_{name}")] for name in shapes]
|
| 161 |
```
|
| 162 |
|
| 163 |
+
- **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.
|
| 164 |
+
- **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+.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
## Speed and accuracy
|
| 167 |
|
| 168 |
+
Time for one photo at 480x854, after the first run. A video frame takes 1.3 to 1.6 times as long.
|
| 169 |
|
| 170 |
+
| GPU | Runtime | Model | float32 (ms) | float16 (ms) |
|
| 171 |
+
|:---|:---|:---:|---:|---:|
|
| 172 |
+
| Apple M1 | LiteRT.js (Chrome) | S | 503 | 224 |
|
| 173 |
+
| Apple M1 | LiteRT.js (Chrome) | B | 1317 | 546 |
|
| 174 |
+
| Intel Xe-LP (laptop) | LiteRT.js (Chrome) | S | 520 | 270 |
|
| 175 |
+
| Intel Xe-LP (laptop) | LiteRT.js (Chrome) | B | 1440 | 700 |
|
| 176 |
+
| RTX 3090 | LiteRT (Python) | S | 34 | 22 |
|
| 177 |
+
| RTX 3090 | LiteRT (Python) | B | 62 | 36 |
|
| 178 |
+
| RTX 3090 | LiteRT (Python) | L | 174 | 84 |
|
| 179 |
+
| RTX 3090 | LiteRT (Python) | H+ | 326 | 142 |
|
| 180 |
|
| 181 |
+
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.
|
|
|
|
|
|
|
| 182 |
|
| 183 |
## What is different from the original
|
| 184 |
|
| 185 |
+
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:
|
| 186 |
|
| 187 |
- One input size per file.
|
| 188 |
+
- Video in the live mode only, over 4 frames. The buffered mode, which predicts clips of 16 frames, is not exported.
|
| 189 |
+
- 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.
|
|
|
|
|
|
|
| 190 |
|
| 191 |
+
To make the files again (it needs [uv](https://docs.astral.sh/uv/)):
|
| 192 |
|
| 193 |
```bash
|
| 194 |
uv run export.py depth -m depthpro2-s --no-center --groups 8 -i photo.jpg
|
|
|
|
| 202 |
uv run export.py stream -m depthpro2-h -i video.mp4
|
| 203 |
```
|
| 204 |
|
|
|
|
|
|
|
| 205 |
## License
|
| 206 |
|
| 207 |
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.
|