import torch
from segment_anything.modeling.image_encoder import ImageEncoderViT
from collections import OrderedDict
from functools import partial
model = ImageEncoderViT(depth=32,
embed_dim=1280,
img_size=1024,
mlp_ratio=4,
norm_layer=partial(torch.nn.LayerNorm, eps=1e-6),
num_heads=16,
patch_size=16,
qkv_bias=True,
use_rel_pos=True,
global_attn_indexes=(7, 15, 23, 31),
window_size=14,
out_chans=256)
state_dict = torch.load(r'/root/segment-anything/model/sam_vit_h_4b8939.pth')
new_state_dict = OrderedDict()
for k in state_dict:
if "image_encoder" in k:
new_state_dict[k[14:]] = state_dict[k]
model.load_state_dict(new_state_dict)
x = torch.randn((1, 3, 1024, 1024))
model.eval()
torch.onnx.export(model,
x,
"./do/sam_vit_h_4b8939_image_embedding.onnx",
input_names=["input"],
output_names=["output"],
opset_version=16)
When I run it, it generates hundreds of files ending in .bias and .weights.Such as “blocks.0.attn.qkv.bias”.
Some warning here:TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can’t record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
I tried add tensor content in the image_encoder model and change the version of something.Nothing happened.