Update README: Add model card metadata, ImageNet-1k metrics, and LiteRT usage example
#1
by akashvverma1995 - opened
README.md
CHANGED
|
@@ -1,20 +1,142 @@
|
|
| 1 |
---
|
| 2 |
library_name: litert
|
|
|
|
| 3 |
tags:
|
| 4 |
- vision
|
| 5 |
- image-classification
|
|
|
|
|
|
|
| 6 |
datasets:
|
| 7 |
- imagenet-1k
|
| 8 |
base_model:
|
| 9 |
- google/efficientnet-b6
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
---
|
|
|
|
| 11 |
# EfficientNet B6
|
| 12 |
|
| 13 |
-
EfficientNet B6 model pre-trained on ImageNet-1k.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
### BibTeX entry and citation info
|
| 20 |
|
|
|
|
| 1 |
---
|
| 2 |
library_name: litert
|
| 3 |
+
pipeline_tag: image-classification
|
| 4 |
tags:
|
| 5 |
- vision
|
| 6 |
- image-classification
|
| 7 |
+
- google
|
| 8 |
+
- computer-vision
|
| 9 |
datasets:
|
| 10 |
- imagenet-1k
|
| 11 |
base_model:
|
| 12 |
- google/efficientnet-b6
|
| 13 |
+
model-index:
|
| 14 |
+
- name: litert-community/efficientnet_b6
|
| 15 |
+
results:
|
| 16 |
+
- task:
|
| 17 |
+
type: image-classification
|
| 18 |
+
name: Image Classification
|
| 19 |
+
dataset:
|
| 20 |
+
name: ImageNet-1k
|
| 21 |
+
type: imagenet-1k
|
| 22 |
+
config: default
|
| 23 |
+
split: validation
|
| 24 |
+
metrics:
|
| 25 |
+
- name: Top 1 Accuracy (Full Precision)
|
| 26 |
+
type: accuracy
|
| 27 |
+
value: 0.8400
|
| 28 |
+
- name: Top 5 Accuracy (Full Precision)
|
| 29 |
+
type: accuracy
|
| 30 |
+
value: 0.9691
|
| 31 |
+
- name: Top 1 Accuracy (Dynamic Quantized wi8 afp32)
|
| 32 |
+
type: accuracy
|
| 33 |
+
value: 0.8383
|
| 34 |
+
- name: Top 5 Accuracy (Dynamic Quantized wi8 afp32)
|
| 35 |
+
type: accuracy
|
| 36 |
+
value: 0.9677
|
| 37 |
---
|
| 38 |
+
|
| 39 |
# EfficientNet B6
|
| 40 |
|
| 41 |
+
EfficientNet B6 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.
|
| 42 |
+
|
| 43 |
+
## Model description
|
| 44 |
+
|
| 45 |
+
The model was converted from a checkpoint from PyTorch Vision.
|
| 46 |
+
|
| 47 |
+
The original model has:
|
| 48 |
+
acc@1 (on ImageNet-1K): 84.008%
|
| 49 |
+
acc@5 (on ImageNet-1K): 96.916%
|
| 50 |
+
num_params: 43,040,704
|
| 51 |
+
|
| 52 |
+
## How to Use
|
| 53 |
+
|
| 54 |
+
**1. Install Dependencies** Ensure your Python environment is set up with the required libraries. Run the following command in your terminal:
|
| 55 |
+
|
| 56 |
+
```bash
|
| 57 |
+
pip install numpy Pillow huggingface_hub ai-edge-litert
|
| 58 |
+
```
|
| 59 |
+
|
| 60 |
+
**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.
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
**3. Save the Script** Create a new file named `classify.py`, paste the script below into it, and save the file:
|
| 64 |
+
|
| 65 |
+
```python
|
| 66 |
+
#!/usr/bin/env python3
|
| 67 |
+
import argparse, json
|
| 68 |
+
import numpy as np
|
| 69 |
+
from PIL import Image
|
| 70 |
+
from huggingface_hub import hf_hub_download
|
| 71 |
+
from ai_edge_litert.compiled_model import CompiledModel
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def preprocess(img: Image.Image) -> np.ndarray:
|
| 75 |
+
img = img.convert("RGB")
|
| 76 |
+
w, h = img.size
|
| 77 |
+
s = 528
|
| 78 |
+
if w < h:
|
| 79 |
+
img = img.resize((s, int(round(h * s / w))), Image.BICUBIC)
|
| 80 |
+
else:
|
| 81 |
+
img = img.resize((int(round(w * s / h)), s), Image.BICUBIC)
|
| 82 |
+
left = (img.size[0] - 528) // 2
|
| 83 |
+
top = (img.size[1] - 528) // 2
|
| 84 |
+
img = img.crop((left, top, left + 528, top + 528))
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
x = np.asarray(img, dtype=np.float32) / 255.0
|
| 88 |
+
x = (x - np.array([0.485, 0.456, 0.406], dtype=np.float32)) / np.array(
|
| 89 |
+
[0.229, 0.224, 0.225], dtype=np.float32
|
| 90 |
+
)
|
| 91 |
+
return np.transpose(x, (2, 0, 1))
|
| 92 |
+
|
| 93 |
|
| 94 |
+
def main():
|
| 95 |
+
ap = argparse.ArgumentParser()
|
| 96 |
+
ap.add_argument("--image", required=True)
|
| 97 |
+
args = ap.parse_args()
|
| 98 |
|
| 99 |
+
|
| 100 |
+
model_path = hf_hub_download("litert-community/efficientnet_b6", "efficientnet_b6.tflite")
|
| 101 |
+
labels_path = hf_hub_download(
|
| 102 |
+
"huggingface/label-files", "imagenet-1k-id2label.json", repo_type="dataset"
|
| 103 |
+
)
|
| 104 |
+
with open(labels_path, "r", encoding="utf-8") as f:
|
| 105 |
+
id2label = {int(k): v for k, v in json.load(f).items()}
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
img = Image.open(args.image)
|
| 109 |
+
x = preprocess(img)
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
model = CompiledModel.from_file(model_path)
|
| 113 |
+
inp = model.create_input_buffers(0)
|
| 114 |
+
out = model.create_output_buffers(0)
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
inp[0].write(x)
|
| 118 |
+
model.run_by_index(0, inp, out)
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
req = model.get_output_buffer_requirements(0, 0)
|
| 122 |
+
y = out[0].read(req["buffer_size"] // np.dtype(np.float32).itemsize, np.float32)
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
pred = int(np.argmax(y))
|
| 126 |
+
label = id2label.get(pred, f"class_{pred}")
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
print(f"Top-1 class index: {pred}")
|
| 130 |
+
print(f"Top-1 label: {label}")
|
| 131 |
+
if __name__ == "__main__":
|
| 132 |
+
main()
|
| 133 |
+
```
|
| 134 |
+
|
| 135 |
+
**4. Execute the Python Script** Run the below command:
|
| 136 |
+
|
| 137 |
+
```bash
|
| 138 |
+
python classify.py --image cat.jpg
|
| 139 |
+
```
|
| 140 |
|
| 141 |
### BibTeX entry and citation info
|
| 142 |
|