直接按转torch 模型的方式 组织输入就可以,不用写config啥的。
import torch
from transformers import AutoTokenizer, AutoConfig
from models.layoutxlm import LayoutXLMForTokenClassification
model = LayoutXLMForTokenClassification.from_pretrained('model_path')
tokenizer = AutoTokenizer.from_pretrained('model_path')
dummy_model_input = tokenizer("This is a sample", return_tensors=None)
dummy_model_input['input_ids'] = torch.zeros((1,512),dtype=torch.int64) + 10
dummy_model_input['bbox'] = torch.zeros((1,512, 4), dtype=torch.int64) + 20
dummy_model_input['images'] = torch.zeros((3,224, 224),dtype=torch.float32)
dummy_model_input['attention_mask'] = torch.zeros((1,512), dtype=torch.int64) + 1
# export
torch.onnx.export(
model,
tuple((dummy_model_input['input_ids'], dummy_model_input['bbox'], dummy_model_input['images'], dummy_model_input['attention_mask'])),
f="out_onnx_path",
input_names=['input_ids', 'bbox', 'images', 'attention_mask'],
output_names=['logits'],
dynamic_axes={'input_ids': {0: 'batch_size'},
'bbox': {0: 'batch_size'},
'attention_mask': {0: 'batch_size'},
},
do_constant_folding=True,
opset_version=13,
)