Views
No views yet
efficientnet_b5_weight_only_wi8_afp32.tflite is a weight-only int8
quantization of the same weights (about 3.7x smaller than float32).
Weight-only quantization is used instead of dynamic-range quantization
because EfficientNet's SE and SiLU layers are sensitive to activation
quantization; in a spot check against the float model the weight-only
file keeps the top-1 predictions on real photos with a minimum logit
correlation of 1.000.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
8
9def preprocess(img: Image.Image) -> np.ndarray:
10 img = img.convert("RGB")
11 w, h = img.size
12 s = 456
13 if w < h:
14 img = img.resize((s, int(round(h * s / w))), Image.BICUBIC)
15 else:
16 img = img.resize((int(round(w * s / h)), s), Image.BICUBIC)
17 left = (img.size[0] - 456) // 2
18 top = (img.size[1] - 456) // 2
19 img = img.crop((left, top, left + 456, top + 456))
20
21
22 x = np.asarray(img, dtype=np.float32) / 255.0
23 x = (x - np.array([0.485, 0.456, 0.406], dtype=np.float32)) / np.array(
24 [0.229, 0.224, 0.225], dtype=np.float32
25 )
26 return np.transpose(x, (2, 0, 1))
27
28
29def main():
30 ap = argparse.ArgumentParser()
31 ap.add_argument("--image", required=True)
32 args = ap.parse_args()
33
34
35 model_path = hf_hub_download("litert-community/efficientnet_b5", "efficientnet_b5.tflite")
36 labels_path = hf_hub_download(
37 "huggingface/label-files", "imagenet-1k-id2label.json", repo_type="dataset"
38 )
39 with open(labels_path, "r", encoding="utf-8") as f:
40 id2label = {int(k): v for k, v in json.load(f).items()}
41
42
43 img = Image.open(args.image)
44 x = preprocess(img)
45
46
47 model = CompiledModel.from_file(model_path)
48 inp = model.create_input_buffers(0)
49 out = model.create_output_buffers(0)
50
51
52 inp[0].write(x)
53 model.run_by_index(0, inp, out)
54
55
56 req = model.get_output_buffer_requirements(0, 0)
57 y = out[0].read(req["buffer_size"] // np.dtype(np.float32).itemsize, np.float32)
58
59
60 pred = int(np.argmax(y))
61 label = id2label.get(pred, f"class_{pred}")
62
63
64 print(f"Top-1 class index: {pred}")
65 print(f"Top-1 label: {label}")
66if __name__ == "__main__":
67 main()python classify.py --image cat.jpg1@article{Tan2019EfficientNetRM,
2 title={EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks},
3 author={Mingxing Tan and Quoc V. Le},
4 journal={ArXiv},
5 year={2019},
6 volume={abs/1905.11946}
7}