qaihm-bot commited on
Commit
cdbd5cd
·
verified ·
1 Parent(s): e04f8b5

Upload README.md with huggingface_hub

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