Update README: Add model card metadata, ImageNet-1k metrics, and LiteRT usage example

#1
Files changed (1) hide show
  1. README.md +105 -21
README.md CHANGED
@@ -1,32 +1,37 @@
1
  ---
2
  library_name: litert
 
3
  tags:
4
- - vision
5
- - image-classification
 
 
6
  datasets:
7
- - imagenet-1k
8
  model-index:
9
- - name: litert-community/MobileNet-v3-small
10
- results:
11
- - task:
12
- type: image-classification
13
- name: Image Classification
14
- dataset:
15
- name: ImageNet-1K
16
- type: imagenet-1k
17
- split: validation
18
- args:
19
- split: validation
20
- metrics:
21
- - type: accuracy
22
- value: 0.67624
23
- name: Top-1 Accuracy
 
 
24
  ---
25
 
26
  # MobileNet V3 Small
27
 
28
 
29
- MobileNet V3 model pre-trained on ImageNet-1k at resolution 224x224
30
 
31
 
32
  ## Model description
@@ -34,9 +39,11 @@ MobileNet V3 model pre-trained on ImageNet-1k at resolution 224x224
34
  The model was converted from a checkpoint from PyTorch Vision.
35
 
36
  The original model has:
37
- acc@1 (on ImageNet-1K): 67.668%
38
  acc@5 (on ImageNet-1K): 87.402%
39
- num_params: 2,542,856
 
 
40
  The license information of the original model was missing.
41
 
42
 
@@ -44,6 +51,83 @@ The license information of the original model was missing.
44
 
45
  The 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.
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
  ### BibTeX entry and citation info
49
  ```bibtex
 
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
  model-index:
12
+ - name: litert-community/MobileNet-v3-small
13
+ results:
14
+ - task:
15
+ type: image-classification
16
+ name: Image Classification
17
+ dataset:
18
+ name: ImageNet-1k
19
+ type: imagenet-1k
20
+ config: default
21
+ split: validation
22
+ metrics:
23
+ - name: Top 1 Accuracy (Full Precision)
24
+ type: accuracy
25
+ value: 0.6762
26
+ - name: Top 5 Accuracy (Full Precision)
27
+ type: accuracy
28
+ value: 0.8740
29
  ---
30
 
31
  # MobileNet V3 Small
32
 
33
 
34
+ MobileNet V3 model pre-trained on ImageNet-1k at resolution 224x224. Originally introduced by Andrew Howard, Mark Sandler, Grace Chu, Liang-Chieh Chen, Bo Chen, Mingxing Tan, Weijun Wang, Yukun Zhu, Ruoming Pang, Vijay Vasudevan, Quoc V. Le, and Hartwig Adam in the paper, [**Searching for MobileNetV3**](https://arxiv.org/abs/1905.02244).
35
 
36
 
37
  ## Model description
 
39
  The model was converted from a checkpoint from PyTorch Vision.
40
 
41
  The original model has:
42
+ acc@1 (on ImageNet-1K): 67.668%
43
  acc@5 (on ImageNet-1K): 87.402%
44
+ num_params: 2,542,856
45
+
46
+
47
  The license information of the original model was missing.
48
 
49
 
 
51
 
52
  The 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.
53
 
54
+ ## How to Use
55
+
56
+ ​​**1. Install Dependencies** Ensure your Python environment is set up with the required libraries. Run the following command in your terminal:
57
+
58
+ ```bash
59
+ pip install numpy Pillow huggingface_hub ai-edge-litert
60
+ ```
61
+
62
+ **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.
63
+
64
+ **3. Save the Script** Create a new file named `classify.py`, paste the script below into it, and save the file:
65
+
66
+ ```python
67
+ #!/usr/bin/env python3
68
+ import argparse, json
69
+ import numpy as np
70
+ from PIL import Image
71
+ from huggingface_hub import hf_hub_download
72
+ from ai_edge_litert.compiled_model import CompiledModel
73
+
74
+ def preprocess(img: Image.Image) -> np.ndarray:
75
+ img = img.convert("RGB")
76
+ w, h = img.size
77
+ s = 256
78
+ if w < h:
79
+ img = img.resize((s, int(round(h * s / w))), Image.BILINEAR)
80
+ else:
81
+ img = img.resize((int(round(w * s / h)), s), Image.BILINEAR)
82
+ left = (img.size[0] - 224) // 2
83
+ top = (img.size[1] - 224) // 2
84
+ img = img.crop((left, top, left + 224, top + 224))
85
+
86
+ x = np.asarray(img, dtype=np.float32) / 255.0
87
+ x = (x - np.array([0.485, 0.456, 0.406], dtype=np.float32)) / np.array(
88
+ [0.229, 0.224, 0.225], dtype=np.float32
89
+ )
90
+ return x
91
+
92
+ def main():
93
+ ap = argparse.ArgumentParser()
94
+ ap.add_argument("--image", required=True)
95
+ args = ap.parse_args()
96
+
97
+ model_path = hf_hub_download("litert-community/MobileNet-v3-small", "mobilenet_v3_small.tflite")
98
+ labels_path = hf_hub_download(
99
+ "huggingface/label-files", "imagenet-1k-id2label.json", repo_type="dataset"
100
+ )
101
+ with open(labels_path, "r", encoding="utf-8") as f:
102
+ id2label = {int(k): v for k, v in json.load(f).items()}
103
+
104
+ img = Image.open(args.image)
105
+ x = preprocess(img)
106
+
107
+ model = CompiledModel.from_file(model_path)
108
+ inp = model.create_input_buffers(0)
109
+ out = model.create_output_buffers(0)
110
+
111
+ inp[0].write(x)
112
+ model.run_by_index(0, inp, out)
113
+
114
+ req = model.get_output_buffer_requirements(0, 0)
115
+ y = out[0].read(req["buffer_size"] // np.dtype(np.float32).itemsize, np.float32)
116
+
117
+ pred = int(np.argmax(y))
118
+ label = id2label.get(pred, f"class_{pred}")
119
+
120
+ print(f"Top-1 class index: {pred}")
121
+ print(f"Top-1 label: {label}")
122
+ if __name__ == "__main__":
123
+ main()
124
+ ```
125
+
126
+ **4. Execute the Python Script** Run the below command:
127
+
128
+ ```bash
129
+ python classify.py --image cat.jpg
130
+ ```
131
 
132
  ### BibTeX entry and citation info
133
  ```bibtex