mlboydaisuke commited on
Commit
cc9d6c1
·
verified ·
1 Parent(s): 5e1403b

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +72 -0
README.md ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ library_name: litert
4
+ pipeline_tag: image-segmentation
5
+ tags: [clipseg, text-prompted-segmentation, open-vocabulary, clip, litert, tflite, on-device, gpu]
6
+ base_model: CIDAS/clipseg-rd64-refined
7
+ ---
8
+
9
+ # CLIPSeg rd64 — LiteRT on-device text-prompted segmentation
10
+
11
+ [CLIPSeg](https://huggingface.co/CIDAS/clipseg-rd64-refined) (CVPR 2022, Apache-2.0) re-authored for
12
+ LiteRT: type what you want to segment ("a cat", "the sky") and get a mask — no fixed class list.
13
+ Three graphs — CLIP **text** and **vision** encoders on the CompiledModel **GPU**, the tiny 3-layer
14
+ **decoder** on **CPU** (its 4-head/head_dim-16 attention fp16-miscomputes on the Mali delegate; the
15
+ 12-head/head_dim-64 vision encoder survives at 0.998).
16
+
17
+ Verified on a Pixel 8a: text **761/761** GPU (~8.7 ms) + vision **613/613** GPU (~8.2 ms) + decoder
18
+ CPU (exact); end-to-end device-vs-PyTorch logits corr **0.99998**, mask IoU **0.9986**.
19
+
20
+ ## Files
21
+ | file | graph | delegate |
22
+ |---|---|---|
23
+ | `clipseg_text_fp16.tflite` | token-emb [1,77,512] → hidden [1,77,512] | GPU |
24
+ | `clipseg_vision_fp16.tflite` | image [1,3,352,352] → t3,t6,t9 [1,485,768] | GPU |
25
+ | `clipseg_decoder.tflite` (fp32) | t3,t6,t9,cond[512] → logits [1,352,352] | CPU |
26
+ | `token_embedding_f16.bin`, `text_projection_f16.bin`, `vocab.json`, `merges.txt` | host assets | — |
27
+
28
+ ## Minimal usage (Python)
29
+
30
+ ```python
31
+ import numpy as np, torch
32
+ from PIL import Image
33
+ from transformers import CLIPSegProcessor
34
+ from ai_edge_litert.interpreter import Interpreter
35
+
36
+ proc = CLIPSegProcessor.from_pretrained("CIDAS/clipseg-rd64-refined")
37
+ img = proc(images=Image.open("photo.jpg"), return_tensors="pt")["pixel_values"].numpy() # [1,3,352,352]
38
+
39
+ vis = Interpreter("clipseg_vision_fp16.tflite"); vis.allocate_tensors()
40
+ vis.set_tensor(vis.get_input_details()[0]["index"], img); vis.invoke()
41
+ t = [vis.get_tensor(o["index"]) for o in sorted(vis.get_output_details(), key=lambda o: o["index"])] # t3,t6,t9
42
+
43
+ # cond[512] from the text encoder (token-emb lookup -> text graph -> EOT row @ text_projection)
44
+ dec = Interpreter("clipseg_decoder.tflite"); dec.allocate_tensors() # CPU (exact)
45
+ ins = dec.get_input_details()
46
+ for d, arr in zip(ins, [t[0], t[1], t[2], cond]): # cond: [1,512] float32
47
+ dec.set_tensor(d["index"], arr.astype(np.float32))
48
+ dec.invoke()
49
+ mask = 1 / (1 + np.exp(-dec.get_tensor(dec.get_output_details()[0]["index"])[0])) # sigmoid, [352,352]
50
+ ```
51
+
52
+ ### Kotlin (Android)
53
+
54
+ ```kotlin
55
+ // vision + text: Accelerator.GPU; decoder: Accelerator.CPU
56
+ val vis = CompiledModel.create(File(dir,"clipseg_vision_fp16.tflite").path, CompiledModel.Options(Accelerator.GPU), null)
57
+ val dec = CompiledModel.create(File(dir,"clipseg_decoder.tflite").path, CompiledModel.Options(Accelerator.CPU), null)
58
+ // vision: image[1,3,352,352] -> t3,t6,t9 decoder: (t3,t6,t9,cond[512]) -> logits[1,352,352]
59
+ // text graph + host BPE/emb-lookup/text_projection produce cond; see ClipSeg.kt in the LiteRT sample.
60
+ val logits = decOut[0].readFloat() // sigmoid -> mask
61
+ ```
62
+
63
+ ## Conversion
64
+ Re-authored with litert-torch: qkv-3D-BMM attention, quick-GELU, baked interpolated pos-embed
65
+ (14²→22² @352), host-side token-embedding lookup, `safe_ln_up` (up-scaled LayerNorm keeping the eps
66
+ fp16-normal), `convT4x4` (exact non-overlapping ConvTranspose as 1×1-conv + 4-D interleave). The
67
+ decoder ships on CPU because its small-head-dim attention fp16-miscomputes on the Mali GPU delegate
68
+ (re-authoring is exact — desktop fp16 corr 0.999996).
69
+
70
+ ## Upstream
71
+ [CIDAS/clipseg-rd64-refined](https://huggingface.co/CIDAS/clipseg-rd64-refined) (Apache-2.0). Please
72
+ cite Lüddecke & Ecker, *Image Segmentation Using Text and Image Prompts* (CVPR 2022).