Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[custom model] Add support for MODNet #569

Merged
merged 1 commit into from
Feb 6, 2024
Merged

[custom model] Add support for MODNet #569

merged 1 commit into from
Feb 6, 2024

Conversation

xenova
Copy link
Collaborator

@xenova xenova commented Feb 5, 2024

This PR is a step towards supporting custom models (even those not supported by transformers).

In this case, the main additional requirement is to support size_divisbility after resizing. Future PRs will add:

  • A way to remove the unsupported warning.
  • A way to set what type of model it is (encoder-only, encoder-decoder, decoder-only, etc).

Usage:

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

// Load model and processor
const model = await AutoModel.from_pretrained('Xenova/modnet-onnx', { quantized: false });
const processor = await AutoProcessor.from_pretrained('Xenova/modnet-onnx');

// 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);

// Process image
const { pixel_values: input } = await processor(image);

// Predict alpha matte
const { output } = await model({input});

// Save output image
const outputImage = await RawImage.fromTensor(output[0].mul(255).to('uint8')).resize(image.width, image.height);
outputImage.save('output.png');
Input image Visualized mask
image image

For more information, see the original repo and example colab.

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@xenova xenova linked an issue Feb 5, 2024 that may be closed by this pull request
@xenova
Copy link
Collaborator Author

xenova commented Feb 5, 2024

For future reference, the quantized version of the model requires --per_channel False --reduce_range False

@xenova xenova merged commit 2bd8c78 into main Feb 6, 2024
4 checks passed
@cyio
Copy link

cyio commented Feb 7, 2024

online demo: https://huggingface.co/spaces/oakerx/vanilla-js-portrait-matting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How can i use this Model for image matting?
3 participants