Spaces:
Sleeping
Sleeping
add model and requirements
Browse files- app.py +53 -0
- requirements.txt +11 -0
- transfer_learning-vgg16.ipynb +0 -0
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn as nn
|
| 5 |
+
from torchvision import models, transforms
|
| 6 |
+
from torch.utils.data import DataLoader
|
| 7 |
+
from torchvision.datasets import ImageFolder
|
| 8 |
+
|
| 9 |
+
vgg16 = models.vgg16(pretrained=True)
|
| 10 |
+
|
| 11 |
+
# Freeze the convolutional base to prevent updating weights during training
|
| 12 |
+
for param in vgg16.features.parameters():
|
| 13 |
+
param.requires_grad = False
|
| 14 |
+
|
| 15 |
+
num_features = vgg16.classifier[6].in_features
|
| 16 |
+
num_classes = 3
|
| 17 |
+
vgg16.classifier[6] = torch.nn.Linear(num_features, num_classes)
|
| 18 |
+
|
| 19 |
+
# Load the model
|
| 20 |
+
model = vgg16
|
| 21 |
+
state_dict = torch.load('vgg16_transfer_learning.pth')
|
| 22 |
+
model.load_state_dict(state_dict)
|
| 23 |
+
model.eval()
|
| 24 |
+
|
| 25 |
+
# Define the same transforms that were used during the model training
|
| 26 |
+
transform = transforms.Compose([
|
| 27 |
+
transforms.Resize((224, 224)),
|
| 28 |
+
transforms.ToTensor(),
|
| 29 |
+
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
|
| 30 |
+
])
|
| 31 |
+
|
| 32 |
+
classes = ('broccoli', 'cabbage', 'cauliflower')
|
| 33 |
+
|
| 34 |
+
def predict(image):
|
| 35 |
+
input_tensor = transform(image)
|
| 36 |
+
input_batch = input_tensor.unsqueeze(0)
|
| 37 |
+
|
| 38 |
+
with torch.no_grad():
|
| 39 |
+
output = model(input_batch)
|
| 40 |
+
|
| 41 |
+
probabilities = torch.nn.functional.softmax(output[0], dim=0)
|
| 42 |
+
max_value, predicted_class = torch.max(probabilities, 0)
|
| 43 |
+
return classes[predicted_class.item()], max_value.item() * 100
|
| 44 |
+
|
| 45 |
+
st.title('Vegetable Classification')
|
| 46 |
+
st.write('you can upload your image of veggies below')
|
| 47 |
+
|
| 48 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 49 |
+
if uploaded_file is not None:
|
| 50 |
+
image = Image.open(uploaded_file).convert('RGB')
|
| 51 |
+
st.image(image, caption='Uploaded Image')
|
| 52 |
+
label, confidence = predict(image)
|
| 53 |
+
st.write(f'Predicted label: {label}, confidence: {confidence:.2f}%')
|
requirements.txt
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch==2.2.1
|
| 2 |
+
torchvision==0.17.1
|
| 3 |
+
torchaudio==2.2.1
|
| 4 |
+
torchsummary==1.5.1
|
| 5 |
+
torcheval==0.0.7
|
| 6 |
+
ipykernel==6.29.3
|
| 7 |
+
ipython==8.22.2
|
| 8 |
+
scikit-learn
|
| 9 |
+
streamlit==1.32.2
|
| 10 |
+
toml==0.10.2
|
| 11 |
+
pillow==10.2.0
|
transfer_learning-vgg16.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|