File size: 1,645 Bytes
05f27b7
 
 
 
5703104
3881eaa
05f27b7
3881eaa
05f27b7
 
3881eaa
 
05f27b7
 
3881eaa
05f27b7
 
3881eaa
05f27b7
 
 
 
 
 
 
3881eaa
 
05f27b7
3881eaa
 
 
05f27b7
 
3881eaa
05f27b7
3881eaa
05f27b7
3881eaa
05f27b7
 
 
 
 
3881eaa
5703104
 
05f27b7
 
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
import torch
import torch.nn as nn
from torchvision import transforms
from PIL import Image
import gradio as gr
from utils import MMIM, load_all_models, predict_image

# βœ… All 25 class labels
class_names = [
    "Chinee apple", "Lantana", "Negative", "Parkinsonia", "Parthenium", "Prickly acacia",
    "Rubber vine", "Siam weed", "Snake weed",      # 1–9 (model1)
    "Broadleaf", "Grass", "Soil", "Soybean",       # 10–13 (model3)
    "Black grass", "Charlock", "Cleavers", "Common Chickweed", "Common Wheat", "Fat Hen",
    "Loose Silky-bent", "Maize", "Scentless Mayweed", "Shepherds purse",
    "Small-flowered Cranesbill", "Sugar beet"      # 14–25 (model2)
]

# βœ… Image transform for Swin
transform = transforms.Compose([
    transforms.Resize((224, 224)),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.5, 0.5, 0.5],
                         std=[0.5, 0.5, 0.5])
])

# βœ… Load models
model1, model2, model3 = load_all_models()

# βœ… Gradio prediction function
def predict(image):
    label, confidence = predict_image(image, model1, model2, model3, transform, class_names)

    if confidence < 0.5:
        return "⚠️ Prediction uncertain – possibly unknown or low confidence"

    return f"🧠 Predicted: **{label}** (Confidence: {confidence:.2f})"

# βœ… Gradio interface
app = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="pil", label="Upload a weed image"),
    outputs=gr.Markdown(),
    title="🌿 Weed Classifier (25 Classes - MMIM)",
    description="Upload an image to classify it into one of 25 weed categories using 3 Swin-MMIM models."
)

if __name__ == "__main__":
    app.launch()