Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exporting the vit_b model with sam exporter ? #422

Open
flores-o opened this issue May 7, 2024 · 1 comment
Open

exporting the vit_b model with sam exporter ? #422

flores-o opened this issue May 7, 2024 · 1 comment

Comments

@flores-o
Copy link

flores-o commented May 7, 2024

url: "https://huggingface.co/schmuell/sam-b-fp16/resolve/main/sam_vit_b_01ec64.encoder-fp16.onnx",

Hi @guschmue, can you share the command used with the sam exporter tool to get this onnx file?

p.s. : I tried exporting the vit_b version with the sam exporter tool and got a larger onnx file (360 MB compared with yours 180 MB) that runs slower in browser with webgpu. Did you convert the model weights to mixed precision/ half precision before exporting it with sam_exporter?

Thank you

@SangbumChoi
Copy link

@flores-o I think you can see by the netron that there is an upcast layer in the file.

import torch
import torchvision.models as models

# Load a pre-trained model
model = models.resnet18(pretrained=True)

# Convert the model to half-precision (FP16)
model = model.half()

# Quantize the model parameters to FP16
quantized_model = torch.quantization.quantize_dynamic(
    model, {torch.nn.Linear}, dtype=torch.float16
)

# Export the quantized model to ONNX format
torch.onnx.export(
    quantized_model,
    torch.randn(1, 3, 224, 224).half(),  # Input shape
    "quantized_resnet.onnx",
    input_names=["input"],
    output_names=["output"],
    opset_version=11,
    example_outputs=torch.randn(1, 1000).half()  # Provide example outputs for dynamic axes
)

Screenshot 2024-05-10 at 2 51 02 PM Screenshot 2024-05-10 at 2 48 58 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants