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

#1
Files changed (1) hide show
  1. README.md +131 -0
README.md CHANGED
@@ -1,8 +1,139 @@
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/alexnet
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.5654
26
+ - name: Top 5 Accuracy (Full Precision)
27
+ type: accuracy
28
+ value: 0.7910
29
  ---
30
+
31
+ # AlexNet
32
+
33
+ The AlexNet architecture is a convolutional neural network pre-trained on the ImageNet-1k dataset. Originally introduced by Krizhevsky et al. in the landmark paper, [**ImageNet Classification with Deep Convolutional Neural Networks**](https://papers.nips.cc/paper/2012/hash/c399862d3b9d6b76c8436e924a68c45b-Abstract.html), this model utilized deep layers and GPU acceleration to prove the effectiveness of deep learning.
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): 56.52%
42
+ acc@5 (on ImageNet-1K): 79.06%
43
+ num_params: 61,100,840
44
+
45
+ ## Intended uses & limitations
46
+
47
+ 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.
48
+
49
+ ## How to Use
50
+
51
+ **1. Install Dependencies** Ensure your Python environment is set up with the required libraries. Run the following command in your terminal:
52
+
53
+ ```bash
54
+ pip install numpy Pillow huggingface_hub ai-edge-litert
55
+ ```
56
+
57
+
58
+ **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.
59
+
60
+
61
+ **3. Save the Script** Create a new file named `classify.py`, paste the script below into it, and save the file:
62
+
63
+ ```python
64
+ #!/usr/bin/env python3
65
+ import argparse, json
66
+ import numpy as np
67
+ from PIL import Image
68
+ from huggingface_hub import hf_hub_download
69
+ from ai_edge_litert.compiled_model import CompiledModel
70
+
71
+ def preprocess(img: Image.Image) -> np.ndarray:
72
+ img = img.convert("RGB")
73
+ w, h = img.size
74
+ s = 256
75
+ if w < h:
76
+ img = img.resize((s, int(round(h * s / w))), Image.BILINEAR)
77
+ else:
78
+ img = img.resize((int(round(w * s / h)), s), Image.BILINEAR)
79
+ left = (img.size[0] - 224) // 2
80
+ top = (img.size[1] - 224) // 2
81
+ img = img.crop((left, top, left + 224, top + 224))
82
+
83
+ x = np.asarray(img, dtype=np.float32) / 255.0
84
+ x = (x - np.array([0.485, 0.456, 0.406], dtype=np.float32)) / np.array(
85
+ [0.229, 0.224, 0.225], dtype=np.float32
86
+ )
87
+ return x
88
+
89
+ def main():
90
+ ap = argparse.ArgumentParser()
91
+ ap.add_argument("--image", required=True)
92
+ args = ap.parse_args()
93
+
94
+ model_path = hf_hub_download("litert-community/alexnet", "alexnet.tflite")
95
+ labels_path = hf_hub_download(
96
+ "huggingface/label-files", "imagenet-1k-id2label.json", repo_type="dataset"
97
+ )
98
+ with open(labels_path, "r", encoding="utf-8") as f:
99
+ id2label = {int(k): v for k, v in json.load(f).items()}
100
+
101
+ img = Image.open(args.image)
102
+ x = preprocess(img)
103
+
104
+ model = CompiledModel.from_file(model_path)
105
+ inp = model.create_input_buffers(0)
106
+ out = model.create_output_buffers(0)
107
+
108
+ inp[0].write(x)
109
+ model.run_by_index(0, inp, out)
110
+
111
+ req = model.get_output_buffer_requirements(0, 0)
112
+ y = out[0].read(req["buffer_size"] // np.dtype(np.float32).itemsize, np.float32)
113
+
114
+ pred = int(np.argmax(y))
115
+ label = id2label.get(pred, f"class_{pred}")
116
+
117
+ print(f"Top-1 class index: {pred}")
118
+ print(f"Top-1 label: {label}")
119
+ if __name__ == "__main__":
120
+ main()
121
+ ```
122
+
123
+ **4. Execute the Python Script** Run the below command:
124
+
125
+ ```bash
126
+ python classify.py --image cat.jpg
127
+ ```
128
+
129
+ ### BibTeX entry and citation info
130
+
131
+ ```bibtex
132
+ @article{krizhevsky2012imagenet,
133
+ title={Imagenet classification with deep convolutional neural networks},
134
+ author={Krizhevsky, Alex and Sutskever, Ilya and Hinton, Geoffrey E},
135
+ journal={Advances in neural information processing systems},
136
+ volume={25},
137
+ year={2012}
138
+ }
139
+ ```