litert-community/efficientnet_b0
0254
1---2library_name: litert3pipeline_tag: image-classification4tags:5 - vision6 - image-classification7 - google8 - computer-vision9datasets:10 - imagenet-1k11base_model:12 - google/efficientnet-b013base_model_relation: quantized14model-index:15 - name: litert-community/efficientnet_b016 results:17 - task:18 type: image-classification19 name: Image Classification20 dataset:21 name: ImageNet-1k22 type: imagenet-1k23 config: default24 split: validation25 metrics:26 - name: Top 1 Accuracy (Full Precision)27 type: accuracy28 value: 0.776529 - name: Top 5 Accuracy (Full Precision)30 type: accuracy31 value: 0.935332---33 34# EfficientNet B035 36EfficientNet B0 model pre-trained on ImageNet-1k. Originally introduced by Tan and Le in the influential paper, [**EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks**](https://arxiv.org/abs/1905.11946) this model utilizes compound scaling to systematically balance network depth, width, and resolution, enabling superior accuracy with significantly higher efficiency than traditional architectures.37 38`efficientnet_b0_int8_channelwise.tflite`: Mixed INT8/FP32 with channelwise INT8 weights. The prefix through stage 5 remains FP32.39 40## Compatibility41 42| File | CPU | GPU | NPU |43|---|---|---|---|44| `efficientnet_b0.tflite` | Supported | Supported | N/A |45| `efficientnet_b0_int8_channelwise.tflite` | Supported | Not supported | Qualcomm / MediaTek |46 47## Intended uses & limitations48 49The model files were converted from pretrained weights from PyTorch Vision. The models may have their own licenses or terms and conditions derived from PyTorch Vision and the dataset used for training. It is your responsibility to determine whether you have permission to use the models for your use case.50 51 52## Model description53The model was converted from a checkpoint from PyTorch Vision. 54 55The original model has: 56acc@1 (on ImageNet-1K): 77.692% 57acc@5 (on ImageNet-1K): 93.532% 58num_params: 5,288,54859 60### Quantized variant61 62`efficientnet_b0_weight_only_wi8_afp32.tflite` is a weight-only int863quantization of the same weights (about 3.5x smaller than float32).64Weight-only quantization is used instead of dynamic-range quantization65because EfficientNet's SE and SiLU layers are sensitive to activation66quantization; in a spot check against the float model the weight-only67file keeps the top-1 predictions on real photos with a minimum logit68correlation of 0.996.69 70## How to Use71 72**1. Install Dependencies** Ensure your Python environment is set up with the required libraries. Run the following command in your terminal: 73 74```bash 75pip install numpy Pillow huggingface_hub ai-edge-litert76```77 78**2. Prepare Your Image** The script expects an image file to analyze. Make sure you have an image (e.g., cat.jpg or car.png) saved in the same working directory as your script.79 80**3. Save the Script** Create a new file named `classify.py`, paste the script below into it, and save the file:81 82```python83#!/usr/bin/env python384import argparse, json85import numpy as np86from PIL import Image87from huggingface_hub import hf_hub_download88from ai_edge_litert.compiled_model import CompiledModel89 90def preprocess(img: Image.Image) -> np.ndarray:91 img = img.convert("RGB")92 w, h = img.size93 s = 25694 if w < h:95 img = img.resize((s, int(h * s / w)), Image.BICUBIC)96 else:97 img = img.resize((int(w * s / h), s), Image.BICUBIC)98 left = int(round((img.size[0] - 224) / 2.0))99 top = int(round((img.size[1] - 224) / 2.0))100 img = img.crop((left, top, left + 224, top + 224))101 102 x = np.asarray(img, dtype=np.float32) / 255.0103 x = (x - np.array([0.485, 0.456, 0.406], dtype=np.float32)) / np.array(104 [0.229, 0.224, 0.225], dtype=np.float32105 )106 return np.ascontiguousarray(x.transpose(2, 0, 1)[None])107 108def main():109 ap = argparse.ArgumentParser()110 ap.add_argument("--image", required=True)111 args = ap.parse_args()112 113 model_path = hf_hub_download("litert-community/efficientnet_b0", "efficientnet_b0.tflite")114 labels_path = hf_hub_download(115 "huggingface/label-files", "imagenet-1k-id2label.json", repo_type="dataset"116 )117 with open(labels_path, "r", encoding="utf-8") as f:118 id2label = {int(k): v for k, v in json.load(f).items()}119 120 img = Image.open(args.image)121 x = preprocess(img)122 123 model = CompiledModel.from_file(model_path)124 inp = model.create_input_buffers(0)125 out = model.create_output_buffers(0)126 127 inp[0].write(x)128 model.run_by_index(0, inp, out)129 130 req = model.get_output_buffer_requirements(0, 0)131 y = out[0].read(req["buffer_size"] // np.dtype(np.float32).itemsize, np.float32)132 133 pred = int(np.argmax(y))134 label = id2label.get(pred, f"class_{pred}")135 136 137 print(f"Top-1 class index: {pred}")138 print(f"Top-1 label: {label}")139if __name__ == "__main__":140 main()141```142 143**4. Execute the Python Script** Run the below command:144 145```bash 146python classify.py --image cat.jpg147```148 149 150### BibTeX entry and citation info151 152```bibtex153@article{Tan2019EfficientNetRM,154 title={EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks},155 author={Mingxing Tan and Quoc V. Le},156 journal={ArXiv},157 year={2019},158 volume={abs/1905.11946}159}160```