ONNX Model Zoo

Xenova/vitmatte-base-distinctions-646

By Xenova · ↓ 9 · ♥ 1 · Updated 2024-10-08

What's in this repo

This page lists the files in Xenova/vitmatte-base-distinctions-646, including model.onnx, model_quantized.onnx.

  • 2 ONNX models (.onnx files)
  • 7 files, including 3 .json, 2 .onnx, 1 .md
  • Last updated 2024-10-08

ONNX files

File Size Download
onnx/model.onnx — Download
onnx/model_quantized.onnx — Download

How to use

These ONNX files can be loaded with ONNX Runtime. To inspect a model graph, upload the file to the Netron viewer. Download links point to the original files on Hugging Face.

README

https://huggingface.co/hustvl/vitmatte-base-distinctions-646 with ONNX weights to be compatible with Transformers.js.

Usage (Transformers.js)

If you haven't already, you can install the Transformers.js JavaScript library from NPM using:

npm i @xenova/transformers

Example: Perform image matting with a VitMatteForImageMatting model.

import { AutoProcessor, VitMatteForImageMatting, RawImage } from '@xenova/transformers';

// Load processor and model
const processor = await AutoProcessor.from_pretrained('Xenova/vitmatte-small-distinctions-646');
const model = await VitMatteForImageMatting.from_pretrained('Xenova/vitmatte-small-distinctions-646', { quantized: false });

// Load image and trimap
const image = await RawImage.fromURL('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/vitmatte_image.png');
const trimap = await RawImage.fromURL('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/vitmatte_trimap.png');

// Prepare image + trimap for the model
const inputs = await processor(image, trimap);

// Predict alpha matte
const { alphas } = await model(inputs);
// Tensor {
//   dims: [ 1, 1, 640, 960 ],
//   type: 'float32',
//   size: 614400,
//   data: Float32Array(614400) [ 0.9894027709960938, 0.9970508813858032, ... ]
// }

You can visualize the alpha matte as follows:

import { Tensor, cat } from '@xenova/transformers';

// Visualize predicted alpha matte
const imageTensor = new Tensor(
  'uint8',
  new Uint8Array(image.data),
  [image.height, image.width, image.channels]
).transpose(2, 0, 1);

// Convert float (0-1) alpha matte to uint8 (0-255)
const alphaChannel = alphas
  .squeeze(0)
  .mul_(255)
  .clamp_(0, 255)
  .round_()
  .to('uint8');

// Concatenate original image with predicted alpha
const imageData = cat([imageTensor, alphaChannel], 0);

// Save output image
const outputImage = RawImage.fromTensor(imageData);
outputImage.save('output.png');

Example inputs:

Image Trimap
vitmatte_image vitmatte_trimap

Example output:

output_unquantized


Note: Having a separate repo for ONNX weights is intended to be a temporary solution until WebML gains more traction. If you would like to make your models web-ready, we recommend converting to ONNX using 🤗 Optimum and structuring your repo like this one (with ONNX weights located in a subfolder named onnx).

Model info

transformers.js
transformers.js onnx vitmatte base_model:hustvl/vitmatte-base-distinctions-646 base_model:quantized:hustvl/vitmatte-base-distinctions-646 region:us
↓ 9 ♥ 1

Created: 2023-12-10

Updated: 2024-10-08

View on Hugging Face

Files (7)

.gitattributes
README.md
config.json
onnx/model.onnx ONNX
onnx/model_quantized.onnx ONNX
preprocessor_config.json
quantize_config.json

Related models