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