mlboydaisuke commited on
Commit
365c0f7
·
verified ·
1 Parent(s): a2f1b98

Add minimal usage snippets (Kotlin + Python)

Browse files
Files changed (1) hide show
  1. README.md +29 -7
README.md CHANGED
@@ -41,18 +41,40 @@ rewrites** (pure CNN).
41
  - **Output**: `[1, 1, 320, 320]` saliency mask in `[0, 1]` (sigmoid). Upscale to the input
42
  size and use as the foreground alpha.
43
 
44
- ## Usage (Android, LiteRT CompiledModel)
 
 
45
 
46
  ```kotlin
47
- val model = CompiledModel.create(
48
- context.assets, "u2net_fp16.tflite",
49
- CompiledModel.Options(Accelerator.GPU), null
50
- )
51
  val inputs = model.createInputBuffers()
52
  val outputs = model.createOutputBuffers()
53
- inputs[0].writeFloat(nchwFloatArray) // [1,3,320,320]
54
  model.run(inputs, outputs)
55
- val mask = outputs[0].readFloat() // [1,1,320,320] in [0,1]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  ```
57
 
58
  A complete Android sample (live camera + gallery background removal) is available in
 
41
  - **Output**: `[1, 1, 320, 320]` saliency mask in `[0, 1]` (sigmoid). Upscale to the input
42
  size and use as the foreground alpha.
43
 
44
+ ## Minimal usage
45
+
46
+ **Android (Kotlin, CompiledModel GPU)**
47
 
48
  ```kotlin
49
+ val model = CompiledModel.create(context.assets, "u2net_fp16.tflite",
50
+ CompiledModel.Options(Accelerator.GPU), null)
 
 
51
  val inputs = model.createInputBuffers()
52
  val outputs = model.createOutputBuffers()
53
+ inputs[0].writeFloat(chw) // [1,3,320,320] /max then ImageNet-norm, NCHW
54
  model.run(inputs, outputs)
55
+ val mask = outputs[0].readFloat() // [1,1,320,320] saliency in [0,1]
56
+ ```
57
+
58
+ **Python (desktop verification)**
59
+
60
+ ```python
61
+ MEAN = np.array([0.485, 0.456, 0.406], np.float32)
62
+ STD = np.array([0.229, 0.224, 0.225], np.float32)
63
+ import numpy as np
64
+ from PIL import Image
65
+ from ai_edge_litert.interpreter import Interpreter
66
+
67
+ orig = Image.open("photo.jpg").convert("RGB")
68
+ a = np.asarray(orig.resize((320, 320)), np.float32)
69
+ a = a / a.max() # per-image max, then ImageNet
70
+ x = ((a - MEAN) / STD).transpose(2, 0, 1)[None] # [1,3,320,320]
71
+
72
+ it = Interpreter(model_path="u2net_fp16.tflite"); it.allocate_tensors()
73
+ it.set_tensor(it.get_input_details()[0]["index"], x); it.invoke()
74
+ m = it.get_tensor(it.get_output_details()[0]["index"])[0, 0] # [320,320], [0,1]
75
+ alpha = Image.fromarray((m * 255).astype(np.uint8)).resize(orig.size)
76
+ cutout = orig.copy(); cutout.putalpha(alpha) # foreground on transparency
77
+ cutout.save("cutout.png")
78
  ```
79
 
80
  A complete Android sample (live camera + gallery background removal) is available in