BiRefNet: Error when use .onnx on web

Hi, I tried to test .onnx on web (CPU) using onnx-community/BiRefNet_T. Here is index.html:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>Background Removal</title>
</head>
<body>
<script type="module">
  import { AutoModel, AutoProcessor, RawImage } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers';

  // Load model and processor
  const model_id = 'onnx-community/BiRefNet_T';
  const model = await AutoModel.from_pretrained(model_id, { dtype: 'fp32' });
  const processor = await AutoProcessor.from_pretrained(model_id);

  // Load image from URL
  const url = 'https://images.pexels.com/photos/5965592/pexels-photo-5965592.jpeg?auto=compress&cs=tinysrgb&w=1024';
  const image = await RawImage.fromURL(url);

  // Pre-process image
  const { pixel_values } = await processor(image);

  // Predict alpha matte
  const { output_image } = await model({ input_image: pixel_values });
  console.log(output_image);
</script>
</body>
</html>

Check Chrome dev tools > Application tab > Cache storage, the .onnx file is loaded & cached without problem: Screenshot 2024-08-28 at 20 07 41

But I got error at predict code line:

const { output_image } = await model({ input_image: pixel_values })

hiLBrmNL_G8aENlwRHGGQ

I try the other input images, got the same issue. Please help to check it. Thank you.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 18 (9 by maintainers)

Most upvoted comments

Hi, @xenova. Could you help me with another thing? I renamed the previous BiRefNet_T to BiRefNet_lite as https://huggingface.co/ZhengPeng7/BiRefNet_lite for users understand the meaning more easily. Could you also rename it in the transformers.js and https://huggingface.co/onnx-community/BiRefNet_T?

Many thanks!!

Updated 👍 the new link is now: https://huggingface.co/onnx-community/BiRefNet_lite

Hi @ZhengPeng7, I already tried birefnet-onnx-sample, .onnx on node works without any problem. I will keep follow up the issue on web on microsoft/onnxruntime#21968. Thank you.

FYI - the ORT team is looking into this: https://github.com/microsoft/onnxruntime/issues/21968

Glad to see that! And thanks to @adha9990’s nice work on that.

Hi, @tidus2102, if you still face some problems there, you can also try this great project to run BiRefNet-ONNX with JavaScript and node.js, made by @adha9990.

Hi there 👋 The model (with the example code) runs fine in Node.js, so if I had to guess, it is an out of memory issue due to browser resource constraints. I think there are two fixes we can consider:

  1. Export at a lower input shape (instead of 1024x1024)
  2. Export with dynamic shapes