我尝试使用 onnxjs 导入一个 ONNX 模型,但遇到了以下错误:
Uncaught (in promise) TypeError: cannot resolve operator 'Cast' with opsets: ai.onnx v11
下面是我 HTML 文件中的代码片段。
<html> <head> </head> <body> <!-- Load ONNX.js --> <script src="https://cdn.jsdelivr.net/npm/onnxjs/dist/onnx.min.js"></script> <!-- Code that consume ONNX.js --> <script> async function test(){ const sess = new onnx.InferenceSession() await sess.loadModel('./onnx_model.onnx') } test() </script> </body></html>
如何解决这个问题?
回答:
这将加载 Resnet 50 模型
const sess = new onnx.InferenceSession()async function test(){ console.time("loading model") await sess.loadModel('https://microsoft.github.io/onnxjs-demo/resnet50v2.onnx') console.timeEnd("loading model") console.log("model loaded");}document.querySelector('#load').addEventListener('click', test);
<script src="https://cdn.jsdelivr.net/npm/onnxjs/dist/onnx.min.js"></script><button id="load">load model</button>
错误信息表明与Cast 操作符有关,可能是 opset 11 不支持该操作符,你可能需要使用 Cast-9。你可能需要生成一个新的模型。
编辑
你的模型使用 onnxruntime python 可以加载
sess = onnxruntime.InferenceSession('../../Downloads/onnx_model.onnx');{i.name: i.shape for i in sess.get_inputs()}{o.name: o.shape for o in sess.get_outputs()}
{'input_ids': ['batch', 'sequence'], 'attention_mask': ['batch', 'sequence'], 'token_type_ids': ['batch', 'sequence']}{'output_0': ['batch', 2]}
一个想法
你可能需要自己调试,希望唯一的问题是 Cast 操作符。
你可以从这里开始查看 onnxjs 对操作符的支持,并重写模型中出现该操作符的部分。
例如,Cast 操作符只出现一次,你可以按以下方式定位它
import onnxmodel = onnx.load('../../Downloads/onnx_model.onnx')for node in model.graph.node: if 'cast' in node.op_type.lower(): print(node.name, node.op_type)
这将打印
Cast_2 Cast
使用 https://netron.app/(或桌面版本),你可以看到它是
所以你应该简单地重写模型中如何处理注意力掩码(attention mask),一个可能的解决方案是将 unsqueeze
和 cast
操作移到模型之外进行处理。