byoussef commited on
Commit
0f0dce0
·
verified ·
1 Parent(s): d45bcfa

Simpler README

Browse files
Files changed (1) hide show
  1. README.md +41 -68
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`), so that it runs on a GPU: in a browser with [LiteRT.js](https://ai.google.dev/edge/litert/web) on WebGPU, or wherever else LiteRT has a GPU delegate. One photo in, depth in meters out. For video, a second set of files runs Depth Pro 2's live mode, in which each frame also attends to the frames before it.
22
 
23
- Try it: the [demo](https://huggingface.co/spaces/byoussef/DepthPro2-WebGPU) runs these files in your browser, on a photo or the camera.
24
 
25
- This is an unofficial conversion. I am not affiliated with Apple and Apple does not endorse it. The buffered video mode and everything else are on the [official page](https://huggingface.co/apple/DepthPro2) and in the [code](https://github.com/apple-aiml-research/ml-depth-pro-2).
26
 
27
  ## Files
28
 
29
- | File | Model | Gives | Input (HxW) | Size (MB) |
30
- |---|:---:|:---|:---:|---:|
31
- | [`depthpro2-s_480x854.tflite`](https://huggingface.co/byoussef/DepthPro2-litert/blob/main/depthpro2-s_480x854.tflite) | S | depth of a photo | 480x854 | 98 |
32
- | [`depthpro2-b_480x854.tflite`](https://huggingface.co/byoussef/DepthPro2-litert/blob/main/depthpro2-b_480x854.tflite) | B | depth of a photo | 480x854 | 387 |
33
- | [`depthpro2-l_480x854.tflite`](https://huggingface.co/byoussef/DepthPro2-litert/blob/main/depthpro2-l_480x854.tflite) | L | depth of a photo | 480x854 | 1337 |
34
- | [`depthpro2-h_480x854.tflite`](https://huggingface.co/byoussef/DepthPro2-litert/blob/main/depthpro2-h_480x854.tflite) | H+ | depth of a photo | 480x854 | 3490 |
35
- | [`depthpro2-s_480x854_w4.tflite`](https://huggingface.co/byoussef/DepthPro2-litert/blob/main/depthpro2-s_480x854_w4.tflite) | S | depth of a video | 480x854 | 99 |
36
- | [`depthpro2-b_480x854_w4.tflite`](https://huggingface.co/byoussef/DepthPro2-litert/blob/main/depthpro2-b_480x854_w4.tflite) | B | depth of a video | 480x854 | 388 |
37
- | [`depthpro2-l_480x854_w4.tflite`](https://huggingface.co/byoussef/DepthPro2-litert/blob/main/depthpro2-l_480x854_w4.tflite) | L | depth of a video | 480x854 | 1339 |
38
- | [`depthpro2-h_480x854_w4.tflite`](https://huggingface.co/byoussef/DepthPro2-litert/blob/main/depthpro2-h_480x854_w4.tflite) | H+ | depth of a video | 480x854 | 3492 |
39
- | [`depthpro2-fov_480x832.tflite`](https://huggingface.co/byoussef/DepthPro2-litert/blob/main/depthpro2-fov_480x832.tflite) | Field of view | field of view | 480x832 | 95 |
40
-
41
- - **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)`, with `f_px` the focal length in pixels of the 854x480 frame.
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
- A video goes through a `_w4` file one frame at a time, each frame made as `frame` is above. The outputs of a frame go back in as the state of the next, and stay on the GPU:
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` and in float32 otherwise. `gpuOptions: {precision: 'fp32'}` next to `accelerator` forces float32.
97
- - **Field of view.** Run it in float32. Float16 moves its focal length, and with it every depth, by up to 2%.
98
- - **Browsers.** Chrome and Firefox work. Safari 27.2 fails to compile two of LiteRT.js's shaders and returns garbage.
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
- A video, with frames made as `frame` is above. Two sets of output buffers take turns: the outputs of one frame are the inputs of the next, so the state stays on the GPU.
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
- - **Install.** `pip install ai-edge-litert huggingface_hub numpy pillow`.
167
- - **GPU.** This is LiteRT's `CompiledModel` on the GPU, and the same code runs every size. A photo on an RTX A6000, copied in and read back:
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
- A frame at 480x854 in Chrome, once it is warm, with the state of a video kept on the GPU:
192
 
193
- | GPU | Model | File | float32 (ms) | float16 (ms) |
194
- |:---|:---:|:---|---:|---:|
195
- | Apple M1 | S | photo | 503 | 224 |
196
- | Apple M1 | S | video | 784 | 346 |
197
- | Apple M1 | B | photo | 1317 | 546 |
198
- | Apple M1 | B | video | 1910 | 793 |
199
- | Intel Xe-LP | S | photo | 520 | 270 |
200
- | Intel Xe-LP | B | photo | 1440 | 700 |
 
 
201
 
202
- The M1 is a MacBook Pro's, in Chrome 158. The Intel GPU is a laptop's integrated one.
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 trained weights are Apple's and the function is the same in float32. The changes:
209
 
210
  - One input size per file.
211
- - Video in the live mode only, with a window of 4 frames as in the original's Mac demo. The state is inputs and outputs, and the masking of its empty slots rides inside the matrix product of the logits. The buffered mode, which predicts clips of 16 frames together and so also sees later frames, is not exported.
212
- - The blocks are rewritten for LiteRT's GPU delegates, which take tensors of rank 4 at most and are slow on a batch: three projections in place of the fused query-key-value one, the heads of attention as a dimension after a batch of 1, and the layer scales folded into the weights before them.
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
- [`export.py`](https://huggingface.co/byoussef/DepthPro2-litert/blob/main/export.py) is the script that made the files, and its docstring has the reasons. It needs [uv](https://docs.astral.sh/uv/), and a photo or a video to calibrate and check the export on:
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.