mlboydaisuke commited on
Commit
8253ed4
·
verified ·
1 Parent(s): dfebf52

Add minimal usage snippets (Kotlin + Python)

Browse files
Files changed (1) hide show
  1. README.md +35 -0
README.md CHANGED
@@ -29,6 +29,41 @@ face[1,3,448,448] (ImageNet-normalized) →[GPU: ResNet50]→ yaw[1,90], pitch[1
29
 
30
  The 90 bins span [-180,180]° (4° each); gaze angle = softmax expectation `Σ p_i·i · 4 − 180` (softmax baked in).
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  ## How it converts (litert-torch)
33
 
34
  Pure CNN (ResNet50 + 2 FC heads). Two numerically-exact ResNet fixes:
 
29
 
30
  The 90 bins span [-180,180]° (4° each); gaze angle = softmax expectation `Σ p_i·i · 4 − 180` (softmax baked in).
31
 
32
+ ## Minimal usage
33
+
34
+ **Android (Kotlin, CompiledModel GPU)**
35
+
36
+ ```kotlin
37
+ val model = CompiledModel.create(context.assets, "gaze_fp16.tflite",
38
+ CompiledModel.Options(Accelerator.GPU), null)
39
+ val inputs = model.createInputBuffers()
40
+ val outputs = model.createOutputBuffers()
41
+ inputs[0].writeFloat(chw) // [1,3,448,448] ImageNet-normalized RGB, NCHW
42
+ model.run(inputs, outputs)
43
+ val yawProbs = outputs[0].readFloat() // [1,90] softmax over 4-deg bins
44
+ val pitchProbs = outputs[1].readFloat() // [1,90]; deg = sum(p_i * i) * 4 - 180
45
+ ```
46
+
47
+ **Python (desktop verification)**
48
+
49
+ ```python
50
+ MEAN = np.array([0.485, 0.456, 0.406], np.float32)
51
+ STD = np.array([0.229, 0.224, 0.225], np.float32)
52
+ import numpy as np
53
+ from PIL import Image
54
+ from ai_edge_litert.interpreter import Interpreter
55
+
56
+ img = Image.open("face.jpg").convert("RGB").resize((448, 448)) # centered face crop
57
+ x = ((np.asarray(img, np.float32) / 255 - MEAN) / STD).transpose(2, 0, 1)[None]
58
+
59
+ it = Interpreter(model_path="gaze_fp16.tflite"); it.allocate_tensors()
60
+ it.set_tensor(it.get_input_details()[0]["index"], x); it.invoke()
61
+ od = it.get_output_details() # output 0 = yaw, 1 = pitch (both [1,90])
62
+ deg = lambda p: float((p * np.arange(90)).sum() * 4 - 180)
63
+ yaw, pitch = (deg(it.get_tensor(o["index"])[0]) for o in od)
64
+ print(f"yaw {yaw:+.1f} deg, pitch {pitch:+.1f} deg")
65
+ ```
66
+
67
  ## How it converts (litert-torch)
68
 
69
  Pure CNN (ResNet50 + 2 FC heads). Two numerically-exact ResNet fixes: