sourcelite commited on
Commit
27ec410
·
1 Parent(s): 5a5f9f5

Enhanced the README.md by adding tested code along with relevant metrics

Browse files
Files changed (1) hide show
  1. README.md +124 -0
README.md CHANGED
@@ -1,8 +1,132 @@
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
  datasets:
8
  - imagenet-1k
9
+ model-index:
10
+ - name: squeezenet1_0
11
+ results:
12
+ - task:
13
+ type: image-classification
14
+ name: Image Classification
15
+ dataset:
16
+ name: ImageNet-1k
17
+ type: imagenet-1k
18
+ config: default
19
+ split: validation
20
+ metrics:
21
+ - name: Top 1 Accuracy (Full Precision)
22
+ type: accuracy
23
+ value: 0.5811
24
+ - name: Top 5 Accuracy (Full Precision)
25
+ type: accuracy
26
+ value: 0.8044
27
  ---
28
+
29
+ # squeezenet1_0
30
+
31
+ SqueezeNet 1.0 model pre-trained on ImageNet-1k at resolution 224x224. It was introduced in [SqueezeNet: AlexNet-level accuracy with 50x fewer parameters and <0.5MB model size](https://arxiv.org/abs/1602.07360) by Forrest N. Iandola, Song Han, Matthew W. Moskewicz, Khalid Ashraf, William J. Dally, and Kurt Keutzer.
32
+
33
+ ## Intended uses & limitations
34
+
35
+ 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.
36
+
37
+ ## Model description
38
+
39
+ The model was converted from a checkpoint from PyTorch Vision (`SqueezeNet1_0_Weights.IMAGENET1K_V1`).
40
+
41
+ The original model has:
42
+ acc@1 (on ImageNet-1K): 58.092%
43
+ acc@5 (on ImageNet-1K): 80.420%
44
+ num_params: 1,248,424
45
+
46
+ ## Use
47
+
48
+ ---
49
+ ```python
50
+ #!/usr/bin/env python3
51
+ import argparse
52
+ import json
53
+ import numpy as np
54
+ from PIL import Image
55
+ from huggingface_hub import hf_hub_download
56
+ from ai_edge_litert.compiled_model import CompiledModel
57
+
58
+ def preprocess(img: Image.Image) -> np.ndarray:
59
+ img = img.convert("RGB")
60
+ w, h = img.size
61
+
62
+ # Resize shortest edge to 256
63
+ s = 256
64
+ if w < h:
65
+ img = img.resize((s, int(round(h * s / w))), Image.BILINEAR)
66
+ else:
67
+ img = img.resize((int(round(w * s / h)), s), Image.BILINEAR)
68
+
69
+ # Central crop to 224x224
70
+ left = (img.size[0] - 224) // 2
71
+ top = (img.size[1] - 224) // 2
72
+ img = img.crop((left, top, left + 224, top + 224))
73
+
74
+ # Rescale to [0.0, 1.0] and Normalize
75
+ x = np.asarray(img, dtype=np.float32) / 255.0
76
+ x = (x - np.array([0.485, 0.456, 0.406], dtype=np.float32)) / np.array(
77
+ [0.229, 0.224, 0.225], dtype=np.float32
78
+ )
79
+
80
+ # Expand dimensions to create NHWC 4D tensor: (1, 224, 224, 3)
81
+ x = np.expand_dims(x, axis=0)
82
+
83
+ return x
84
+
85
+ def main():
86
+ ap = argparse.ArgumentParser()
87
+ ap.add_argument("--image", required=True, help="Path to the input image")
88
+ args = ap.parse_args()
89
+
90
+ # Download the TFLite model and labels
91
+ model_path = hf_hub_download("litert-community/squeezenet1_0", "squeezenet1_0.tflite")
92
+ labels_path = hf_hub_download(
93
+ "huggingface/label-files", "imagenet-1k-id2label.json", repo_type="dataset"
94
+ )
95
+
96
+ with open(labels_path, "r", encoding="utf-8") as f:
97
+ id2label = {int(k): v for k, v in json.load(f).items()}
98
+
99
+ img = Image.open(args.image)
100
+ x = preprocess(img)
101
+
102
+ model = CompiledModel.from_file(model_path)
103
+ inp = model.create_input_buffers(0)
104
+ out = model.create_output_buffers(0)
105
+
106
+ inp[0].write(x)
107
+ model.run_by_index(0, inp, out)
108
+
109
+ req = model.get_output_buffer_requirements(0, 0)
110
+ y = out[0].read(req["buffer_size"] // np.dtype(np.float32).itemsize, np.float32)
111
+
112
+ pred = int(np.argmax(y))
113
+ label = id2label.get(pred, f"class_{pred}")
114
+
115
+ print(f"Top-1 class index: {pred}")
116
+ print(f"Top-1 label: {label}")
117
+
118
+ if __name__ == "__main__":
119
+ main()
120
+
121
+ ### BibTeX Entry and Citation Info
122
+ ```bibtex
123
+ @misc{iandola2016squeezenetalexnetlevelaccuracy50x,
124
+ title={SqueezeNet: AlexNet-level accuracy with 50x fewer parameters and <0.5MB model size},
125
+ author={Forrest N. Iandola and Song Han and Matthew W. Moskewicz and Khalid Ashraf and William J. Dally and Kurt Keutzer},
126
+ year={2016},
127
+ eprint={1602.07360},
128
+ archivePrefix={arXiv},
129
+ primaryClass={cs.CV},
130
+ url={https://arxiv.org/abs/1602.07360},
131
+ }
132
+ ```