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

news/2024/7/6 5:19:09 标签: bert, 人工智能, onnxruntime

自定义 bertonnxruntime 推理错误:TypeError: run(): incompatible function arguments

  • 自定义 bertonnxruntime 推理错误:TypeError: run(): incompatible function arguments
    • 推理代码
    • 错误提示
    • 核心错误
  • 解决方法
    • 核对参数
    • 修改代码

bert__onnxruntime_TypeError_run_incompatible_function_arguments_2">自定义 bertonnxruntime 推理错误: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)

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


http://www.niftyadmin.cn/n/5314196.html

相关文章

sqlalchemy expire_all 方法详解,强制刷新会话缓存

在 SQLAlchemy 中&#xff0c;expire_all() 方法是 Session 类的一个重要方法&#xff0c;用于使会话&#xff08;Session&#xff09;中所有当前加载的对象过期。这意味着下次访问任何已加载对象的属性时&#xff0c;SQLAlchemy 将从数据库中重新加载这些对象的最新状态。这是…

计算机网络(湖科大)第一章 概述

文章目录 &#x1f4e3;1.1 计算机网络在信息时代的作用&#x1f4e3;1.2 因特网概述✨1.2.1 网络、互联网和因特网✨1.2.2 因特网发展的三个阶段✨1.2.3 因特网的标准化工作✨1.2.4 因特网的组成 &#x1f4e3;1.3 三种交换方式✨1.3.1 电路交换&#xff08;Circuit Switching…

[算法与数据结构][c++][python]:C++与Python中的赋值、浅拷贝与深拷贝

C与Python中的赋值、浅拷贝与深拷贝 写在前面&#xff1a;Python和C中的赋值与深浅拷贝&#xff0c;由于其各自语言特性的问题&#xff0c;在概念和实现上稍微有点差异&#xff0c;本文将这C和Python中的拷贝与赋值放到一起&#xff0c;希望通过对比学习两语言实现上的异同点&a…

红队打靶练习:RICKDICULOUSLYEASY: 1

目录 信息收集 1、arp 2、nmap 3、nikto 4、whatweb 目录探测 gobuster dirsearch WEB get flag1 /robots.txt FTP get flag2 telenet登录 get flag3 get flag4 9090端口 get flag5 dirsearch ssh登录 Summer用户 get flag6 信息收集 get flag7 get fl…

zabbix监控windows主机

下载安装zabbix agent安装包 Zabbix官网下载地址: https://www.zabbix.com/cn/download_agents?version5.0LTS&release5.0.40&osWindows&os_versionAny&hardwareamd64&encryptionOpenSSL&packagingMSI&show_legacy0 这里使用zabbix agent2 安装 …

系列十四、理解MySQL varchar(50)

一、理解MySQL varchar(50) 1.1、概述 日常开发中&#xff0c;数据库建表是必不可少的一个环节&#xff0c;建表的时候通常会看到设定某个字段的长度为varchar(50)&#xff0c;例如如下建表语句&#xff1a; 那么怎么理解varchar(50)&#xff1f;这个分情况的&#xff0c;MySQ…

科研上新 | 第6期:优化LLM数学推理;深度学习建模基因表达调控;基于深度学习的近实时海洋碳汇估算

编者按&#xff1a;欢迎阅读“科研上新”栏目&#xff01;“科研上新”汇聚了微软亚洲研究院最新的创新成果与科研动态。在这里&#xff0c;你可以快速浏览研究院的亮点资讯&#xff0c;保持对前沿领域的敏锐嗅觉&#xff0c;同时也能找到先进实用的开源工具。 本期内容速览 …

vue购物车案例,v-model 之 lazy、number、trim,与后端交互

购物车案例 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>Title</title><script src"./js/vue.js"></script> </head> <body> <div id"d1"&…