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
+
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/resnet18_quantized/web-assets/banner.png)
|
16 |
+
|
17 |
+
# ResNet18Quantized: Optimized for Mobile Deployment
|
18 |
+
## Imagenet classifier and general purpose backbone
|
19 |
+
|
20 |
+
ResNet18 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 ResNet18Quantized found [here](https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py).
|
23 |
+
This repository provides scripts to run ResNet18Quantized on Qualcomm® devices.
|
24 |
+
More details on model performance across various devices, can be found
|
25 |
+
[here](https://aihub.qualcomm.com/models/resnet18_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: 11.7M
|
35 |
+
- Model size: 11.3 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 | 0.375 ms | 0 - 14 MB | FP16 | NPU | [ResNet18Quantized.tflite](https://huggingface.co/qualcomm/ResNet18Quantized/blob/main/ResNet18Quantized.tflite)
|
41 |
+
| Samsung Galaxy S23 Ultra (Android 13) | Snapdragon® 8 Gen 2 | QNN Model Library | 0.359 ms | 0 - 68 MB | FP16 | NPU | [ResNet18Quantized.so](https://huggingface.co/qualcomm/ResNet18Quantized/blob/main/ResNet18Quantized.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
|
50 |
+
```
|
51 |
+
|
52 |
+
|
53 |
+
## Configure Qualcomm® AI Hub to run this model on a cloud-hosted device
|
54 |
+
|
55 |
+
Sign-in to [Qualcomm® AI Hub](https://app.aihub.qualcomm.com/) with your
|
56 |
+
Qualcomm® ID. Once signed in navigate to `Account -> Settings -> API Token`.
|
57 |
+
|
58 |
+
With this API token, you can configure your client to run models on the cloud
|
59 |
+
hosted devices.
|
60 |
+
```bash
|
61 |
+
qai-hub configure --api_token API_TOKEN
|
62 |
+
```
|
63 |
+
Navigate to [docs](https://app.aihub.qualcomm.com/docs/) for more information.
|
64 |
+
|
65 |
+
|
66 |
+
|
67 |
+
## Demo off target
|
68 |
+
|
69 |
+
The package contains a simple end-to-end demo that downloads pre-trained
|
70 |
+
weights and runs this model on a sample input.
|
71 |
+
|
72 |
+
```bash
|
73 |
+
python -m qai_hub_models.models.resnet18_quantized.demo
|
74 |
+
```
|
75 |
+
|
76 |
+
The above demo runs a reference implementation of pre-processing, model
|
77 |
+
inference, and post processing.
|
78 |
+
|
79 |
+
**NOTE**: If you want running in a Jupyter Notebook or Google Colab like
|
80 |
+
environment, please add the following to your cell (instead of the above).
|
81 |
+
```
|
82 |
+
%run -m qai_hub_models.models.resnet18_quantized.demo
|
83 |
+
```
|
84 |
+
|
85 |
+
|
86 |
+
### Run model on a cloud-hosted device
|
87 |
+
|
88 |
+
In addition to the demo, you can also run the model on a cloud-hosted Qualcomm®
|
89 |
+
device. This script does the following:
|
90 |
+
* Performance check on-device on a cloud-hosted device
|
91 |
+
* Downloads compiled assets that can be deployed on-device for Android.
|
92 |
+
* Accuracy check between PyTorch and on-device outputs.
|
93 |
+
|
94 |
+
```bash
|
95 |
+
python -m qai_hub_models.models.resnet18_quantized.export
|
96 |
+
```
|
97 |
+
|
98 |
+
```
|
99 |
+
Profile Job summary of ResNet18Quantized
|
100 |
+
--------------------------------------------------
|
101 |
+
Device: Samsung Galaxy S23 Ultra (13)
|
102 |
+
Estimated Inference Time: 0.38 ms
|
103 |
+
Estimated Peak Memory Range: 0.01-14.00 MB
|
104 |
+
Compute Units: NPU (37) | Total (37)
|
105 |
+
|
106 |
+
Profile Job summary of ResNet18Quantized
|
107 |
+
--------------------------------------------------
|
108 |
+
Device: Samsung Galaxy S23 Ultra (13)
|
109 |
+
Estimated Inference Time: 0.36 ms
|
110 |
+
Estimated Peak Memory Range: 0.01-67.99 MB
|
111 |
+
Compute Units: NPU (35) | Total (35)
|
112 |
+
|
113 |
+
|
114 |
+
```
|
115 |
+
## How does this work?
|
116 |
+
|
117 |
+
This [export script](https://github.com/quic/ai-hub-models/blob/main/qai_hub_models/models/ResNet18Quantized/export.py)
|
118 |
+
leverages [Qualcomm® AI Hub](https://aihub.qualcomm.com/) to optimize, validate, and deploy this model
|
119 |
+
on-device. Lets go through each step below in detail:
|
120 |
+
|
121 |
+
Step 1: **Compile model for on-device deployment**
|
122 |
+
|
123 |
+
To compile a PyTorch model for on-device deployment, we first trace the model
|
124 |
+
in memory using the `jit.trace` and then call the `submit_compile_job` API.
|
125 |
+
|
126 |
+
```python
|
127 |
+
import torch
|
128 |
+
|
129 |
+
import qai_hub as hub
|
130 |
+
from qai_hub_models.models.resnet18_quantized import Model
|
131 |
+
|
132 |
+
# Load the model
|
133 |
+
torch_model = Model.from_pretrained()
|
134 |
+
torch_model.eval()
|
135 |
+
|
136 |
+
# Device
|
137 |
+
device = hub.Device("Samsung Galaxy S23")
|
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 |
+
|
184 |
+
on_device_output = inference_job.download_output_data()
|
185 |
+
|
186 |
+
```
|
187 |
+
With the output of the model, you can compute like PSNR, relative errors or
|
188 |
+
spot check the output with expected output.
|
189 |
+
|
190 |
+
**Note**: This on-device profiling and inference requires access to Qualcomm®
|
191 |
+
AI Hub. [Sign up for early access](https://aihub.qualcomm.com/sign-up).
|
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.resnet18_quantized.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.resnet18_quantized.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 ResNet18Quantized's performance across various devices [here](https://aihub.qualcomm.com/models/resnet18_quantized).
|
225 |
+
Explore all available models on [Qualcomm® AI Hub](https://aihub.qualcomm.com/)
|
226 |
+
|
227 |
+
## License
|
228 |
+
- The license for the original implementation of ResNet18Quantized can be found
|
229 |
+
[here](https://github.com/pytorch/vision/blob/main/LICENSE).
|
230 |
+
- 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).
|
231 |
+
|
232 |
+
## References
|
233 |
+
* [Deep Residual Learning for Image Recognition](https://arxiv.org/abs/1512.03385)
|
234 |
+
* [Source Model Implementation](https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py)
|
235 |
+
|
236 |
+
## Community
|
237 |
+
* 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.
|
238 |
+
* For questions or feedback please [reach out to us](mailto:ai-hub-support@qti.qualcomm.com).
|
239 |
+
|
240 |
+
|