qaihm-bot commited on
Commit
70de015
·
verified ·
1 Parent(s): 9da655e

Upload README.md with huggingface_hub

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