自定义 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)

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

  • 12
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
runtimeerror: failed to import transformers.models.bert.modeling_bert错误是由于在导入transformers中的BERT模型时出现了问题。该错误可能有多种可能的原因。 首先,可能是因为您没有正确安装transformers库或该库的某些依赖项。请确保已正确安装transformers库,并且您的环境中已安装了所有必需的依赖项。您可以使用pip或conda来安装该库,具体取决于您使用的是哪个Python包管理器。 其次,可能是因为您尝试导入的BERT模型的路径或名称不正确。请检查您的导入语句,并确保正确指定了BERT模型所在的路径和名称。您可以查看transformers文档来获取正确的模型导入语句示例。 另外,可能是因为您的系统缺少必需的依赖项。某些模型可能需要特定的依赖项才能正确导入。请查看transformers文档,了解与所使用的BERT模型相关的所有必备系统依赖项,并确保您的系统已正确安装它们。 最后,如果以上方法仍无法解决问题,可能是因为您的transformers库版本过旧或过新,导致与BERT模型的兼容性问题。请尝试更新或回滚transformers库的版本,以确保与您使用的BERT模型兼容的transformers版本。 综上所述,runtimeerror: failed to import transformers.models.bert.modeling_bert错误可能是由于transformers库安装问题、路径或名称错误、缺少系统依赖项或与BERT模型不兼容的库版本等问题引起的。您可以通过检查和解决以上问题来解决此错误

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

szZack

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值