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