shreyajn commited on
Commit
bb040db
1 Parent(s): 324a13a

Upload README.md with huggingface_hub

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