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

#1
Files changed (1) hide show
  1. README.md +142 -0
README.md CHANGED
@@ -1,8 +1,150 @@
1
  ---
2
  library_name: litert
 
3
  tags:
4
  - vision
5
  - image-classification
 
 
6
  datasets:
7
  - imagenet-1k
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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/convnext_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.8356
26
+ - name: Top 5 Accuracy (Full Precision)
27
+ type: accuracy
28
+ value: 0.9666
29
  ---
30
+
31
+ # Convnext Small
32
+
33
+ ConvNeXt Small model designed as a balanced, pure convolutional backbone that bridges the gap between efficiency and high-performance Vision Transformers. Originally introduced by Zhuang Liu, Hanzi Mao, Chao-Yuan Wu, Christoph Feichtenhofer, Trevor Darrell, Saining Xie. in the modernized paper, [A ConvNet for the 2020s](https://arxiv.org/abs/2201.03545), this model adopts "Transformer-like" design choices—including \\(7 \times 7 \\) depthwise convolutions, inverted bottlenecks, and fewer activation/normalization layers—to achieve superior scalability. With approximately 50M parameters and 8.7 GFLOPs, it provides a "modernized" ResNet alternative that competes favorably with Swin-T in terms of accuracy and throughput for general vision tasks.
34
+
35
+
36
+ ## Model description
37
+
38
+ The model was converted from a checkpoint from PyTorch Vision.
39
+
40
+ The original model has:
41
+ acc@1 (on ImageNet-1K): 83.616%
42
+ acc@5 (on ImageNet-1K): 96.65%
43
+ num_params: 50223688
44
+
45
+
46
+ ## Intended uses & limitations
47
+
48
+ 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.
49
+
50
+
51
+
52
+ ## How to Use
53
+
54
+ ​​**1. Install Dependencies**
55
+
56
+ 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**
63
+
64
+ 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.
65
+
66
+
67
+ **3. Save the Script**
68
+
69
+ Create a new file named `classify.py`, paste the script below into it, and save the file:
70
+
71
+ ```python
72
+ #!/usr/bin/env python3
73
+ import argparse, json
74
+ import numpy as np
75
+ from PIL import Image
76
+ from huggingface_hub import hf_hub_download
77
+ from ai_edge_litert.compiled_model import CompiledModel
78
+
79
+ def preprocess(img: Image.Image) -> np.ndarray:
80
+ img = img.convert("RGB")
81
+ w, h = img.size
82
+ s = 230
83
+ if w < h:
84
+ img = img.resize((s, int(round(h * s / w))), Image.BILINEAR)
85
+ else:
86
+ img = img.resize((int(round(w * s / h)), s), Image.BILINEAR)
87
+ left = (img.size[0] - 224) // 2
88
+ top = (img.size[1] - 224) // 2
89
+ img = img.crop((left, top, left + 224, top + 224))
90
+
91
+ x = np.asarray(img, dtype=np.float32) / 255.0
92
+ x = (x - np.array([0.485, 0.456, 0.406], dtype=np.float32)) / np.array(
93
+ [0.229, 0.224, 0.225], dtype=np.float32
94
+ )
95
+ return np.expand_dims(x, axis=0)
96
+
97
+ def main():
98
+ ap = argparse.ArgumentParser()
99
+ ap.add_argument("--image", required=True)
100
+ args = ap.parse_args()
101
+
102
+ model_path = hf_hub_download("litert-community/convnext_small", “convnext_small.tflite")
103
+ labels_path = hf_hub_download(
104
+ "huggingface/label-files", "imagenet-1k-id2label.json", repo_type="dataset"
105
+ )
106
+ with open(labels_path, "r", encoding="utf-8") as f:
107
+ id2label = {int(k): v for k, v in json.load(f).items()}
108
+
109
+ img = Image.open(args.image)
110
+ x = preprocess(img)
111
+
112
+ model = CompiledModel.from_file(model_path)
113
+ inp = model.create_input_buffers(0)
114
+ out = model.create_output_buffers(0)
115
+
116
+ inp[0].write(x)
117
+ model.run_by_index(0, inp, out)
118
+
119
+ req = model.get_output_buffer_requirements(0, 0)
120
+ y = out[0].read(req["buffer_size"] // np.dtype(np.float32).itemsize, np.float32)
121
+
122
+ pred = int(np.argmax(y))
123
+ label = id2label.get(pred, f"class_{pred}")
124
+
125
+ print(f"Top-1 class index: {pred}")
126
+ print(f"Top-1 label: {label}")
127
+ if __name__ == "__main__":
128
+ main()
129
+ ```
130
+ **4. Execute the Python Script**
131
+
132
+ Run the below command:
133
+
134
+ ```bash
135
+ python classify.py --image cat.jpg
136
+ ```
137
+
138
+ ### BibTeX entry and citation info
139
+
140
+ ```bibtex
141
+ @misc{liu2022convnet2020s,
142
+ title={A ConvNet for the 2020s},
143
+ author={Zhuang Liu and Hanzi Mao and Chao-Yuan Wu and Christoph Feichtenhofer and Trevor Darrell and Saining Xie},
144
+ year={2022},
145
+ eprint={2201.03545},
146
+ archivePrefix={arXiv},
147
+ primaryClass={cs.CV},
148
+ url={https://arxiv.org/abs/2201.03545},
149
+ }
150
+ ```