🚀 人臉解析模型
本項目是一個用於人臉解析的語義分割模型,它基於 nvidia/mit-b5 模型,並使用 CelebAMask-HQ 數據集進行微調。該模型可用於將人臉圖像分割為不同的語義區域,如皮膚、鼻子、眼睛等。此外,項目還提供了適用於 Python 和瀏覽器的使用示例,方便開發者集成。

語義分割 模型從 nvidia/mit-b5 微調而來,使用 CelebAMask-HQ 數據集進行人臉解析。更多選項請參閱 Transformers Segformer 文檔。
用於 Web 推理的 ONNX 模型由 Xenova 貢獻。
📚 詳細文檔
標籤信息
完整的標籤列表可以從 config.json 中提取。
編號 |
標籤 |
說明 |
0 |
background |
背景 |
1 |
skin |
皮膚 |
2 |
nose |
鼻子 |
3 |
eye_g |
眼鏡 |
4 |
l_eye |
左眼 |
5 |
r_eye |
右眼 |
6 |
l_brow |
左眉毛 |
7 |
r_brow |
右眉毛 |
8 |
l_ear |
左耳 |
9 |
r_ear |
右耳 |
10 |
mouth |
嘴唇間區域 |
11 |
u_lip |
上嘴唇 |
12 |
l_lip |
下嘴唇 |
13 |
hair |
頭髮 |
14 |
hat |
帽子 |
15 |
ear_r |
耳環 |
16 |
neck_l |
項鍊 |
17 |
neck |
脖子 |
18 |
cloth |
衣服 |
💻 使用示例
基礎用法(Python)
import torch
from torch import nn
from transformers import SegformerImageProcessor, SegformerForSemanticSegmentation
from PIL import Image
import matplotlib.pyplot as plt
import requests
device = (
"cuda"
if torch.cuda.is_available()
else "mps"
if torch.backends.mps.is_available()
else "cpu"
)
image_processor = SegformerImageProcessor.from_pretrained("jonathandinu/face-parsing")
model = SegformerForSemanticSegmentation.from_pretrained("jonathandinu/face-parsing")
model.to(device)
url = "https://images.unsplash.com/photo-1539571696357-5a69c17a67c6"
image = Image.open(requests.get(url, stream=True).raw)
inputs = image_processor(images=image, return_tensors="pt").to(device)
outputs = model(**inputs)
logits = outputs.logits
upsampled_logits = nn.functional.interpolate(logits,
size=image.size[::-1],
mode='bilinear',
align_corners=False)
labels = upsampled_logits.argmax(dim=1)[0]
labels_viz = labels.cpu().numpy()
plt.imshow(labels_viz)
plt.show()
基礎用法(瀏覽器 - Transformers.js)
import {
pipeline,
env,
} from "https://cdn.jsdelivr.net/npm/@xenova/transformers@2.14.0";
env.allowLocalModels = false;
model = await pipeline("image-segmentation", "jonathandinu/face-parsing");
const output = await model(url);
for (const m of output) {
print(`Found ${m.label}`);
m.mask.save(`${m.label}.png`);
}
高級用法(p5.js)
由於 p5.js 使用動畫循環抽象,我們需要注意加載模型和進行預測。
async function preload() {
const { pipeline, env } = await import(
"https://cdn.jsdelivr.net/npm/@xenova/transformers@2.14.0"
);
env.allowLocalModels = false;
model = await pipeline("image-segmentation", "jonathandinu/face-parsing");
print("face-parsing 模型已加載");
}
完整 p5.js 示例
模型信息
⚠️ 侷限性和偏差
雖然計算機視覺模型的能力令人印象深刻,但它們也可能強化或加劇社會偏差。用於微調的 CelebAMask-HQ 數據集雖然很大,但不一定具有完美的多樣性或代表性。此外,這些圖像僅來自名人。