自定义 bert 在 onnxruntime 推理错误:TypeError: run(): incompatible function arguments

自定义 bert 在 onnxruntime 推理错误:TypeError: run(): incompatible function arguments

自定义 bert 在 onnxruntime 推理错误:TypeError: run(): incompatible function arguments

推理代码

复制代码
    # text embedding
    toks = self.tokenizer([text])
    if self.debug:
        print('toks', toks)

    text_embed = self.text_model_session.run(output_names=['output'], input_feed=toks)

错误提示

复制代码
Traceback (most recent call last):
  File "/xx/workspace/model/test_onnx.py", line 90, in <module>
    res = inferencer.inference(text, img_path)
  File "/xx/workspace/model/test_onnx.py", line 58, in inference
    text_embed = self.text_model_session.run(output_names=['output'], input_feed=toks)
  File "/xx/miniconda3/envs/py39/lib/python3.9/site-packages/onnxruntime/capi/onnxruntime_inference_collection.py", line 220, in run
    return self._sess.run(output_names, input_feed, run_options)
TypeError: run(): incompatible function arguments. The following argument types are supported:
    1. (self: onnxruntime.capi.onnxruntime_pybind11_state.InferenceSession, arg0: List[str], arg1: Dict[str, object], arg2: onnxruntime.capi.onnxruntime_pybind11_state.RunOptions) -> List[object]

Invoked with: <onnxruntime.capi.onnxruntime_pybind11_state.InferenceSession object at 0x7f975ded1570>, ['output'], {'input_ids': array([[ 101, 3899,  102]]), 'token_type_ids': array([[0, 0, 0]]), 'attention_mask': array([[1, 1, 1]])}, None

核心错误

复制代码
TypeError: run(): incompatible function arguments. The following argument types are supported:
    1. (self: onnxruntime.capi.onnxruntime_pybind11_state.InferenceSession, arg0: List[str], arg1: Dict[str, object], arg2: onnxruntime.capi.onnxruntime_pybind11_state.RunOptions) -> List[object]

解决方法

核对参数

arg0: List[str]

arg1: Dict[str, object]

对应的参数

复制代码
output_names=['output'], input_feed=toks

arg0=['output'] 参数类型正确

arg1=toks 表面看参数也正常,打印看看toks的每个值的类型

type(toks['input_ids']) 输出为 <class 'torch.Tensor'>, 实际需要输入类型为 <class 'numpy.ndarray'>

修改代码

复制代码
    # text embedding
    toks = self.tokenizer([text])
    if self.debug:
        print('toks', toks)
    
    text_input = {}
    text_input['input_ids'] = toks['input_ids'].numpy()
    text_input['token_type_ids'] = toks['token_type_ids'].numpy()
    text_input['attention_mask'] = toks['attention_mask'].numpy()
    text_embed = self.text_model_session.run(output_names=['output'], input_feed=text_input)

再次执行代码,正常运行,无报错!!

相关推荐
凌晨一点的秃头猪3 分钟前
ORB局部描述子提取
人工智能·分类·数据挖掘
独处东汉9 分钟前
freertos开发空气检测仪之延迟函数设计:DWT软件实现
人工智能·stm32·单片机·嵌入式硬件
阿杰学AI11 分钟前
AI核心知识67——大语言模型之NTP (简洁且通俗易懂版)
人工智能·ai·语言模型·自然语言处理·aigc·ntp·机械学习
码农三叔15 分钟前
(6-2)手部、足部与末端执行器设计:足部结构
人工智能·架构·机器人·人形机器人
玄同76524 分钟前
SQLAlchemy 初始化全流程详解:从引擎到会话的每一步
数据库·人工智能·python·sql·mysql·语言模型·知识图谱
小雨青年26 分钟前
Cursor 项目实战:AI播客策划助手(四)—— 产品发布与交付收尾
前端·人工智能
Jorunk28 分钟前
使用F5-TTS训练自己的数据
人工智能·语音识别
玄同76529 分钟前
大模型生成 Token 的原理:从文本到模型理解的 “翻译官”
人工智能·python·语言模型·自然语言处理·nlp·知识图谱·token
源于花海33 分钟前
深度迁移学习:深度神经网络的可迁移性
人工智能·机器学习·迁移学习·深度神经网络·深度迁移学习
源于花海34 分钟前
深度迁移学习:最简单的深度迁移——Finetune(微调)
人工智能·机器学习·迁移学习·finetune·深度迁移学习