Run this model with HF Jobs
Run Xenova/sam-vit-base on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs to fine-tune https://huggingface.co/Xenova/sam-vit-base. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/Xenova/sam-vit-base on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Instructions to use Xenova/sam-vit-base with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers.js
How to use Xenova/sam-vit-base with Transformers.js:
// npm i @huggingface/transformers import { pipeline } from '@huggingface/transformers'; // Allocate pipeline const pipe = await pipeline('mask-generation', 'Xenova/sam-vit-base');
https://huggingface.co/facebook/sam-vit-base 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 @huggingface/transformers
Example: Perform mask generation with Xenova/sam-vit-base.
import { SamModel, AutoProcessor, RawImage } from "@huggingface/transformers";
// Load model and processor
const model = await SamModel.from_pretrained("Xenova/sam-vit-base");
const processor = await AutoProcessor.from_pretrained("Xenova/sam-vit-base");
// Prepare image and input points
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]]];
// Process inputs and perform mask generation
const inputs = await processor(raw_image, { input_points });
const outputs = await model(inputs);
// Post-process masks
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
// }
// ]
const scores = outputs.iou_scores;
console.log(scores);
// Tensor {
// dims: [ 1, 1, 3 ],
// type: 'float32',
// data: Float32Array(3) [
// 0.9466127157211304,
// 0.9890615344047546,
// 0.8316894769668579
// ],
// size: 3
// }
You can then visualize the generated mask with:
const image = RawImage.fromTensor(masks[0][0].mul(255));
image.save('mask.png');
Next, select the channel with the highest IoU score, which in this case is the second (green) channel. Intersecting this with the original image gives us an isolated version of the subject:
Demo
We've also got an online demo, which you can try out here.
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).
- Downloads last month
- 2,912
Model tree for Xenova/sam-vit-base
Base model
facebook/sam-vit-base
