mlboydaisuke commited on
Commit
526c603
·
verified ·
1 Parent(s): 04e63ea

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +105 -0
README.md ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ library_name: litert
4
+ pipeline_tag: depth-estimation
5
+ tags:
6
+ - litert
7
+ - tflite
8
+ - depth-estimation
9
+ - monocular-depth
10
+ - on-device
11
+ - gpu
12
+ - depth-anything
13
+ base_model: depth-anything/DA3-SMALL
14
+ ---
15
+
16
+ # Depth Anything 3 (Small) — LiteRT GPU, monocular depth
17
+
18
+ On-device **LiteRT / TFLite** conversion of [**Depth Anything 3 — Small**](https://huggingface.co/depth-anything/DA3-SMALL)
19
+ (ByteDance-Seed, Apache-2.0) for **monocular depth**, running fully on the mobile **GPU** via the LiteRT
20
+ `CompiledModel` API (ML Drift delegate). No CPU fallback ops — the whole graph is GPU-compatible.
21
+
22
+ | | |
23
+ |---|---|
24
+ | Task | Monocular depth (single RGB → depth) |
25
+ | Backbone | DINOv2 ViT-S + RoPE, DPT/DualDPT depth head |
26
+ | Input | `[1, 3, 896, 504]` NCHW float32, ImageNet-normalized, **native portrait aspect** |
27
+ | Output | `[1, 1, 896, 504]` depth |
28
+ | Precision / size | FP16, **55 MB** |
29
+ | Device | Pixel 8a, LiteRT GPU (`Accelerator.GPU`), **~0.9 s / image** (FP16, CompiledModel.Run) |
30
+ | Fidelity | **corr 0.99948** vs official PyTorch; on-device **GPU-vs-CPU cos 0.99993** (re-verified, see below) |
31
+
32
+ ## Why a fixed 896×504 (native aspect, not square)
33
+
34
+ DA3 processes images at their **native aspect ratio** (`upper_bound_resize`, longer side → 896, multiple of 14).
35
+ Forcing a square `896×896` and letterbox-padding drops the match to corr **0.977** (the black padding leaks into
36
+ the content through global attention). Converting at the native rectangle restores **corr 0.9994** and is also
37
+ faster (fewer tokens). This checkpoint is built for **portrait ~9:16**. For another aspect, re-convert at that
38
+ shape (or your camera's fixed aspect) with the script below.
39
+
40
+ ## Preprocessing (must match)
41
+
42
+ ```
43
+ resize to 504×896 (W×H) → x/255 → (x - mean) / std
44
+ mean = [0.485, 0.456, 0.406], std = [0.229, 0.224, 0.225] # ImageNet, RGB, NCHW
45
+ ```
46
+
47
+ ## GPU-clean conversion (what was patched)
48
+
49
+ Converted with `litert-torch`. DA3 is not GPU-clean out of the box; the following exact, GPU-clean rewrites
50
+ were applied (all numerically faithful unless noted):
51
+
52
+ 1. checkpoint `model.` key-prefix strip (load fix)
53
+ 2. RoPE `max_position = int(positions.max())+1` → constant (torch.export data-dependent)
54
+ 3. fused-QKV attention → 3 separate Linears + 4D attention (avoids 5D RESHAPE; exact, 1e-6)
55
+ 4. **LayerScale** `gamma` folded into `attn.proj` / `mlp.fc2` (the LayerScale MUL otherwise mis-lays-out the
56
+ token dim on the GPU delegate: `fully_connected {1,1,N,C} vs {N,1,1,C}`)
57
+ 5. `pos_embed` bicubic interpolation **baked** to a constant (the interpolate of a constant emits `GATHER_ND`
58
+ on desktop and `RESIZE_BILINEAR` with 0 runtime inputs on device)
59
+ 6. **ConvTranspose2d(k=s,stride=s)** → zero-stuff (nearest-upsample × top-left mask) + `Conv2d` (flipped
60
+ weight) — exact equivalent (~1e-7), because the Pixel-8a GPU rejects `TRANSPOSE_CONV` and the conv+
61
+ depth-to-space alternative needs >4D
62
+ 7. DPT-head `custom_interpolate` `align_corners=True → False` (GPU bans `align_corners=True` resize) — **the
63
+ only non-exact rewrite**; source of the residual ~0.05 % vs the official model
64
+ 8. head UV pos-embed-again disabled (its `make_sincos` broadcast emits `BROADCAST_TO`; ratio-0.1 refinement)
65
+ 9. camera-token insertion `x[:, :, 0] = cam_token` → `torch.cat` (in-place index-assign → `SELECT_V2`)
66
+
67
+ Net result: `GATHER_ND = 0`, no `>4D` tensors, no `TRANSPOSE_CONV` / `BROADCAST_TO` / banned ops.
68
+
69
+ ## Fidelity note (honest)
70
+
71
+ corr **0.99948** vs the official FP32 PyTorch pipeline. FP16 is **not** a factor (FP32≡FP16, corr 1.0). The
72
+ residual ~0.05 % is the `align_corners=True→False` change in (7), which the mobile GPU forces — an irreducible
73
+ hardware constraint, not a conversion error. Structure and edge sharpness are visually identical.
74
+
75
+ ## On-device GPU verification (re-confirmed)
76
+
77
+ Re-verified on a Pixel 8a with the official LiteRT C++ runtime + ML Drift accelerator: the model compiles
78
+ to **`Replacing 1460 out of 1460 node(s) with delegate (LITERT_CL)`** (full residency, single partition,
79
+ no XNNPACK CPU fallback), and the **on-device GPU output matches the CPU/XNNPACK reference at cos 0.99993 /
80
+ Pearson 0.99975** for the same input — i.e. the GPU result is numerically faithful, not merely "resident"
81
+ (GPU full residency does not by itself guarantee a correct result).
82
+
83
+ ## Usage (Android / LiteRT CompiledModel)
84
+
85
+ ```kotlin
86
+ val model = CompiledModel.create(context.assets, "da3_small_gpu_fp16.tflite",
87
+ CompiledModel.Options(Accelerator.GPU), null)
88
+ // input: [1,3,896,504] NCHW, ImageNet-normalized; output: [1,1,896,504] depth
89
+ ```
90
+
91
+ ## Training data & PII
92
+
93
+ Depth Anything 3 was trained by ByteDance-Seed on a large-scale collection of monocular-depth data — a mix
94
+ of **synthetic depth datasets and real images with pseudo-labelled depth** (the Depth Anything line scales
95
+ to tens of millions of images). No new training was performed for this conversion — it is a weights-faithful
96
+ (corr ≈ 1.0) format change of the public `depth-anything/DA3-SMALL` checkpoint. Because the source data
97
+ includes real-world indoor/outdoor scenes, it may incidentally contain people, faces, vehicles, signage and
98
+ other PII; no PII was deliberately collected and this conversion adds none. Apply your own content/PII
99
+ filtering as appropriate. See the original [Depth Anything 3](https://github.com/ByteDance-Seed/depth-anything-3)
100
+ release and [paper](https://arxiv.org/abs/2511.10647) for full dataset details.
101
+
102
+ ## License
103
+
104
+ Apache-2.0, inherited from the upstream [Depth Anything 3](https://github.com/ByteDance-Seed/depth-anything-3).
105
+ This is a format conversion; all credit to the original authors (ByteDance-Seed).