Views
No views yet

ONNX model for web inference contributed by Xenova.
| id | label | note |
|---|---|---|
| 0 | background | |
| 1 | skin | |
| 2 | nose | |
| 3 | eye_g | eyeglasses |
| 4 | l_eye | left eye |
| 5 | r_eye | right eye |
| 6 | l_brow | left eyebrow |
| 7 | r_brow | right eyebrow |
| 8 | l_ear | left ear |
| 9 | r_ear | right ear |
| 10 | mouth | area between lips |
| 11 | u_lip | upper lip |
| 12 | l_lip | lower lip |
| 13 | hair | |
| 14 | hat | |
| 15 | ear_r | earring |
| 16 | neck_l | necklace |
| 17 | neck | |
| 18 | cloth | clothing |
1import torch
2from torch import nn
3from transformers import SegformerImageProcessor, SegformerForSemanticSegmentation
4
5from PIL import Image
6import matplotlib.pyplot as plt
7import requests
8
9# convenience expression for automatically determining device
10device = (
11 "cuda"
12 # Device for NVIDIA or AMD GPUs
13 if torch.cuda.is_available()
14 else "mps"
15 # Device for Apple Silicon (Metal Performance Shaders)
16 if torch.backends.mps.is_available()
17 else "cpu"
18)
19
20# load models
21image_processor = SegformerImageProcessor.from_pretrained("jonathandinu/face-parsing")
22model = SegformerForSemanticSegmentation.from_pretrained("jonathandinu/face-parsing")
23model.to(device)
24
25# expects a PIL.Image or torch.Tensor
26url = "https://images.unsplash.com/photo-1539571696357-5a69c17a67c6"
27image = Image.open(requests.get(url, stream=True).raw)
28
29# run inference on image
30inputs = image_processor(images=image, return_tensors="pt").to(device)
31outputs = model(**inputs)
32logits = outputs.logits # shape (batch_size, num_labels, ~height/4, ~width/4)
33
34# resize output to match input image dimensions
35upsampled_logits = nn.functional.interpolate(logits,
36 size=image.size[::-1], # H x W
37 mode='bilinear',
38 align_corners=False)
39
40# get label masks
41labels = upsampled_logits.argmax(dim=1)[0]
42
43# move to CPU to visualize in matplotlib
44labels_viz = labels.cpu().numpy()
45plt.imshow(labels_viz)
46plt.show()1import {
2 pipeline,
3 env,
4} from "https://cdn.jsdelivr.net/npm/@xenova/transformers@2.14.0";
5
6// important to prevent errors since the model files are likely remote on HF hub
7env.allowLocalModels = false;
8
9// instantiate image segmentation pipeline with pretrained face parsing model
10model = await pipeline("image-segmentation", "jonathandinu/face-parsing");
11
12// async inference since it could take a few seconds
13const output = await model(url);
14
15// each label is a separate mask object
16// [
17// { score: null, label: 'background', mask: transformers.js RawImage { ... }}
18// { score: null, label: 'hair', mask: transformers.js RawImage { ... }}
19// ...
20// ]
21for (const m of output) {
22 print(`Found ${m.label}`);
23 m.mask.save(`${m.label}.png`);
24}1// ...
2
3// asynchronously load transformers.js and instantiate model
4async function preload() {
5 // load transformers.js library with a dynamic import
6 const { pipeline, env } = await import(
7 "https://cdn.jsdelivr.net/npm/@xenova/transformers@2.14.0"
8 );
9
10 // important to prevent errors since the model files are remote on HF hub
11 env.allowLocalModels = false;
12
13 // instantiate image segmentation pipeline with pretrained face parsing model
14 model = await pipeline("image-segmentation", "jonathandinu/face-parsing");
15
16 print("face-parsing model loaded");
17}
18
19// ...