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

Add the live-video files (S, B, L, H+) and their README section

Browse files
README.md CHANGED
@@ -10,6 +10,7 @@ tags:
10
  - depth-estimation
11
  - metric-depth
12
  - monocular-depth
 
13
  - litert
14
  - tflite
15
  - webgpu
@@ -17,25 +18,31 @@ tags:
17
 
18
  # Depth Pro 2 for LiteRT
19
 
20
- [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.
21
 
22
  Try it: the [demo](https://huggingface.co/spaces/byoussef/DepthPro2-WebGPU) runs these files in your browser, on a photo or the camera.
23
 
24
- This is an unofficial conversion. I am not affiliated with Apple and Apple does not endorse it. Video 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).
25
 
26
  ## Files
27
 
28
- | File | Model | Input (HxW) | Size (MB) |
29
- |---|:---:|:---:|---:|
30
- | [`depthpro2-s_480x854.tflite`](https://huggingface.co/byoussef/DepthPro2-litert/blob/main/depthpro2-s_480x854.tflite) | S | 480x854 | 98 |
31
- | [`depthpro2-b_480x854.tflite`](https://huggingface.co/byoussef/DepthPro2-litert/blob/main/depthpro2-b_480x854.tflite) | B | 480x854 | 387 |
32
- | [`depthpro2-l_480x854.tflite`](https://huggingface.co/byoussef/DepthPro2-litert/blob/main/depthpro2-l_480x854.tflite) | L | 480x854 | 1337 |
33
- | [`depthpro2-h_480x854.tflite`](https://huggingface.co/byoussef/DepthPro2-litert/blob/main/depthpro2-h_480x854.tflite) | H+ | 480x854 | 3490 |
34
- | [`depthpro2-fov_480x832.tflite`](https://huggingface.co/byoussef/DepthPro2-litert/blob/main/depthpro2-fov_480x832.tflite) | Field of view | 480x832 | 95 |
 
 
 
 
35
 
36
  - **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.
 
 
37
  - **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)`.
38
- - **Sizes.** Each size is sharper than the one before it, and slower. S and B run in a browser. L and H+ do not: LiteRT.js 2.6.0 aborts while it loads L, and H+ is larger. They are for LiteRT outside the browser, as in the Python example below.
39
 
40
  ## In a browser
41
 
@@ -63,6 +70,29 @@ This is an unofficial conversion. I am not affiliated with Apple and Apple does
63
  </script>
64
  ```
65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  - **Precision.** LiteRT computes in float16 when the GPU has `shader-f16` and in float32 otherwise. `gpuOptions: {precision: 'fp32'}` next to `accelerator` forces float32.
67
  - **Field of view.** Run it in float32. Float16 moves its focal length, and with it every depth, by up to 2%.
68
  - **Browsers.** Chrome and Firefox work. Safari 27.2 fails to compile two of LiteRT.js's shaders and returns garbage.
@@ -111,8 +141,30 @@ focal = 0.5 * 480 / np.tan(np.radians(vertical) / 2) # pixels of the 854x480 fr
111
  depth = focal / (854 * inverse_depth) # meters; infinite where the inverse depth is 0, as in the sky
112
  ```
113
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
  - **Install.** `pip install ai-edge-litert huggingface_hub numpy pillow`.
115
- - **GPU.** This is LiteRT's `CompiledModel` on the GPU, and the same code runs every size. A frame on an RTX A6000, copied in and read back:
116
 
117
  | Model | float32 (ms) | float16 (ms) | GPU memory in float32 (MiB) |
118
  |:---:|---:|---:|---:|
@@ -122,30 +174,46 @@ depth = focal / (854 * inverse_depth) # meters; infinite where the inverse dept
122
  | H+ | 331 | 136 | 4137 |
123
 
124
  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.
125
- - **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. On an RTX PRO 6000 it lost the Vulkan device in some runs, so try it on your GPU first.
126
- - **CPU.** Replace the two GPU lines with `options.cpu_options.num_threads = 8`: a frame 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.
 
 
 
 
 
 
 
 
 
127
 
128
  ## Speed and accuracy
129
 
130
- A frame at 480x854 in Chrome, on a laptop's Intel integrated GPU (Xe-LP) once it is warm:
 
 
 
 
 
 
 
 
 
131
 
132
- | GPU | Model | float32 (ms) | float16 (ms) |
133
- |:---|:---:|---:|---:|
134
- | Intel Xe-LP | S | 520 | 270 |
135
- | Intel Xe-LP | B | 1440 | 700 |
136
 
137
- In float32 the output is within 0.01% of the PyTorch model's, relative to its largest value. In float16 the inverse depth is off by a median of 1.0% with S and 1.2% with B in Chrome on the Intel GPU, as the average of five photos. With LiteRT in Python on the RTX A6000, the median over a frame averaged 0.3% to 0.6%, by size, and reached 1.4% on the worst of 40 frames.
138
 
139
  ## What is different from the original
140
 
141
  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:
142
 
143
- - One frame at a time. The attention across video frames and its state are not exported.
144
  - One input size per file.
 
145
  - 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.
146
  - 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.
 
147
 
148
- [`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 to calibrate and check the export on:
149
 
150
  ```bash
151
  uv run export.py depth -m depthpro2-s --no-center --groups 8 -i photo.jpg
@@ -153,9 +221,13 @@ uv run export.py depth -m depthpro2-b -i photo.jpg
153
  uv run export.py depth -m depthpro2-l -i photo.jpg
154
  uv run export.py depth -m depthpro2-h -i photo.jpg # takes 18 GB of memory
155
  uv run export.py fov -s 480x832 -i photo.jpg
 
 
 
 
156
  ```
157
 
158
- `-s HxW` exports another size. `--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.
159
 
160
  ## License
161
 
 
10
  - depth-estimation
11
  - metric-depth
12
  - monocular-depth
13
+ - video-depth-estimation
14
  - litert
15
  - tflite
16
  - webgpu
 
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
  </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'});
77
+ const names = ['valid', 'keys', 'values', 'prefixes'];
78
+ let state = {};
79
+ for (const {name, shape} of video.getInputDetails()) {
80
+ if (names.includes(name)) state[name] = new Tensor(new Float32Array(shape.reduce((a, b) => a * b)), Array.from(shape));
81
+ }
82
+
83
+ async function next(frame) { // RGB planes of one frame, as above
84
+ const input = new Tensor(frame, [1, 3, 480, 854]);
85
+ const outputs = await video.run({frame: input, ...state});
86
+ const onHost = await outputs.inverse_depth.moveTo('wasm');
87
+ const inverseDepth = onHost.toTypedArray().slice();
88
+ input.delete();
89
+ onHost.delete();
90
+ for (const name of names) state[name].delete();
91
+ state = Object.fromEntries(names.map((name) => [name, outputs[`next_${name}`]]));
92
+ return inverseDepth;
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.
 
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)
148
+ signature = video.get_signature_by_index(0)["key"]
149
+ shapes = {name: details["shape"] for name, details in video.get_input_tensor_details(signature).items()}
150
+ produced = list(video.get_output_tensor_details(signature))
151
+ inputs = video.create_input_buffers(0)
152
+ for buffer, (name, shape) in zip(inputs, shapes.items()):
153
+ if name != "frame":
154
+ buffer.write(np.zeros(shape, np.float32)) # an empty state
155
+ frame_buffer = inputs[list(shapes).index("frame")]
156
+ turns = [video.create_output_buffers(0), video.create_output_buffers(0)]
157
+
158
+ for index, image in enumerate(frames): # 854x480 PIL images
159
+ frame_buffer.write(planes(image))
160
+ outputs = turns[index % 2]
161
+ video.run_by_index(0, inputs, outputs)
162
+ inverse_depth = outputs[produced.index("inverse_depth")].read(480 * 854, np.float32)
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
  |:---:|---:|---:|---:|
 
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
 
221
  uv run export.py depth -m depthpro2-l -i photo.jpg
222
  uv run export.py depth -m depthpro2-h -i photo.jpg # takes 18 GB of memory
223
  uv run export.py fov -s 480x832 -i photo.jpg
224
+ uv run export.py stream -m depthpro2-s --no-center --groups 8 --cross-groups 32 -i video.mp4
225
+ uv run export.py stream -m depthpro2-b -i video.mp4
226
+ 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
 
depthpro2-b_480x854_w4.tflite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:15aaa24473781664d974449ab0a1baae833c511631d4fadf277d97fb3a7cfda2
3
+ size 388424320
depthpro2-h_480x854_w4.tflite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f8941580d5f52574475f9ecc53699b49ac479d2a5c081a333577cf0a05725b29
3
+ size 3491884176
depthpro2-l_480x854_w4.tflite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3c4a050cc5b952bd90e2ddc0056c42307eaf102bb6a44aac60568a38bbb7abe3
3
+ size 1338756712
depthpro2-s_480x854_w4.tflite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4f40e467bd59af68924edc1929ad009b9a48f139bbd4e11907991dd2f8b84612
3
+ size 99144436