mlboydaisuke commited on
Commit
98a233a
·
verified ·
1 Parent(s): 9705b2b

Add minimal usage snippets (Kotlin + Python)

Browse files
Files changed (1) hide show
  1. README.md +33 -0
README.md CHANGED
@@ -28,6 +28,39 @@ udnie), each a **3.5 MB** fp16 graph.
28
  image[1,3,256,256] (RGB 0-255) →[GPU: TransformerNet]→ stylized[1,3,256,256] (RGB 0-255)
29
  ```
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  ## How it converts (litert-torch) — three numerically-exact re-authorings
32
 
33
  1. **`ReflectionPad2d` → zero-pad** (`GATHER_ND` → `PAD`; border-only difference).
 
28
  image[1,3,256,256] (RGB 0-255) →[GPU: TransformerNet]→ stylized[1,3,256,256] (RGB 0-255)
29
  ```
30
 
31
+ ## Minimal usage
32
+
33
+ **Android (Kotlin, CompiledModel GPU)**
34
+
35
+ ```kotlin
36
+ val model = CompiledModel.create(context.assets, "style_candy_fp16.tflite",
37
+ CompiledModel.Options(Accelerator.GPU), null)
38
+ val inputs = model.createInputBuffers()
39
+ val outputs = model.createOutputBuffers()
40
+ inputs[0].writeFloat(chw) // [1,3,256,256] RGB 0-255, NCHW
41
+ model.run(inputs, outputs)
42
+ val stylized = outputs[0].readFloat() // [1,3,256,256] RGB 0-255
43
+ ```
44
+
45
+ **Python (desktop verification)**
46
+
47
+ ```python
48
+ import numpy as np
49
+ from PIL import Image
50
+ from ai_edge_litert.interpreter import Interpreter
51
+
52
+ img = Image.open("photo.jpg").convert("RGB")
53
+ w, h = img.size; s = min(w, h)
54
+ img = img.crop(((w-s)//2, (h-s)//2, (w+s)//2, (h+s)//2)).resize((256, 256))
55
+ x = np.asarray(img, np.float32).transpose(2, 0, 1)[None] # 0-255, no normalization
56
+
57
+ # candy / mosaic / rain_princess / udnie
58
+ it = Interpreter(model_path="style_candy_fp16.tflite"); it.allocate_tensors()
59
+ it.set_tensor(it.get_input_details()[0]["index"], x); it.invoke()
60
+ y = it.get_tensor(it.get_output_details()[0]["index"])[0] # [3,256,256] RGB 0-255
61
+ Image.fromarray(y.transpose(1, 2, 0).clip(0, 255).astype(np.uint8)).save("stylized.png")
62
+ ```
63
+
64
  ## How it converts (litert-torch) — three numerically-exact re-authorings
65
 
66
  1. **`ReflectionPad2d` → zero-pad** (`GATHER_ND` → `PAD`; border-only difference).