Views
No views yet
| File | Description |
|---|---|
mobilenet_v3_large.tflite | Full precision LiteRT/TFLite model. |
mobilenet_v3_large_dynamic_wi8_afp32.tflite | Dynamic weight-only INT8 model with FP32 activations. |
mobilenet_v3_large_Google_Tensor_G5_apply_plugin.tflite | AOT-compiled artifact for the Google Tensor G5 target. |
mobilenet_v3_large_int8_channelwise.tflite | Static INT8 model with channelwise INT8 weights and asymmetric INT8 activations. |
mobilenet_v3_large_int8_channelwise.tflite was produced with the STATIC_WI8_AI8 quantization recipe. Weights are signed INT8 and use symmetric channelwise quantization for weight tensors. Activations are signed INT8 with asymmetric quantization parameters.pip install numpy Pillow huggingface_hub ai-edge-litertclassify.py, paste the script below into it, and save the file:1#!/usr/bin/env python3
2import argparse, json
3import numpy as np
4from PIL import Image
5from huggingface_hub import hf_hub_download
6from ai_edge_litert.compiled_model import CompiledModel
7
8def preprocess(img: Image.Image) -> np.ndarray:
9 img = img.convert("RGB")
10 w, h = img.size
11 s = 232
12 if w < h:
13 img = img.resize((s, int(round(h * s / w))), Image.BILINEAR)
14 else:
15 img = img.resize((int(round(w * s / h)), s), Image.BILINEAR)
16 left = (img.size[0] - 224) // 2
17 top = (img.size[1] - 224) // 2
18 img = img.crop((left, top, left + 224, top + 224))
19
20 x = np.asarray(img, dtype=np.float32) / 255.0
21 x = (x - np.array([0.485, 0.456, 0.406], dtype=np.float32)) / np.array(
22 [0.229, 0.224, 0.225], dtype=np.float32
23 )
24 return x
25
26def main():
27 ap = argparse.ArgumentParser()
28 ap.add_argument("--image", required=True)
29 args = ap.parse_args()
30
31 model_path = hf_hub_download("litert-community/MobileNet-v3-large", "mobilenet_v3_large.tflite")
32 labels_path = hf_hub_download(
33 "huggingface/label-files", "imagenet-1k-id2label.json", repo_type="dataset"
34 )
35 with open(labels_path, "r", encoding="utf-8") as f:
36 id2label = {int(k): v for k, v in json.load(f).items()}
37
38 img = Image.open(args.image)
39 x = preprocess(img)
40
41 model = CompiledModel.from_file(model_path)
42 inp = model.create_input_buffers(0)
43 out = model.create_output_buffers(0)
44
45 inp[0].write(x)
46 model.run_by_index(0, inp, out)
47
48 req = model.get_output_buffer_requirements(0, 0)
49 y = out[0].read(req["buffer_size"] // np.dtype(np.float32).itemsize, np.float32)
50
51 pred = int(np.argmax(y))
52 label = id2label.get(pred, f"class_{pred}")
53
54 print(f"Top-1 class index: {pred}")
55 print(f"Top-1 label: {label}")
56if __name__ == "__main__":
57 main()python classify.py --image cat.jpg1@article{DBLP:journals/corr/abs-1905-02244,
2 author = {Andrew Howard and
3 Mark Sandler and
4 Grace Chu and
5 Liang{-}Chieh Chen and
6 Bo Chen and
7 Mingxing Tan and
8 Weijun Wang and
9 Yukun Zhu and
10 Ruoming Pang and
11 Vijay Vasudevan and
12 Quoc V. Le and
13 Hartwig Adam},
14 title = {Searching for MobileNetV3},
15 journal = {CoRR},
16 volume = {abs/1905.02244},
17 year = {2019},
18 url = {http://arxiv.org/abs/1905.02244},
19 eprinttype = {arXiv},
20 eprint = {1905.02244},
21 timestamp = {Thu, 27 May 2021 16:20:51 +0200},
22 biburl = {https://dblp.org/rec/journals/corr/abs-1905-02244.bib},
23 bibsource = {dblp computer science bibliography, https://dblp.org}
24}