Views
No views yet
efficientnet_b7_weight_only_wi8_afp32.tflite is a weight-only int8
quantization of the same weights (about 3.8x 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
8def preprocess(img: Image.Image) -> np.ndarray:
9 img = img.convert("RGB")
10 w, h = img.size
11 s = 600
12 if w < h:
13 img = img.resize((s, int(round(h * s / w))), Image.BICUBIC)
14 else:
15 img = img.resize((int(round(w * s / h)), s), Image.BICUBIC)
16 left = (img.size[0] - 600) // 2
17 top = (img.size[1] - 600) // 2
18 img = img.crop((left, top, left + 600, top + 600))
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 np.transpose(x, (2, 0, 1))
25
26def main():
27 ap = argparse.ArgumentParser()
28 ap.add_argument("--image", required=True)
29 args = ap.parse_args()
30
31
32 model_path = hf_hub_download("litert-community/efficientnet_b7", "efficientnet_b7.tflite")
33 labels_path = hf_hub_download(
34 "huggingface/label-files", "imagenet-1k-id2label.json", repo_type="dataset"
35 )
36 with open(labels_path, "r", encoding="utf-8") as f:
37 id2label = {int(k): v for k, v in json.load(f).items()}
38
39
40 img = Image.open(args.image)
41 x = preprocess(img)
42
43
44 model = CompiledModel.from_file(model_path)
45 inp = model.create_input_buffers(0)
46 out = model.create_output_buffers(0)
47
48
49 inp[0].write(x)
50 model.run_by_index(0, inp, out)
51
52
53 req = model.get_output_buffer_requirements(0, 0)
54 y = out[0].read(req["buffer_size"] // np.dtype(np.float32).itemsize, np.float32)
55
56
57 pred = int(np.argmax(y))
58 label = id2label.get(pred, f"class_{pred}")
59
60
61 print(f"Top-1 class index: {pred}")
62 print(f"Top-1 label: {label}")
63if __name__ == "__main__":
64 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}