qaihm-bot commited on
Commit
2934d01
·
verified ·
1 Parent(s): 36b65ca

Upload README.md with huggingface_hub

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