mlboydaisuke commited on
Commit
3b5f0d3
·
verified ·
1 Parent(s): 5b8f645

Add minimal usage snippets (Kotlin + Python)

Browse files
Files changed (1) hide show
  1. README.md +47 -0
README.md CHANGED
@@ -40,6 +40,53 @@ For anchor `i` at grid `(gx,gy)` with `stride ∈ {8,16,32}`:
40
  `score = obj * max_class`; then per-class NMS. Divide boxes by the letterbox ratio to map back.
41
  Reference Kotlin + Python decode in the sample below.
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  ## Performance
44
 
45
  COCO val2017 AP **32.8** (FP32 reference). Real-time on Pixel 8a GPU.
 
40
  `score = obj * max_class`; then per-class NMS. Divide boxes by the letterbox ratio to map back.
41
  Reference Kotlin + Python decode in the sample below.
42
 
43
+ ## Minimal usage
44
+
45
+ **Android (Kotlin, CompiledModel GPU)**
46
+
47
+ ```kotlin
48
+ val model = CompiledModel.create(context.assets, "yolox_tiny.tflite",
49
+ CompiledModel.Options(Accelerator.GPU), null)
50
+ val inputs = model.createInputBuffers()
51
+ val outputs = model.createOutputBuffers()
52
+ inputs[0].writeFloat(nhwc) // [1,416,416,3] BGR 0-255, letterbox pad 114
53
+ model.run(inputs, outputs)
54
+ val raw = outputs[0].readFloat() // [1,3549,85] -> decode + NMS on host (see Python)
55
+ ```
56
+
57
+ **Python (desktop verification)**
58
+
59
+ ```python
60
+ import numpy as np
61
+ from PIL import Image
62
+ from ai_edge_litert.interpreter import Interpreter
63
+
64
+ SIZE = 416
65
+ img = Image.open("photo.jpg").convert("RGB")
66
+ r = min(SIZE / img.width, SIZE / img.height)
67
+ w, h = round(img.width * r), round(img.height * r)
68
+ canvas = np.full((SIZE, SIZE, 3), 114, np.float32) # letterbox, gray 114
69
+ canvas[:h, :w] = np.asarray(img.resize((w, h)), np.float32)
70
+ x = np.ascontiguousarray(canvas[..., ::-1])[None] # RGB -> BGR, 0-255, NHWC
71
+
72
+ it = Interpreter(model_path="yolox_tiny.tflite"); it.allocate_tensors()
73
+ it.set_tensor(it.get_input_details()[0]["index"], x); it.invoke()
74
+ out = it.get_tensor(it.get_output_details()[0]["index"])[0] # [3549,85]
75
+
76
+ grids, strides = [], [] # anchors = grid cells, s 8/16/32
77
+ for s in (8, 16, 32):
78
+ n = SIZE // s
79
+ gy, gx = np.mgrid[:n, :n]
80
+ grids.append(np.stack([gx, gy], -1).reshape(-1, 2)); strides.append(np.full((n * n, 1), s))
81
+ g = np.concatenate(grids).astype(np.float32); sv = np.concatenate(strides).astype(np.float32)
82
+ xy = (out[:, :2] + g) * sv; wh = np.exp(out[:, 2:4]) * sv # boxes in 416-space
83
+ score = out[:, 4:5] * out[:, 5:] # obj x class (already sigmoid)
84
+ cls, conf = score.argmax(1), score.max(1)
85
+ for i in np.where(conf > 0.35)[0]: # + per-class NMS in practice
86
+ x1, y1 = (xy[i] - wh[i] / 2) / r; x2, y2 = (xy[i] + wh[i] / 2) / r
87
+ print(f"coco class {cls[i]} {conf[i]:.2f} [{x1:.0f},{y1:.0f},{x2:.0f},{y2:.0f}]")
88
+ ```
89
+
90
  ## Performance
91
 
92
  COCO val2017 AP **32.8** (FP32 reference). Real-time on Pixel 8a GPU.