Spaces:
Sleeping
Sleeping
File size: 4,668 Bytes
f244144 0643611 f244144 0643611 f244144 c52c937 0643611 c52c937 0643611 c52c937 0643611 c52c937 0643611 f244144 c52c937 126a28e c52c937 f244144 0643611 f244144 0643611 f244144 0643611 f244144 0643611 f244144 0643611 c52c937 0643611 f244144 c52c937 f129297 f244144 0643611 f244144 c52c937 f244144 c52c937 f244144 c52c937 0643611 c52c937 |
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
import gradio as gr
import torch
import torch.nn as nn
from PIL import Image
import torchvision.transforms as transforms
# ResNet9 Model Tanımı
def ConvBlock(in_channels, out_channels, pool=False):
layers = [
nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True)
]
if pool:
layers.append(nn.MaxPool2d(4))
return nn.Sequential(*layers)
class ResNet9(nn.Module):
def __init__(self, in_channels, num_diseases):
super().__init__()
self.conv1 = ConvBlock(in_channels, 64)
self.conv2 = ConvBlock(64, 128, pool=True)
self.res1 = nn.Sequential(ConvBlock(128, 128), ConvBlock(128, 128))
self.conv3 = ConvBlock(128, 256, pool=True)
self.conv4 = ConvBlock(256, 512, pool=True)
self.res2 = nn.Sequential(ConvBlock(512, 512), ConvBlock(512, 512))
self.classifier = nn.Sequential(
nn.MaxPool2d(4),
nn.Flatten(),
nn.Linear(512, num_diseases)
)
def forward(self, xb):
out = self.conv1(xb)
out = self.conv2(out)
out = self.res1(out) + out
out = self.conv3(out)
out = self.conv4(out)
out = self.res2(out) + out
out = self.classifier(out)
return out
# Hastalık isimleri - KEND─░N─░ZE GÖRE DE─×─░┼×T─░R─░N
CLASS_NAMES = [
'Apple___Apple_scab',
'Apple___Black_rot',
'Apple___Cedar_apple_rust',
'Apple___healthy',
'Blueberry___healthy',
'Cherry_(including_sour)___Powdery_mildew',
'Cherry_(including_sour)___healthy',
'Corn_(maize)___Cercospora_leaf_spot Gray_leaf_spot',
'Corn_(maize)___Common_rust_',
'Corn_(maize)___Northern_Leaf_Blight',
'Corn_(maize)___healthy',
'Grape___Black_rot',
'Grape___Esca_(Black_Measles)',
'Grape___Leaf_blight_(Isariopsis_Leaf_Spot)',
'Grape___healthy',
'Orange___Haunglongbing_(Citrus_greening)',
'Peach___Bacterial_spot',
'Peach___healthy',
'Pepper,_bell___Bacterial_spot',
'Pepper,_bell___healthy',
'Potato___Early_blight',
'Potato___Late_blight',
'Potato___healthy',
'Raspberry___healthy',
'Soybean___healthy',
'Squash___Powdery_mildew',
'Strawberry___Leaf_scorch',
'Strawberry___healthy',
'Tomato___Bacterial_spot',
'Tomato___Early_blight',
'Tomato___Late_blight',
'Tomato___Leaf_Mold',
'Tomato___Septoria_leaf_spot',
'Tomato___Spider_mites Two-spotted_spider_mite',
'Tomato___Target_Spot',
'Tomato___Tomato_Yellow_Leaf_Curl_Virus',
'Tomato___Tomato_mosaic_virus',
'Tomato___healthy'
]
# Device ayarı
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Model yükleme
model = ResNet9(in_channels=3, num_diseases=len(CLASS_NAMES))
model.load_state_dict(torch.load('plant-disease-model.pth',
map_location=device,
weights_only=False))
model.to(device)
model.eval()
# Transform
transform = transforms.Compose([
transforms.Resize((256, 256)),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
# Tahmin fonksiyonu
def predict(image):
"""Resimden hastalık tahmini yapar"""
# Resmi hazırla
img_tensor = transform(image).unsqueeze(0).to(device)
# Tahmin yap
with torch.no_grad():
output = model(img_tensor)
probabilities = torch.nn.functional.softmax(output[0], dim=0)
# En yüksek 5 tahmini al
top5_prob, top5_idx = torch.topk(probabilities, 5)
# Sonuçları hazırla
results = {}
for i in range(5):
class_name = CLASS_NAMES[top5_idx[i].item()]
probability = top5_prob[i].item()
# İsmi daha okunabilir yap
display_name = class_name.replace('___', ' - ').replace('_', ' ')
results[display_name] = float(probability)
return results
# Gradio arayüzü
demo = gr.Interface(
fn=predict,
inputs=gr.Image(type="pil", label="🌿 Bitki Resmi Yükleyin"),
outputs=gr.Label(num_top_classes=5, label="🔍 Tahmin Sonuçları"),
title="🌱 Plant Disease Detection",
description="Bitki yapraklarının resmini yükleyin, hastalık tespiti yapılsın! Model 38 farklı bitki hastalığını tespit edebilir.",
examples=[
# Örnek resimleri buraya ekleyebilirsiniz
],
theme="soft",
css="""
.gradio-container {
font-family: 'IBM Plex Sans', sans-serif;
}
"""
)
if __name__ == "__main__":
demo.launch() |