Spaces:
Sleeping
Sleeping
File size: 1,778 Bytes
a3f5bf4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
import gradio as gr
from transformers import SegformerForSemanticSegmentation, SegformerImageProcessor
from PIL import Image
import torch
import numpy as np
# Load processor
processor = SegformerImageProcessor(do_reduce_labels=False)
# Load pretrained SegFormer structure
model = SegformerForSemanticSegmentation.from_pretrained(
"nvidia/segformer-b0-finetuned-ade-512-512",
num_labels=7,
ignore_mismatched_sizes=True
)
# Load trained weights from file in same directory
model.load_state_dict(torch.load("trained_model.pt", map_location=torch.device("cpu")))
model.eval()
# Prediction function
def segment_image(input_image):
inputs = processor(images=input_image, return_tensors="pt")
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
pred_mask = torch.argmax(logits, dim=1)[0].cpu().numpy()
normalized_mask = (pred_mask * (255 // 7)).astype(np.uint8) # 7 classes
output_image = Image.fromarray(normalized_mask)
# Resize mask (3x)
scale_factor = 3
new_size = (output_image.width * scale_factor, output_image.height * scale_factor)
bigger_output = output_image.resize(new_size, resample=Image.NEAREST)
return bigger_output
# Gradio Interface with submit button
demo = gr.Interface(
fn=segment_image,
inputs=gr.Image(type="pil", label="Upload Blood Smear Image"),
outputs=gr.Image(type="pil", label="Predicted Grayscale Mask"),
title="Malaria Blood Smear Segmentation (Custom SegFormer)",
description="Upload a blood smear image. This app uses a custom trained SegFormer model (7 malaria-specific classes) to segment it.",
examples=["1.png", "2.png", "3.png"], # examples in same directory
live=False
)
if __name__ == "__main__":
demo.launch()
|