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

#1
Files changed (1) hide show
  1. README.md +118 -1
README.md CHANGED
@@ -1,8 +1,125 @@
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: ResNet34
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.7330
26
+ - name: Top 5 Accuracy (Full Precision)
27
+ type: accuracy
28
+ value: 0.9142
29
+ - name: Top 1 Accuracy (Dynamic Quantized wi8 afp32)
30
+ type: accuracy
31
+ value: 0.7328
32
+ - name: Top 5 Accuracy (Dynamic Quantized wi8 afp32)
33
+ type: accuracy
34
+ value: 0.9141
35
+ ---
36
+
37
+ # ResNet 34
38
+
39
+ The ResNet-34 architecture is a convolutional neural network pre-trained on the ImageNet-1k dataset. Originally introduced by He et al. in the landmark paper, [**Deep Residual Learning for Image Recognition**](https://arxiv.org/pdf/1512.03385), this model utilizes residual mapping to overcome the vanishing gradient problem, enabling the training of substantially deeper networks.
40
+
41
+
42
+ ## Model description
43
+
44
+ The model was converted from a checkpoint from PyTorch Vision.
45
+
46
+ The original model has:
47
+ acc@1 (on ImageNet-1K): 73.314%
48
+ acc@5 (on ImageNet-1K): 91.42%
49
+ num_params: 21,797,672
50
+
51
+ ## Intended uses & limitations
52
+
53
+ 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.
54
+
55
+ ## Use
56
+
57
+ ```python
58
+ #!/usr/bin/env python3
59
+ import argparse, json
60
+ import numpy as np
61
+ from PIL import Image
62
+ from huggingface_hub import hf_hub_download
63
+ from ai_edge_litert.compiled_model import CompiledModel
64
+
65
+ def preprocess(img: Image.Image) -> np.ndarray:
66
+ img = img.convert("RGB")
67
+ w, h = img.size
68
+ s = 256
69
+ if w < h:
70
+ img = img.resize((s, int(round(h * s / w))), Image.BILINEAR)
71
+ else:
72
+ img = img.resize((int(round(w * s / h)), s), Image.BILINEAR)
73
+ left = (img.size[0] - 224) // 2
74
+ top = (img.size[1] - 224) // 2
75
+ img = img.crop((left, top, left + 224, top + 224))
76
+
77
+ x = np.asarray(img, dtype=np.float32) / 255.0
78
+ x = (x - np.array([0.485, 0.456, 0.406], dtype=np.float32)) / np.array(
79
+ [0.229, 0.224, 0.225], dtype=np.float32
80
+ )
81
+ return np.transpose(x, (2, 0, 1))
82
+
83
+ def main():
84
+ ap = argparse.ArgumentParser()
85
+ ap.add_argument("--image", required=True)
86
+ args = ap.parse_args()
87
+
88
+ model_path = hf_hub_download("litert-community/resnet34", "resnet34.tflite")
89
+ labels_path = hf_hub_download(
90
+ "huggingface/label-files", "imagenet-1k-id2label.json", repo_type="dataset"
91
+ )
92
+ with open(labels_path, "r", encoding="utf-8") as f:
93
+ id2label = {int(k): v for k, v in json.load(f).items()}
94
+
95
+ img = Image.open(args.image)
96
+ x = preprocess(img)
97
+
98
+ model = CompiledModel.from_file(model_path)
99
+ inp = model.create_input_buffers(0)
100
+ out = model.create_output_buffers(0)
101
+
102
+ inp[0].write(x)
103
+ model.run_by_index(0, inp, out)
104
+
105
+ req = model.get_output_buffer_requirements(0, 0)
106
+ y = out[0].read(req["buffer_size"] // np.dtype(np.float32).itemsize, np.float32)
107
+
108
+ pred = int(np.argmax(y))
109
+ label = id2label.get(pred, f"class_{pred}")
110
+
111
+ print(f"Top-1 class index: {pred}")
112
+ print(f"Top-1 label: {label}")
113
+ if __name__ == "__main__":
114
+ main()
115
+ ```
116
+ ### BibTeX entry and citation info
117
+
118
+ ```bibtex
119
+ @inproceedings{he2016deep,
120
+ title={Deep residual learning for image recognition},
121
+ author={He, Kaiming and Zhang, Xiangyu and Ren, Shaoqing and Sun, Jian},
122
+ booktitle={Proceedings of the IEEE conference on computer vision and pattern recognition}, pages={770--778},
123
+ year={2016}
124
+ }
125
+ ```