Xenova HF Staff commited on
Commit
fbcd2a3
·
1 Parent(s): b701cfa

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +71 -0
README.md CHANGED
@@ -4,4 +4,75 @@ library_name: transformers.js
4
 
5
  https://huggingface.co/hustvl/vitmatte-base-composition-1k 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/hustvl/vitmatte-base-composition-1k 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 image matting with a `VitMatteForImageMatting` model.
15
+ ```javascript
16
+ import { AutoProcessor, VitMatteForImageMatting, RawImage } from '@xenova/transformers';
17
+
18
+ // Load processor and model
19
+ const processor = await AutoProcessor.from_pretrained('Xenova/vitmatte-base-composition-1k');
20
+ const model = await VitMatteForImageMatting.from_pretrained('Xenova/vitmatte-base-composition-1k', { quantized: false });
21
+
22
+ // Load image and trimap
23
+ const image = await RawImage.fromURL('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/vitmatte_image.png');
24
+ const trimap = await RawImage.fromURL('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/vitmatte_trimap.png');
25
+
26
+ // Prepare image + trimap for the model
27
+ const inputs = await processor(image, trimap);
28
+
29
+ // Predict alpha matte
30
+ const { alphas } = await model(inputs);
31
+ // Tensor {
32
+ // dims: [ 1, 1, 640, 960 ],
33
+ // type: 'float32',
34
+ // size: 614400,
35
+ // data: Float32Array(614400) [ 0.997240424156189, 0.9971460103988647, ... ]
36
+ // }
37
+ ```
38
+
39
+ You can visualize the alpha matte as follows:
40
+ ```javascript
41
+ import { Tensor, cat } from '@xenova/transformers';
42
+
43
+ // Visualize predicted alpha matte
44
+ const imageTensor = new Tensor(
45
+ 'uint8',
46
+ new Uint8Array(image.data),
47
+ [image.height, image.width, image.channels]
48
+ ).transpose(2, 0, 1);
49
+
50
+ // Convert float (0-1) alpha matte to uint8 (0-255)
51
+ const alphaChannel = alphas
52
+ .squeeze(0)
53
+ .mul_(255)
54
+ .clamp_(0, 255)
55
+ .round_()
56
+ .to('uint8');
57
+
58
+ // Concatenate original image with predicted alpha
59
+ const imageData = cat([imageTensor, alphaChannel], 0);
60
+
61
+ // Save output image
62
+ const outputImage = RawImage.fromTensor(imageData);
63
+ outputImage.save('output.png');
64
+ ```
65
+
66
+ Example inputs:
67
+ | Image| Trimap |
68
+ |--------|--------|
69
+ | ![vitmatte_image](https://cdn-uploads.huggingface.co/production/uploads/61b253b7ac5ecaae3d1efe0c/gIwr_finBzqCrzD8Y0Ghm.png) | ![vitmatte_trimap](https://cdn-uploads.huggingface.co/production/uploads/61b253b7ac5ecaae3d1efe0c/ozO5KfIuA3kVZChMelrAZ.png) |
70
+
71
+ Example output:
72
+
73
+ ![output_quantized](https://cdn-uploads.huggingface.co/production/uploads/61b253b7ac5ecaae3d1efe0c/vbvAkOPlaMGFJUJOkF8C3.png)
74
+
75
+ ---
76
+
77
+
78
  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`).