Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +44 -0
- best_modelv2.pth +3 -0
- model.py +23 -0
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from torchvision import transforms
|
| 5 |
+
from model import ResNet50 # Assuming your model architecture is defined in a separate file called model.py
|
| 6 |
+
|
| 7 |
+
# Load the model
|
| 8 |
+
model = ResNet50()
|
| 9 |
+
model.load_state_dict(torch.load('best_modelv2.pth', map_location=torch.device('cpu')))
|
| 10 |
+
model.eval()
|
| 11 |
+
|
| 12 |
+
# Define transform for input images
|
| 13 |
+
data_transforms = transforms.Compose([
|
| 14 |
+
transforms.Resize((224, 224)),
|
| 15 |
+
transforms.ToTensor(),
|
| 16 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 17 |
+
])
|
| 18 |
+
|
| 19 |
+
# Function to predict image label
|
| 20 |
+
def predict_image_label(image):
|
| 21 |
+
# Preprocess the image
|
| 22 |
+
image = data_transforms(image).unsqueeze(0)
|
| 23 |
+
|
| 24 |
+
# Make prediction
|
| 25 |
+
with torch.no_grad():
|
| 26 |
+
output = model(image)
|
| 27 |
+
_, predicted = torch.max(output, 1)
|
| 28 |
+
|
| 29 |
+
return predicted.item()
|
| 30 |
+
|
| 31 |
+
# Streamlit app
|
| 32 |
+
st.title("Leaf or Plant Classifier")
|
| 33 |
+
|
| 34 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 35 |
+
|
| 36 |
+
if uploaded_file is not None:
|
| 37 |
+
# Display the uploaded image
|
| 38 |
+
image = Image.open(uploaded_file)
|
| 39 |
+
st.image(image, caption='Uploaded Image', use_column_width=True)
|
| 40 |
+
|
| 41 |
+
# Classify the image
|
| 42 |
+
prediction = predict_image_label(image)
|
| 43 |
+
label = 'Leaf' if prediction == 0 else 'Plant'
|
| 44 |
+
st.write(f"Prediction: {label}")
|
best_modelv2.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9bd76196b9333f2dac3eccbb8ca6d1e0a4cf5d3daf898e53a608fc796addc99a
|
| 3 |
+
size 94370430
|
model.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import torch.optim as optim
|
| 4 |
+
import torchvision.transforms as transforms
|
| 5 |
+
from torchvision import models
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import os
|
| 8 |
+
import random
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class ResNet50(nn.Module):
|
| 12 |
+
def __init__(self):
|
| 13 |
+
super(ResNet50, self).__init__()
|
| 14 |
+
self.resnet = models.resnet50(pretrained=True)
|
| 15 |
+
for param in self.resnet.parameters():
|
| 16 |
+
param.requires_grad = False
|
| 17 |
+
self.resnet.fc = nn.Sequential(
|
| 18 |
+
nn.Linear(2048, 2)
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
def forward(self, x):
|
| 22 |
+
x = self.resnet(x)
|
| 23 |
+
return x
|