Spaces:
Sleeping
Sleeping
fix: bug fixed
Browse files- app.py +59 -38
- model.py +0 -13
- requirements.txt +2 -2
app.py
CHANGED
|
@@ -1,13 +1,22 @@
|
|
| 1 |
-
|
| 2 |
import torch
|
| 3 |
-
|
| 4 |
from PIL import Image
|
| 5 |
-
import
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
NUM_CLASSES = 38
|
| 9 |
-
MODEL_PATH = 'plant-disease-model-complete.pth'
|
| 10 |
-
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 11 |
|
| 12 |
CLASS_NAMES = [
|
| 13 |
'Tomato___Late_blight',
|
|
@@ -50,48 +59,60 @@ CLASS_NAMES = [
|
|
| 50 |
'Corn_(maize)___healthy'
|
| 51 |
]
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
transform = transforms.Compose([
|
| 54 |
transforms.Resize((256, 256)),
|
| 55 |
transforms.ToTensor(),
|
| 56 |
-
transforms.Normalize(
|
| 57 |
])
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
loaded_model.to(DEVICE)
|
| 67 |
-
loaded_model.eval()
|
| 68 |
-
return loaded_model
|
| 69 |
-
|
| 70 |
-
model = load_model()
|
| 71 |
-
|
| 72 |
-
def predict_image(img: Image.Image):
|
| 73 |
-
tensor = transform(img).unsqueeze(0) # (1, C, H, W) şekline getir
|
| 74 |
|
| 75 |
-
#
|
| 76 |
-
tensor = tensor.to(DEVICE)
|
| 77 |
with torch.no_grad():
|
| 78 |
-
|
| 79 |
-
probabilities = torch.nn.functional.softmax(
|
| 80 |
|
| 81 |
-
# En yüksek
|
| 82 |
-
|
| 83 |
|
| 84 |
-
#
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
|
|
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
)
|
| 95 |
|
| 96 |
if __name__ == "__main__":
|
| 97 |
-
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
from PIL import Image
|
| 5 |
+
import torchvision.transforms as transforms
|
| 6 |
+
|
| 7 |
+
# Model tanımı (ResNet yapınızı buraya ekleyin)
|
| 8 |
+
# Eğer torchvision ResNet kullanıyorsanız:
|
| 9 |
+
from torchvision import models
|
| 10 |
+
|
| 11 |
+
class PlantDiseaseModel(nn.Module):
|
| 12 |
+
def __init__(self, num_classes):
|
| 13 |
+
super(PlantDiseaseModel, self).__init__()
|
| 14 |
+
self.model = models.resnet50(pretrained=False)
|
| 15 |
+
self.model.fc = nn.Linear(self.model.fc.in_features, num_classes)
|
| 16 |
+
|
| 17 |
+
def forward(self, x):
|
| 18 |
+
return self.model(x)
|
| 19 |
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
CLASS_NAMES = [
|
| 22 |
'Tomato___Late_blight',
|
|
|
|
| 59 |
'Corn_(maize)___healthy'
|
| 60 |
]
|
| 61 |
|
| 62 |
+
|
| 63 |
+
# Device ayarı
|
| 64 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 65 |
+
|
| 66 |
+
# Model yükleme
|
| 67 |
+
model = PlantDiseaseModel(num_classes=len(CLASS_NAMES))
|
| 68 |
+
model.load_state_dict(torch.load('plant-disease-model-complete.pth', map_location=device))
|
| 69 |
+
model.to(device)
|
| 70 |
+
model.eval()
|
| 71 |
+
|
| 72 |
+
# Transform
|
| 73 |
transform = transforms.Compose([
|
| 74 |
transforms.Resize((256, 256)),
|
| 75 |
transforms.ToTensor(),
|
| 76 |
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
| 77 |
])
|
| 78 |
|
| 79 |
+
# Tahmin fonksiyonu
|
| 80 |
+
def predict(image):
|
| 81 |
+
"""Resimden hastalık tahmini yapar"""
|
| 82 |
+
|
| 83 |
+
# Resmi hazırla
|
| 84 |
+
img_tensor = transform(image).unsqueeze(0).to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
# Tahmin yap
|
|
|
|
| 87 |
with torch.no_grad():
|
| 88 |
+
output = model(img_tensor)
|
| 89 |
+
probabilities = torch.nn.functional.softmax(output[0], dim=0)
|
| 90 |
|
| 91 |
+
# En yüksek 3 tahmini al
|
| 92 |
+
top3_prob, top3_idx = torch.topk(probabilities, 3)
|
| 93 |
|
| 94 |
+
# Sonuçları hazırla
|
| 95 |
+
results = {}
|
| 96 |
+
for i in range(3):
|
| 97 |
+
class_name = CLASS_NAMES[top3_idx[i].item()]
|
| 98 |
+
probability = top3_prob[i].item()
|
| 99 |
+
results[class_name] = float(probability)
|
| 100 |
|
| 101 |
+
return results
|
| 102 |
|
| 103 |
+
# Gradio arayüzü
|
| 104 |
+
demo = gr.Interface(
|
| 105 |
+
fn=predict,
|
| 106 |
+
inputs=gr.Image(type="pil", label="Bitki Resmi Yükleyin"),
|
| 107 |
+
outputs=gr.Label(num_top_classes=3, label="Tahmin Sonuçları"),
|
| 108 |
+
title="🌱 Plant Disease Detection",
|
| 109 |
+
description="Bitki yapraklarının resmini yükleyin, hastalık tespiti yapılsın!",
|
| 110 |
+
examples=[
|
| 111 |
+
# Örnek resimleri buraya ekleyebilirsiniz
|
| 112 |
+
],
|
| 113 |
+
theme="soft"
|
| 114 |
)
|
| 115 |
|
| 116 |
if __name__ == "__main__":
|
| 117 |
+
demo.launch()
|
| 118 |
+
|
model.py
DELETED
|
@@ -1,13 +0,0 @@
|
|
| 1 |
-
# model.py (Örnek - Kendi mimarinize göre düzenleyin)
|
| 2 |
-
import torch.nn as nn
|
| 3 |
-
from torchvision import models
|
| 4 |
-
|
| 5 |
-
def get_model(num_classes):
|
| 6 |
-
# ResNet modelini yükle
|
| 7 |
-
model = models.resnet18(pretrained=False) # Eğer eğitimde pretrained kullandıysanız True yapın
|
| 8 |
-
|
| 9 |
-
# Son katmanı değiştir (Sınıf sayınıza göre)
|
| 10 |
-
num_ftrs = model.fc.in_features
|
| 11 |
-
model.fc = nn.Linear(num_ftrs, num_classes)
|
| 12 |
-
|
| 13 |
-
return model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
gradio
|
| 2 |
torch
|
| 3 |
torchvision
|
| 4 |
-
|
| 5 |
-
|
|
|
|
| 1 |
gradio
|
| 2 |
torch
|
| 3 |
torchvision
|
| 4 |
+
Pillow
|
| 5 |
+
numpy
|