qaihm-bot commited on
Commit
d8ec576
·
verified ·
1 Parent(s): 13e6cd2

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +234 -0
README.md ADDED
@@ -0,0 +1,234 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ datasets:
3
+ - imagenet-1k
4
+ - imagenet-22k
5
+ library_name: pytorch
6
+ license: bsd-3-clause
7
+ pipeline_tag: image-classification
8
+ tags:
9
+ - quantized
10
+ - android
11
+
12
+ ---
13
+
14
+ ![](https://qaihub-public-assets.s3.us-west-2.amazonaws.com/qai-hub-models/models/convnext_tiny_w8a8_quantized/web-assets/model_demo.png)
15
+
16
+ # ConvNext-Tiny-w8a8-Quantized: Optimized for Mobile Deployment
17
+ ## Imagenet classifier and general purpose backbone
18
+
19
+ ConvNextTiny is a machine learning model that can classify images from the Imagenet dataset. It can also be used as a backbone in building more complex models for specific use cases.
20
+
21
+ This model is an implementation of ConvNext-Tiny-w8a8-Quantized found [here](https://github.com/pytorch/vision/blob/main/torchvision/models/convnext.py).
22
+ This repository provides scripts to run ConvNext-Tiny-w8a8-Quantized on Qualcomm® devices.
23
+ More details on model performance across various devices, can be found
24
+ [here](https://aihub.qualcomm.com/models/convnext_tiny_w8a8_quantized).
25
+
26
+
27
+ ### Model Details
28
+
29
+ - **Model Type:** Image classification
30
+ - **Model Stats:**
31
+ - Model checkpoint: Imagenet
32
+ - Input resolution: 224x224
33
+ - Number of parameters: 28.6M
34
+ - Model size: 28 MB
35
+ - Precision: w8a8 (8-bit weights, 8-bit activations)
36
+
37
+
38
+ | Device | Chipset | Target Runtime | Inference Time (ms) | Peak Memory Range (MB) | Precision | Primary Compute Unit | Target Model
39
+ | ---|---|---|---|---|---|---|---|
40
+ | Samsung Galaxy S23 Ultra (Android 13) | Snapdragon® 8 Gen 2 | TFLite | 33.794 ms | 7 - 34 MB | INT8 | NPU | [ConvNext-Tiny-w8a8-Quantized.tflite](https://huggingface.co/qualcomm/ConvNext-Tiny-w8a8-Quantized/blob/main/ConvNext-Tiny-w8a8-Quantized.tflite)
41
+ | Samsung Galaxy S23 Ultra (Android 13) | Snapdragon® 8 Gen 2 | QNN Model Library | 1.732 ms | 0 - 12 MB | INT8 | NPU | [ConvNext-Tiny-w8a8-Quantized.so](https://huggingface.co/qualcomm/ConvNext-Tiny-w8a8-Quantized/blob/main/ConvNext-Tiny-w8a8-Quantized.so)
42
+
43
+
44
+ ## Installation
45
+
46
+ This model can be installed as a Python package via pip.
47
+
48
+ ```bash
49
+ pip install "qai-hub-models[convnext_tiny_w8a8_quantized]"
50
+ ```
51
+
52
+
53
+
54
+ ## Configure Qualcomm® AI Hub to run this model on a cloud-hosted device
55
+
56
+ Sign-in to [Qualcomm® AI Hub](https://app.aihub.qualcomm.com/) with your
57
+ Qualcomm® ID. Once signed in navigate to `Account -> Settings -> API Token`.
58
+
59
+ With this API token, you can configure your client to run models on the cloud
60
+ hosted devices.
61
+ ```bash
62
+ qai-hub configure --api_token API_TOKEN
63
+ ```
64
+ Navigate to [docs](https://app.aihub.qualcomm.com/docs/) for more information.
65
+
66
+
67
+
68
+ ## Demo off target
69
+
70
+ The package contains a simple end-to-end demo that downloads pre-trained
71
+ weights and runs this model on a sample input.
72
+
73
+ ```bash
74
+ python -m qai_hub_models.models.convnext_tiny_w8a8_quantized.demo
75
+ ```
76
+
77
+ The above demo runs a reference implementation of pre-processing, model
78
+ inference, and post processing.
79
+
80
+ **NOTE**: If you want running in a Jupyter Notebook or Google Colab like
81
+ environment, please add the following to your cell (instead of the above).
82
+ ```
83
+ %run -m qai_hub_models.models.convnext_tiny_w8a8_quantized.demo
84
+ ```
85
+
86
+
87
+ ### Run model on a cloud-hosted device
88
+
89
+ In addition to the demo, you can also run the model on a cloud-hosted Qualcomm®
90
+ device. This script does the following:
91
+ * Performance check on-device on a cloud-hosted device
92
+ * Downloads compiled assets that can be deployed on-device for Android.
93
+ * Accuracy check between PyTorch and on-device outputs.
94
+
95
+ ```bash
96
+ python -m qai_hub_models.models.convnext_tiny_w8a8_quantized.export
97
+ ```
98
+
99
+ ```
100
+ Profile Job summary of ConvNext-Tiny-w8a8-Quantized
101
+ --------------------------------------------------
102
+ Device: Snapdragon X Elite CRD (11)
103
+ Estimated Inference Time: 1.93 ms
104
+ Estimated Peak Memory Range: 0.48-0.48 MB
105
+ Compute Units: NPU (215) | Total (215)
106
+
107
+
108
+ ```
109
+ ## How does this work?
110
+
111
+ This [export script](https://github.com/quic/ai-hub-models/blob/main/qai_hub_models/models/ConvNext-Tiny-w8a8-Quantized/export.py)
112
+ leverages [Qualcomm® AI Hub](https://aihub.qualcomm.com/) to optimize, validate, and deploy this model
113
+ on-device. Lets go through each step below in detail:
114
+
115
+ Step 1: **Compile model for on-device deployment**
116
+
117
+ To compile a PyTorch model for on-device deployment, we first trace the model
118
+ in memory using the `jit.trace` and then call the `submit_compile_job` API.
119
+
120
+ ```python
121
+ import torch
122
+
123
+ import qai_hub as hub
124
+ from qai_hub_models.models.convnext_tiny_w8a8_quantized import Model
125
+
126
+ # Load the model
127
+ torch_model = Model.from_pretrained()
128
+ torch_model.eval()
129
+
130
+ # Device
131
+ device = hub.Device("Samsung Galaxy S23")
132
+
133
+ # Trace model
134
+ input_shape = torch_model.get_input_spec()
135
+ sample_inputs = torch_model.sample_inputs()
136
+
137
+ pt_model = torch.jit.trace(torch_model, [torch.tensor(data[0]) for _, data in sample_inputs.items()])
138
+
139
+ # Compile model on a specific device
140
+ compile_job = hub.submit_compile_job(
141
+ model=pt_model,
142
+ device=device,
143
+ input_specs=torch_model.get_input_spec(),
144
+ )
145
+
146
+ # Get target model to run on-device
147
+ target_model = compile_job.get_target_model()
148
+
149
+ ```
150
+
151
+
152
+ Step 2: **Performance profiling on cloud-hosted device**
153
+
154
+ After compiling models from step 1. Models can be profiled model on-device using the
155
+ `target_model`. Note that this scripts runs the model on a device automatically
156
+ provisioned in the cloud. Once the job is submitted, you can navigate to a
157
+ provided job URL to view a variety of on-device performance metrics.
158
+ ```python
159
+ profile_job = hub.submit_profile_job(
160
+ model=target_model,
161
+ device=device,
162
+ )
163
+
164
+ ```
165
+
166
+ Step 3: **Verify on-device accuracy**
167
+
168
+ To verify the accuracy of the model on-device, you can run on-device inference
169
+ on sample input data on the same cloud hosted device.
170
+ ```python
171
+ input_data = torch_model.sample_inputs()
172
+ inference_job = hub.submit_inference_job(
173
+ model=target_model,
174
+ device=device,
175
+ inputs=input_data,
176
+ )
177
+
178
+ on_device_output = inference_job.download_output_data()
179
+
180
+ ```
181
+ With the output of the model, you can compute like PSNR, relative errors or
182
+ spot check the output with expected output.
183
+
184
+ **Note**: This on-device profiling and inference requires access to Qualcomm®
185
+ AI Hub. [Sign up for access](https://myaccount.qualcomm.com/signup).
186
+
187
+
188
+ ## Run demo on a cloud-hosted device
189
+
190
+ You can also run the demo on-device.
191
+
192
+ ```bash
193
+ python -m qai_hub_models.models.convnext_tiny_w8a8_quantized.demo --on-device
194
+ ```
195
+
196
+ **NOTE**: If you want running in a Jupyter Notebook or Google Colab like
197
+ environment, please add the following to your cell (instead of the above).
198
+ ```
199
+ %run -m qai_hub_models.models.convnext_tiny_w8a8_quantized.demo -- --on-device
200
+ ```
201
+
202
+
203
+ ## Deploying compiled model to Android
204
+
205
+
206
+ The models can be deployed using multiple runtimes:
207
+ - TensorFlow Lite (`.tflite` export): [This
208
+ tutorial](https://www.tensorflow.org/lite/android/quickstart) provides a
209
+ guide to deploy the .tflite model in an Android application.
210
+
211
+
212
+ - QNN (`.so` export ): This [sample
213
+ app](https://docs.qualcomm.com/bundle/publicresource/topics/80-63442-50/sample_app.html)
214
+ provides instructions on how to use the `.so` shared library in an Android application.
215
+
216
+
217
+ ## View on Qualcomm® AI Hub
218
+ Get more details on ConvNext-Tiny-w8a8-Quantized's performance across various devices [here](https://aihub.qualcomm.com/models/convnext_tiny_w8a8_quantized).
219
+ Explore all available models on [Qualcomm® AI Hub](https://aihub.qualcomm.com/)
220
+
221
+ ## License
222
+ - The license for the original implementation of ConvNext-Tiny-w8a8-Quantized can be found
223
+ [here](https://github.com/pytorch/vision/blob/main/LICENSE).
224
+ - The license for the compiled assets for on-device deployment can be found [here]({deploy_license_url})
225
+
226
+ ## References
227
+ * [A ConvNet for the 2020s](https://arxiv.org/abs/2201.03545)
228
+ * [Source Model Implementation](https://github.com/pytorch/vision/blob/main/torchvision/models/convnext.py)
229
+
230
+ ## Community
231
+ * Join [our AI Hub Slack community](https://qualcomm-ai-hub.slack.com/join/shared_invite/zt-2d5zsmas3-Sj0Q9TzslueCjS31eXG2UA#/shared-invite/email) to collaborate, post questions and learn more about on-device AI.
232
+ * For questions or feedback please [reach out to us](mailto:ai-hub-support@qti.qualcomm.com).
233
+
234
+