【发布时间】:2020-02-18 12:48:03
【问题描述】:
我正在尝试使用 ONNX.js 在浏览器中运行 ONNX 对象检测模型。我知道在 Tensorflow.js 中,您只需将图像对象传递给模型,Tensorflow 会自动创建模型所需的张量,但在 ONNX 中,我们必须从图像创建张量,然后将其传递给模型。
我阅读了官方存储库 - https://github.com/Microsoft/onnxjs,但我找不到任何关于如何在 Javascript 中从图像创建张量的资源。
如果有人知道请帮忙?
我正在使用以下代码在浏览器中使用 ONNX.js 运行对象检测
<html>
<head> </head>
<body>
<div onclick="runOD()">
Run Model
</div>
<img src="box.jpg" id="image">
<!-- Load ONNX.js -->
<script src="https://cdn.jsdelivr.net/npm/onnxjs/dist/onnx.min.js"></script>
<!-- Code that consume ONNX.js -->
<script>
async function runOD() {
// Creat the session and load the pre-trained model
const session = new onnx.InferenceSession({
backendHint: 'webgl'
});
await session.loadModel("./model.onnx");
//Function to load image to Canvas and Convert it to Tensor
const preprocessedData = loadImage();
const inputTensor = new onnx.Tensor(preprocessedData, 'float32', [1, 3, 244, 244]);
// Run model with Tensor inputs and get the result.
const outputMap = await session.run([inputTensor]);
const outputData = outputMap.values().next().value.data;
console.log(`model output tensor: ${outputData.data}.`);
}
</script>
</body>
</html>
【问题讨论】:
标签: javascript onnx