Skip to content

Commit 4046ea3

Browse files
Shiva ChilukamariShiva Chilukamari
authored andcommitted
Merge branch 'sam2.1_hiera_small' of https://github.com/CodeLinaro/olive-recipes into sam_vit_base
Combining recipes for facebook/sam-vit-base and facebook/sam2.1-hiera-small
2 parents fba8ffe + 81f4b3b commit 4046ea3

10 files changed

Lines changed: 947 additions & 0 deletions

sam2.1-hiera-small/LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Microsoft.
2+
Copyright (c) Microsoft Corporation.
3+
4+
MIT License
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.

sam2.1-hiera-small/QNN/README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# SAM2 Model Conversion
2+
3+
This repository demonstrates the optimization of the [sam2.1-hiera-small](https://github.com/facebookresearch/sam2) model using **post-training quantization (PTQ)** techniques.
4+
5+
6+
### Quantization Python Environment Setup
7+
Quantization is resource-intensive and requires GPU acceleration
8+
9+
For GPU Environment Setup:
10+
```bash
11+
pip install -r requirements.txt
12+
```
13+
14+
### AOT Compilation Python Environment Setup
15+
Model compilation using QNN Execution Provider requires a Python environment with onnxruntime-qnn installed. In a separate Python environment, install the required packages:
16+
17+
```bash
18+
# Install Olive & ORT-QNN
19+
pip install olive-ai onnxruntime-qnn torch torchvision transformers
20+
```
21+
22+
Replace `/path/to/qnn/env/bin` in [sam21_vision_encoder_qnn_ctx.json](sam21_vision_encoder_qnn_ctx.json) and [sam21_mask_decoder_qnn_ctx.json](sam21_mask_decoder_qnn_ctx.json) with the path to the directory containing your QNN environment's Python executable. This path can be found by running the following command in the environment:
23+
24+
```bash
25+
# Linux
26+
command -v python
27+
# Windows
28+
# where python
29+
```
30+
31+
This command will return the path to the Python executable. Set the parent directory of the executable as the `/path/to/qnn/env/bin` in the config file.
32+
33+
### Generate ONNX Model
34+
Activate the **Quantization Python Environment** and run command to generate encoder and decoder models:
35+
36+
```bash
37+
python generate_model.py
38+
```
39+
40+
### Run the Quantization + Compilation Config
41+
Activate the **Quantization Python Environment** and run the workflow:
42+
43+
For Encoder Model:
44+
```bash
45+
olive run --config sam21_vision_encoder_qnn_ctx.json
46+
```
47+
48+
For Decoder Model:
49+
```bash
50+
olive run --config sam21_mask_decoder_qnn_ctx.json
51+
```
52+
53+
> ⚠️ If optimization fails during context binary generation, rerun the command. The process will resume from the last completed step.
54+
55+
### Model ORT Execution
56+
57+
Execute SAM model in **AOT Compilation Python Environment** using following command:
58+
59+
```bash
60+
python sam2_mask_generator.py --model_ve path/to/encoder_model.onnx --model_md path/to/decoder_model.onnx --image_path car.png --box_x 40 --box_y 235 --box_w 940 --box_h 490 --output_path car_mask.png
61+
```

sam2.1-hiera-small/QNN/config.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# -------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License.
4+
# --------------------------------------------------------------------------
5+
6+
7+
class ModelConfig:
8+
model_name = "facebook/sam2.1-hiera-small"
9+
data_dir = "quantization_dataset_100"
10+
ve_input_name = "pixel_values"
11+
ve_channel_size = 3
12+
ve_sample_size = 1024
13+
md_input_names = ("image_embeddings", "high_res_features1", "high_res_features2", "coords.1", "labels")
14+
ms_input_shapes = ((1, 256, 64, 64), (1, 32, 256, 256), (1, 64, 128, 128), (1, 5, 2), (1, 5))

0 commit comments

Comments
 (0)