|
|
import torch
|
|
|
import torch.nn.functional as F
|
|
|
from torchvision import transforms
|
|
|
from PIL import Image
|
|
|
from transformers import AutoModelForImageClassification, AutoImageProcessor
|
|
|
import gradio as gr
|
|
|
|
|
|
|
|
|
model_name = "watersplash/waste-classification"
|
|
|
model = AutoModelForImageClassification.from_pretrained(model_name)
|
|
|
image_processor = AutoImageProcessor.from_pretrained(model_name)
|
|
|
|
|
|
|
|
|
def preprocess_image(image):
|
|
|
transform = transforms.Compose([
|
|
|
transforms.Resize((224, 224)),
|
|
|
transforms.ToTensor(),
|
|
|
transforms.Normalize(mean=image_processor.image_mean, std=image_processor.image_std)
|
|
|
])
|
|
|
return transform(image).unsqueeze(0)
|
|
|
|
|
|
|
|
|
def predict_waste(image):
|
|
|
image = Image.fromarray(image)
|
|
|
input_tensor = preprocess_image(image)
|
|
|
|
|
|
|
|
|
with torch.no_grad():
|
|
|
outputs = model(input_tensor)
|
|
|
|
|
|
|
|
|
probabilities = torch.sigmoid(outputs.logits)[0]
|
|
|
|
|
|
|
|
|
threshold = 0.5
|
|
|
predicted_labels = [label for idx, label in model.config.id2label.items() if probabilities[idx] >= threshold]
|
|
|
confidence_scores = [f"{probabilities[idx] * 100:.2f}%" for idx in range(len(probabilities)) if probabilities[idx] >= threshold]
|
|
|
|
|
|
if predicted_labels:
|
|
|
result = "\n".join([f"{label}: {score}" for label, score in zip(predicted_labels, confidence_scores)])
|
|
|
else:
|
|
|
result = "No clear classification (confidence below threshold)"
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
interface = gr.Interface(
|
|
|
fn=predict_waste,
|
|
|
inputs=gr.Image(type="numpy"),
|
|
|
outputs="text",
|
|
|
title="Multi-Label Waste Sorting App",
|
|
|
description="Upload an image of waste. The model will classify it into multiple waste categories with confidence scores."
|
|
|
)
|
|
|
|
|
|
|
|
|
interface.launch()
|
|
|
|