Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
library_name: transformers.js
|
| 3 |
+
tags:
|
| 4 |
+
- image-matting
|
| 5 |
+
- portrait-matting
|
| 6 |
+
---
|
| 7 |
+
|
| 8 |
+
https://github.com/ZHKKKe/MODNet with ONNX weights to be compatible with Transformers.js.
|
| 9 |
+
|
| 10 |
+
## Usage (Transformers.js)
|
| 11 |
+
|
| 12 |
+
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:
|
| 13 |
+
```bash
|
| 14 |
+
npm i @xenova/transformers
|
| 15 |
+
```
|
| 16 |
+
|
| 17 |
+
You can then use the model for portrait matting, as follows:
|
| 18 |
+
|
| 19 |
+
```js
|
| 20 |
+
import { AutoProcessor, RawImage, AutoModel } from '@xenova/transformers';
|
| 21 |
+
|
| 22 |
+
// Load model and processor
|
| 23 |
+
const model = await AutoModel.from_pretrained('Xenova/modnet-onnx', { quantized: false });
|
| 24 |
+
const processor = await AutoProcessor.from_pretrained('Xenova/modnet-onnx');
|
| 25 |
+
|
| 26 |
+
// Load image from URL
|
| 27 |
+
const url = 'https://images.pexels.com/photos/5965592/pexels-photo-5965592.jpeg?auto=compress&cs=tinysrgb&w=1024';
|
| 28 |
+
const image = await RawImage.fromURL(url);
|
| 29 |
+
|
| 30 |
+
// Pre-process image
|
| 31 |
+
const { pixel_values } = await processor(image);
|
| 32 |
+
|
| 33 |
+
// Predict alpha matte
|
| 34 |
+
const { output } = await model({ input: pixel_values });
|
| 35 |
+
|
| 36 |
+
// Save output mask
|
| 37 |
+
const mask = await RawImage.fromTensor(output[0].mul(255).to('uint8')).resize(image.width, image.height);
|
| 38 |
+
mask.save('mask.png');
|
| 39 |
+
```
|
| 40 |
+
|
| 41 |
+
| Input image | Output mask |
|
| 42 |
+
|--------|--------|
|
| 43 |
+
|  |  |
|
| 44 |
+
|
| 45 |
+
---
|
| 46 |
+
|
| 47 |
+
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`).
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
For more information, see the original [repo](https://github.com/ZHKKKe/MODNet) and example [colab](https://colab.research.google.com/drive/1P3cWtg8fnmu9karZHYDAtmm1vj1rgA-f?usp=sharing).
|