byoussef commited on
Commit
85da0e0
·
verified ·
1 Parent(s): bd8c590

Use the CompiledModel API in the Python example

Browse files
Files changed (1) hide show
  1. README.md +18 -10
README.md CHANGED
@@ -68,19 +68,25 @@ This is an unofficial conversion. I am not affiliated with Apple and Apple does
68
 
69
  ```python
70
  import numpy as np
71
- from ai_edge_litert.interpreter import Interpreter
 
 
72
  from huggingface_hub import hf_hub_download
73
  from PIL import Image, ImageOps
74
 
75
  REPO = "byoussef/DepthPro2-litert"
76
 
 
 
 
77
 
78
- def run(name, frame):
79
- model = Interpreter(model_path=hf_hub_download(REPO, name))
80
- model.allocate_tensors()
81
- model.set_tensor(model.get_input_details()[0]["index"], frame)
82
- model.invoke()
83
- return model.get_tensor(model.get_output_details()[0]["index"])[0]
 
84
 
85
 
86
  def planes(image): # RGB in [0, 1], shaped (1, 3, H, W)
@@ -91,17 +97,19 @@ def planes(image): # RGB in [0, 1], shaped (1, 3, H, W)
91
  # the original code resizes with, and a sharper filter moves the focal length below by 4%.
92
  photo = Image.open("photo.jpg").convert("RGB")
93
  frame = ImageOps.fit(photo, (854, 480), Image.Resampling.BILINEAR)
94
- inverse_depth = run("depthpro2-s_480x854.tflite", planes(frame)) # (480, 854)
95
 
96
  # The focal length, for a photo that comes without one.
97
  crop = frame.resize((864, 480), Image.Resampling.BILINEAR).crop((16, 0, 848, 480))
98
- horizontal, vertical = run("depthpro2-fov_480x832.tflite", planes(crop)) # degrees
99
  focal = 0.5 * 480 / np.tan(np.radians(vertical) / 2) # pixels of the 854x480 frame
100
 
101
  depth = focal / (854 * inverse_depth) # meters; infinite where the inverse depth is 0, as in the sky
102
  ```
103
 
104
- This runs on the CPU. `pip install ai-edge-litert huggingface_hub numpy pillow` covers it.
 
 
105
 
106
  ## Speed and accuracy
107
 
 
68
 
69
  ```python
70
  import numpy as np
71
+ from ai_edge_litert.compiled_model import CompiledModel
72
+ from ai_edge_litert.hardware_accelerator import HardwareAccelerator
73
+ from ai_edge_litert.options import Options
74
  from huggingface_hub import hf_hub_download
75
  from PIL import Image, ImageOps
76
 
77
  REPO = "byoussef/DepthPro2-litert"
78
 
79
+ options = Options.create()
80
+ options.hardware_accelerators = HardwareAccelerator.GPU
81
+ options.gpu_options.enforce_f32 = True
82
 
83
+
84
+ def run(name, frame, size):
85
+ model = CompiledModel.from_file(hf_hub_download(REPO, name), options=options)
86
+ inputs, outputs = model.create_input_buffers(0), model.create_output_buffers(0)
87
+ inputs[0].write(frame)
88
+ model.run_by_index(0, inputs, outputs)
89
+ return outputs[0].read(size, np.float32)
90
 
91
 
92
  def planes(image): # RGB in [0, 1], shaped (1, 3, H, W)
 
97
  # the original code resizes with, and a sharper filter moves the focal length below by 4%.
98
  photo = Image.open("photo.jpg").convert("RGB")
99
  frame = ImageOps.fit(photo, (854, 480), Image.Resampling.BILINEAR)
100
+ inverse_depth = run("depthpro2-s_480x854.tflite", planes(frame), 480 * 854).reshape(480, 854)
101
 
102
  # The focal length, for a photo that comes without one.
103
  crop = frame.resize((864, 480), Image.Resampling.BILINEAR).crop((16, 0, 848, 480))
104
+ horizontal, vertical = run("depthpro2-fov_480x832.tflite", planes(crop), 2) # degrees
105
  focal = 0.5 * 480 / np.tan(np.radians(vertical) / 2) # pixels of the 854x480 frame
106
 
107
  depth = focal / (854 * inverse_depth) # meters; infinite where the inverse depth is 0, as in the sky
108
  ```
109
 
110
+ - **Install.** `pip install ai-edge-litert huggingface_hub numpy pillow`.
111
+ - **GPU.** This is LiteRT's `CompiledModel` on the GPU: S takes about 50 ms a frame on a desktop NVIDIA GPU. `enforce_f32` keeps it in float32; without it LiteRT picks float16, which lost the Vulkan device on that GPU.
112
+ - **CPU.** Replace the two GPU lines with `options.cpu_options.num_threads = 8`: about 300 ms a frame for S. Left at one thread it takes 2 s.
113
 
114
  ## Speed and accuracy
115