Xenova HF staff commited on
Commit
1eb7b19
1 Parent(s): a63aecd

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +73 -0
README.md CHANGED
@@ -4,4 +4,77 @@ library_name: transformers.js
4
 
5
  https://huggingface.co/facebook/sam-vit-large with ONNX weights to be compatible with Transformers.js.
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  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](https://huggingface.co/docs/optimum/index) and structuring your repo like this one (with ONNX weights located in a subfolder named `onnx`).
 
4
 
5
  https://huggingface.co/facebook/sam-vit-large with ONNX weights to be compatible with Transformers.js.
6
 
7
+ ## Usage (Transformers.js)
8
+
9
+ If you haven't already, you can install the [Transformers.js](https://huggingface.co/docs/transformers.js) JavaScript library from [NPM](https://www.npmjs.com/package/@xenova/transformers) using:
10
+ ```bash
11
+ npm i @xenova/transformers
12
+ ```
13
+
14
+ **Example:** Perform mask generation with `Xenova/sam-vit-large`.
15
+
16
+ ```js
17
+ import { SamModel, AutoProcessor, RawImage } from '@xenova/transformers';
18
+
19
+ // Load model and processor
20
+ const model = await SamModel.from_pretrained('Xenova/sam-vit-large');
21
+ const processor = await AutoProcessor.from_pretrained('Xenova/sam-vit-large');
22
+
23
+ // Prepare image and input points
24
+ const img_url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/corgi.jpg';
25
+ const raw_image = await RawImage.read(img_url);
26
+ const input_points = [[[340, 250]]]; // 2D localization of a window
27
+
28
+ // Process inputs and perform mask generation
29
+ const inputs = await processor(raw_image, input_points);
30
+ const outputs = await model(inputs);
31
+
32
+ // Post-process masks
33
+ const masks = await processor.post_process_masks(outputs.pred_masks, inputs.original_sizes, inputs.reshaped_input_sizes);
34
+ console.log(masks);
35
+ // [
36
+ // Tensor {
37
+ // dims: [ 1, 3, 410, 614 ],
38
+ // type: 'bool',
39
+ // data: Uint8Array(755220) [ ... ],
40
+ // size: 755220
41
+ // }
42
+ // ]
43
+ const scores = outputs.iou_scores;
44
+ console.log(scores);
45
+ // Tensor {
46
+ // dims: [ 1, 1, 3 ],
47
+ // type: 'float32',
48
+ // data: Float32Array(3) [
49
+ // 1.0122944116592407,
50
+ // 0.9184409976005554,
51
+ // 0.9796935319900513
52
+ // ],
53
+ // size: 3
54
+ // }
55
+ ```
56
+
57
+ You can then visualize the generated mask with:
58
+
59
+ ```js
60
+ const image = RawImage.fromTensor(masks[0][0].mul(255));
61
+ image.save('mask.png');
62
+ ```
63
+
64
+ ![image/png](https://cdn-uploads.huggingface.co/production/uploads/61b253b7ac5ecaae3d1efe0c/nE4G3JQM0p6FOdXPHIkDd.png)
65
+
66
+ Next, select the channel with the highest IoU score, which in this case is the first (red) channel. Intersecting this with the original image gives us an isolated version of the subject:
67
+
68
+
69
+ ![image/gif](https://cdn-uploads.huggingface.co/production/uploads/61b253b7ac5ecaae3d1efe0c/69WqecJ7s2uAq01wkiEU4.gif)
70
+
71
+ ## Demo
72
+
73
+ We've also got an online demo, which you can try out [here](https://huggingface.co/spaces/Xenova/segment-anything-web).
74
+
75
+
76
+ <video controls autoplay src="https://cdn-uploads.huggingface.co/production/uploads/61b253b7ac5ecaae3d1efe0c/Y0wAOw6hz9rWpwiuMoz2A.mp4"></video>
77
+
78
+ ---
79
+
80
  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](https://huggingface.co/docs/optimum/index) and structuring your repo like this one (with ONNX weights located in a subfolder named `onnx`).