JSFiddle - React, Tailwind, and code Playground
HTML
<!DOCTYPE html>
<html>
<head>
<title>ONNX Runtime JavaScript examples: Quick Start - Web (using script tag)</title>
</head>
<body>
<!-- see also advanced usage of importing ONNX Runtime Web: -->
<!-- https://github.com/microsoft/onnxruntime-inference-examples/tree/main/js/importing_onnxruntime-web -->
<!-- import ONNXRuntime Web from CDN -->
<script src="https://cdn.jsdelivr.net/npm/onnxruntime-web@1.18.0/dist/ort.webgpu.min.js"></script>
<script>
// use an async context to call onnxruntime functions.
async function main() {
try {
// create a new session and load the specific model.
const url = 'https://huggingface.co/onnx-community/rtdetr_r18vd_coco_o365/resolve/main/onnx/model.onnx?download=true';
const session = await ort.InferenceSession.create(url, {
executionProviders: ['webgpu'],
});
const dims = [1, 3, 640, 640];
const input = new ort.Tensor(
'float32',
new Float32Array(dims.reduce((acc, x) => acc * x, 1)),
dims,
)
// prepare feeds. use model input names as keys.
const feeds = { pixel_values: input };
// feed inputs and run
const results = await session.run(feeds);
console.log(results)
// read from results
const dataC = results.image_features.data;
document.write(`data of result tensor 'c': ${dataC}`);
} catch (e) {
document.write(`failed to inference ONNX model: ${e}.`);
}
}
main();
</script>
</body>
</html>