Spaces:
Runtime error
Runtime error
Upload 4 files
Browse files- model.py +90 -0
- models/potato_model_statedict__f.pth +3 -0
- models/tomato_model_statedict__f.pth +3 -0
- predict.py +65 -0
model.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn.functional as F
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
|
| 5 |
+
class Base(nn.Module):
|
| 6 |
+
def training_step(self, batch):
|
| 7 |
+
images, labels = batch
|
| 8 |
+
out = self(images) # Generate predictions
|
| 9 |
+
loss = F.cross_entropy(out, labels) # Calculate loss
|
| 10 |
+
return loss
|
| 11 |
+
|
| 12 |
+
def validation_step(self, batch):
|
| 13 |
+
images, labels = batch
|
| 14 |
+
out = self(images) # Generate predictions
|
| 15 |
+
loss = F.cross_entropy(out, labels) # Calculate loss
|
| 16 |
+
acc = accuracy(out, labels) # Calculate accuracy
|
| 17 |
+
return {'val_loss': loss.detach(), 'val_acc': acc}
|
| 18 |
+
|
| 19 |
+
def validation_epoch_end(self, outputs):
|
| 20 |
+
batch_losses = [x['val_loss'] for x in outputs]
|
| 21 |
+
epoch_loss = torch.stack(batch_losses).mean() # Combine losses
|
| 22 |
+
batch_accs = [x['val_acc'] for x in outputs]
|
| 23 |
+
epoch_acc = torch.stack(batch_accs).mean() # Combine accuracies
|
| 24 |
+
return {'val_loss': epoch_loss.item(), 'val_acc': epoch_acc.item()}
|
| 25 |
+
|
| 26 |
+
def epoch_end(self, epoch, result):
|
| 27 |
+
print("Epoch [{}], train_loss: {:.4f}, val_loss: {:.4f}, val_acc: {:.4f}".format(
|
| 28 |
+
epoch, result['train_loss'], result['val_loss'], result['val_acc']))
|
| 29 |
+
|
| 30 |
+
# print(f'Epoch: {epoch} | Train_loss: {result['train_loss']} | Val_loss:{result['val_loss']} | Val_acc: {result['val_acc']}')
|
| 31 |
+
|
| 32 |
+
def accuracy(outputs, labels):
|
| 33 |
+
_, preds = torch.max(outputs, dim=1)
|
| 34 |
+
return torch.tensor(torch.sum(preds == labels).item() / len(preds))
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
class PotatoDiseaseDetectionModel(Base):
|
| 38 |
+
def __init__(self, in_channels=3, num_classes=3):
|
| 39 |
+
super(PotatoDiseaseDetectionModel, self).__init__()
|
| 40 |
+
|
| 41 |
+
# Define the network layers
|
| 42 |
+
self.network = nn.Sequential(
|
| 43 |
+
nn.Conv2d(in_channels=in_channels, out_channels=64, kernel_size=3, stride=1, padding=1),
|
| 44 |
+
nn.BatchNorm2d(64),
|
| 45 |
+
nn.ReLU(inplace=True),
|
| 46 |
+
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=1, padding=1),
|
| 47 |
+
nn.BatchNorm2d(64),
|
| 48 |
+
nn.ReLU(inplace=True),
|
| 49 |
+
nn.MaxPool2d(kernel_size=2, stride=2),
|
| 50 |
+
|
| 51 |
+
nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, stride=1, padding=1),
|
| 52 |
+
nn.BatchNorm2d(128),
|
| 53 |
+
nn.ReLU(inplace=True),
|
| 54 |
+
nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, stride=1, padding=1),
|
| 55 |
+
nn.BatchNorm2d(128),
|
| 56 |
+
nn.ReLU(inplace=True),
|
| 57 |
+
nn.MaxPool2d(kernel_size=2, stride=2),
|
| 58 |
+
|
| 59 |
+
nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, stride=1, padding=1),
|
| 60 |
+
nn.BatchNorm2d(256),
|
| 61 |
+
nn.ReLU(inplace=True),
|
| 62 |
+
nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, stride=1, padding=1),
|
| 63 |
+
nn.BatchNorm2d(256),
|
| 64 |
+
nn.ReLU(inplace=True),
|
| 65 |
+
nn.MaxPool2d(kernel_size=2, stride=2),
|
| 66 |
+
|
| 67 |
+
nn.Flatten()
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
# Define the classifier layers
|
| 71 |
+
self.classifier = nn.Sequential(
|
| 72 |
+
nn.Linear(in_features=256*28*28, out_features=128),
|
| 73 |
+
nn.BatchNorm1d(128),
|
| 74 |
+
nn.ReLU(inplace=True),
|
| 75 |
+
nn.Dropout(0.5),
|
| 76 |
+
nn.Linear(in_features=128, out_features=num_classes)
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
def forward(self, x):
|
| 80 |
+
# Pass the input through the network
|
| 81 |
+
x = self.network(x)
|
| 82 |
+
|
| 83 |
+
# Pass the output through the classifier
|
| 84 |
+
x = self.classifier(x)
|
| 85 |
+
|
| 86 |
+
return x
|
| 87 |
+
|
| 88 |
+
# Create the model with desired number of classes
|
| 89 |
+
potato_model = PotatoDiseaseDetectionModel(num_classes=3)
|
| 90 |
+
tomato_model = PotatoDiseaseDetectionModel(num_classes=3)
|
models/potato_model_statedict__f.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5245846b73c7b93ac3df45df2e0d033e7bdbf2c042d0a0cd5fc4bc27966441ce
|
| 3 |
+
size 107378331
|
models/tomato_model_statedict__f.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7c3cfea307a1bc339aaebfd4761e5a8dd2dfb8f03d113db76c9079552773b258
|
| 3 |
+
size 107378331
|
predict.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from torchvision import transforms as tt
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import torch
|
| 4 |
+
from torchvision import transforms as tt
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import cv2
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def predict_potato(image_path, model):
|
| 10 |
+
|
| 11 |
+
# Define the pre-processing transform
|
| 12 |
+
transforms = tt.Compose([
|
| 13 |
+
tt.Resize((224, 224)),
|
| 14 |
+
tt.ToTensor()
|
| 15 |
+
])
|
| 16 |
+
image = Image.open(image_path).convert("RGB")
|
| 17 |
+
# Pre-process the image
|
| 18 |
+
image_tensor = transforms(image).unsqueeze(0)
|
| 19 |
+
# Set the model to evaluation mode
|
| 20 |
+
model.eval()
|
| 21 |
+
|
| 22 |
+
# Make a prediction
|
| 23 |
+
with torch.no_grad():
|
| 24 |
+
output = model(image_tensor)
|
| 25 |
+
|
| 26 |
+
# Convert the output to probabilities using softmax
|
| 27 |
+
probabilities = torch.nn.functional.softmax(output[0], dim=0)
|
| 28 |
+
# Get the predicted class
|
| 29 |
+
predicted_class = torch.argmax(probabilities).item()
|
| 30 |
+
# Get the probability for the predicted class
|
| 31 |
+
predicted_probability = probabilities[predicted_class].item()
|
| 32 |
+
# Define class labels
|
| 33 |
+
class_labels = ['Potato Early Blight', 'Potato Late Blight', 'Potato Healthy']
|
| 34 |
+
|
| 35 |
+
return class_labels[predicted_class], predicted_probability, image
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def predict_tomato(image_file, model):
|
| 39 |
+
# Define the pre-processing transform
|
| 40 |
+
transforms = tt.Compose([
|
| 41 |
+
tt.Resize((224, 224)),
|
| 42 |
+
tt.ToTensor()
|
| 43 |
+
])
|
| 44 |
+
|
| 45 |
+
# Load and preprocess the image
|
| 46 |
+
image = Image.open(image_file).convert("RGB")
|
| 47 |
+
image_tensor = transforms(image).unsqueeze(0)
|
| 48 |
+
|
| 49 |
+
# Set the model to evaluation mode
|
| 50 |
+
model.eval()
|
| 51 |
+
|
| 52 |
+
# Make a prediction
|
| 53 |
+
with torch.no_grad():
|
| 54 |
+
output = model(image_tensor)
|
| 55 |
+
|
| 56 |
+
# Convert the output to probabilities using softmax
|
| 57 |
+
probabilities = torch.nn.functional.softmax(output[0], dim=0)
|
| 58 |
+
# Get the predicted class
|
| 59 |
+
predicted_class = torch.argmax(probabilities).item()
|
| 60 |
+
# Get the probability for the predicted class
|
| 61 |
+
predicted_probability = probabilities[predicted_class].item()
|
| 62 |
+
# Define class labels for tomato
|
| 63 |
+
class_labels = ['Tomato Early Blight', 'Tomato Late Blight', 'Tomato Healthy']
|
| 64 |
+
|
| 65 |
+
return class_labels[predicted_class], predicted_probability, image
|