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

Add Transformers.js update + demo #6

Closed
wants to merge 2 commits into from
Closed

Conversation

xenova
Copy link

@xenova xenova commented Jan 10, 2024

Hi there! 🤗 This PR adds a bullet point for SlimSAM + transformers.js support (which was added today), allowing these models to be run directly in a browser, using a few lines of JS code. Demo video:

sam-demo.mp4

Demo link + source code: https://huggingface.co/spaces/Xenova/segment-anything-web
Release notes: https://github.com/xenova/transformers.js/releases/tag/2.14.0


Transformers.js uses a very similar API as the python library, as can be seen with the following example code:

Example: Perform mask generation w/ Xenova/slimsam-77-uniform.

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

const model = await SamModel.from_pretrained('Xenova/slimsam-77-uniform');
const processor = await AutoProcessor.from_pretrained('Xenova/slimsam-77-uniform');

const img_url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/corgi.jpg';
const raw_image = await RawImage.read(img_url);
const input_points = [[[340, 250]]] // 2D localization of a window

const inputs = await processor(raw_image, input_points);
const outputs = await model(inputs);

const masks = await processor.post_process_masks(outputs.pred_masks, inputs.original_sizes, inputs.reshaped_input_sizes);
console.log(masks);
// [
//   Tensor {
//     dims: [ 1, 3, 410, 614 ],
//     type: 'bool',
//     data: Uint8Array(755220) [ ... ],
//     size: 755220
//   }
// ]

Visualization of masks:

animation

@czg1225
Copy link
Owner

czg1225 commented Jan 11, 2024

We have add the demo link in our readme, thanks!

@xenova
Copy link
Author

xenova commented Jan 11, 2024

Amazing! Will close this PR then 😇

@xenova xenova closed this Jan 11, 2024
@xenova xenova deleted the patch-1 branch January 11, 2024 14:41
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.

None yet

2 participants