|
import torch |
|
from torch import nn |
|
from transformers import SegformerImageProcessor, SegformerForSemanticSegmentation |
|
from PIL import Image |
|
import matplotlib.pyplot as plt |
|
import requests |
|
import gradio as gr |
|
import numpy as np |
|
|
|
|
|
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) |
|
|
|
|
|
def infer(image: Image.Image) -> np.ndarray: |
|
|
|
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() |
|
return labels_viz |
|
|
|
|
|
iface = gr.Interface( |
|
fn=infer, |
|
inputs=gr.inputs.Image(type="pil"), |
|
outputs=gr.outputs.Image(type="numpy"), |
|
live=True, |
|
title="Face Parsing with Segformer", |
|
description="Upload an image to perform face parsing using the Segformer model for semantic segmentation." |
|
) |
|
|
|
|
|
iface.launch() |
|
|