JinHyeong99
1
a36cae8
raw
history blame
1.81 kB
import gradio as gr
from transformers import SegformerFeatureExtractor, SegformerForSemanticSegmentation
from PIL import Image
import numpy as np
import torch
# λͺ¨λΈκ³Ό feature extractor λ‘œλ“œ
model_name = "nvidia/segformer-b0-finetuned-ade-512-512"
model = SegformerForSemanticSegmentation.from_pretrained(model_name)
feature_extractor = SegformerFeatureExtractor.from_pretrained(model_name)
def create_color_map(num_classes):
""" μž„μ˜μ˜ 색상 맀핑 생성 """
np.random.seed(42) # μž¬ν˜„μ„±μ„ μœ„ν•œ μ‹œλ“œ μ„€μ •
return {i: np.random.randint(0, 256, 3) for i in range(num_classes)}
def segment_image(image):
# 이미지 처리
image = image.resize(512,512)
inputs = feature_extractor(images=image, return_tensors="pt")
with torch.no_grad():
outputs = model(**inputs)
# 마슀크 생성
upsampled_logits = torch.nn.functional.interpolate(
outputs.logits, size=image.size[::-1], mode="bilinear", align_corners=False
)
upsampled_predictions = upsampled_logits.argmax(dim=1)
mask = upsampled_predictions.squeeze().numpy()
# 색상 맀핑
color_map = create_color_map(150) # ADE20Kμ—λŠ” μ•½ 150개의 ν΄λž˜μŠ€κ°€ 있음
colored_mask = np.array([color_map[class_id] for class_id in mask.flatten()]).reshape(mask.shape + (3,))
# κ²°κ³Ό λ°˜ν™˜
return Image.fromarray(colored_mask.astype(np.uint8))
# μ˜ˆμ‹œ 이미지 경둜
example_images = ["image1.jpg", "image2.jpg", "image3.jpg"]
# Gradio μΈν„°νŽ˜μ΄μŠ€ μ„€μ •
iface = gr.Interface(
fn=segment_image,
inputs=gr.inputs.Image(type="pil"),
outputs="image",
title="Image Segmentation with SegFormer",
description="Upload an image to segment it using SegFormer model.",
examples=example_images
)
# μΈν„°νŽ˜μ΄μŠ€ μ‹€ν–‰
iface.launch()