Update app.py
Browse files
app.py
CHANGED
|
@@ -1,91 +1,25 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import torch
|
| 3 |
-
import torch.nn as nn
|
| 4 |
-
import torchvision.models as models
|
| 5 |
-
from PIL import Image
|
| 6 |
-
from torchvision import transforms
|
| 7 |
import gradio as gr
|
| 8 |
-
import
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
# Image transformations
|
| 14 |
-
transform = transforms.Compose([
|
| 15 |
-
transforms.Resize((224, 224)),
|
| 16 |
-
transforms.ToTensor(),
|
| 17 |
-
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) # ImageNet standard
|
| 18 |
-
])
|
| 19 |
-
|
| 20 |
-
# Initialize models
|
| 21 |
-
def load_models():
|
| 22 |
-
# Load models (ResNet, MobileNetV2)
|
| 23 |
-
resnet18 = models.resnet18(pretrained=False)
|
| 24 |
-
resnet18.fc = nn.Linear(resnet18.fc.in_features, 11) # Modify output layer for 11 classes
|
| 25 |
-
resnet18.load_state_dict(torch.load('models/resnet18_fish_classification_model.pth'))
|
| 26 |
-
resnet18.to(device)
|
| 27 |
-
|
| 28 |
-
mobilenet_v2 = models.mobilenet_v2(pretrained=False)
|
| 29 |
-
mobilenet_v2.classifier[1] = nn.Linear(1280, 11) # Modify output layer for 11 classes
|
| 30 |
-
mobilenet_v2.load_state_dict(torch.load('models/mobilenetv2_fish_classification_model.pth'))
|
| 31 |
-
mobilenet_v2.to(device)
|
| 32 |
-
|
| 33 |
-
models_dict = {
|
| 34 |
-
'ResNet18': resnet18,
|
| 35 |
-
'MobileNetV2': mobilenet_v2
|
| 36 |
-
}
|
| 37 |
-
|
| 38 |
-
return models_dict
|
| 39 |
-
|
| 40 |
-
# Image prediction function
|
| 41 |
-
def predict_image(image):
|
| 42 |
-
models_dict = load_models()
|
| 43 |
-
image = transform(image).unsqueeze(0).to(device)
|
| 44 |
-
|
| 45 |
-
predictions = {}
|
| 46 |
-
for model_name, model in models_dict.items():
|
| 47 |
-
model.eval()
|
| 48 |
-
with torch.no_grad():
|
| 49 |
-
outputs = model(image)
|
| 50 |
-
probabilities = torch.nn.functional.softmax(outputs, dim=1)
|
| 51 |
-
_, predicted_class = torch.max(probabilities, 1)
|
| 52 |
-
confidence = probabilities[0, predicted_class].item()
|
| 53 |
-
predictions[model_name] = {'predicted_class': predicted_class.item(), 'confidence': confidence}
|
| 54 |
-
|
| 55 |
-
return predictions
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
y=[label, 'Other'],
|
| 62 |
-
orientation='h',
|
| 63 |
-
marker_color=['#667eea', '#e0e0e0'],
|
| 64 |
-
text=[f'{confidence:.1f}%', f'{100-confidence:.1f}%'],
|
| 65 |
-
textposition='inside',
|
| 66 |
-
textfont=dict(color='white', size=14)
|
| 67 |
-
))
|
| 68 |
-
fig.update_layout(
|
| 69 |
-
title=f"Prediction Confidence: {label}",
|
| 70 |
-
xaxis_title="Confidence (%)",
|
| 71 |
-
height=200,
|
| 72 |
-
margin=dict(l=0, r=0, t=40, b=0),
|
| 73 |
-
showlegend=False,
|
| 74 |
-
plot_bgcolor='rgba(0,0,0,0)',
|
| 75 |
-
paper_bgcolor='rgba(0,0,0,0)'
|
| 76 |
-
)
|
| 77 |
-
return fig
|
| 78 |
|
|
|
|
|
|
|
| 79 |
|
| 80 |
# Gradio Interface
|
| 81 |
def gradio_interface():
|
| 82 |
# This will load the models and use Gradio's interface to interact
|
| 83 |
-
models_dict = load_models()
|
| 84 |
-
|
| 85 |
gradio_interface = gr.Interface(
|
| 86 |
-
fn=predict_image,
|
| 87 |
-
inputs=gr.inputs.Image(type="pil"),
|
| 88 |
-
outputs=gr.outputs.JSON(),
|
| 89 |
live=True,
|
| 90 |
title="Fish Image Classification",
|
| 91 |
description="Classify fish images using AI models (ResNet18, MobileNetV2).",
|
|
@@ -93,15 +27,5 @@ def gradio_interface():
|
|
| 93 |
|
| 94 |
return gradio_interface
|
| 95 |
|
| 96 |
-
#
|
| 97 |
-
st.title("Fish Image Classification with AI")
|
| 98 |
-
|
| 99 |
-
st.markdown("""
|
| 100 |
-
This app allows you to upload a fish image, and it will classify the fish into one of the categories using two different AI models:
|
| 101 |
-
- **ResNet18**
|
| 102 |
-
- **MobileNetV2**
|
| 103 |
-
|
| 104 |
-
Upload an image below to get predictions.
|
| 105 |
-
""")
|
| 106 |
-
|
| 107 |
gradio_interface().launch(inline=True)
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
+
from inference import predict_image # Importing the predict_image function from inference.py
|
| 4 |
|
| 5 |
+
# Streamlit UI
|
| 6 |
+
st.title("Fish Image Classification with AI")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
st.markdown("""
|
| 9 |
+
This app allows you to upload a fish image, and it will classify the fish into one of the categories using two different AI models:
|
| 10 |
+
- **ResNet18**
|
| 11 |
+
- **MobileNetV2**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
Upload an image below to get predictions.
|
| 14 |
+
""")
|
| 15 |
|
| 16 |
# Gradio Interface
|
| 17 |
def gradio_interface():
|
| 18 |
# This will load the models and use Gradio's interface to interact
|
|
|
|
|
|
|
| 19 |
gradio_interface = gr.Interface(
|
| 20 |
+
fn=predict_image, # Function that performs inference
|
| 21 |
+
inputs=gr.inputs.Image(type="pil"), # Image input
|
| 22 |
+
outputs=gr.outputs.JSON(), # Output format (JSON with predictions)
|
| 23 |
live=True,
|
| 24 |
title="Fish Image Classification",
|
| 25 |
description="Classify fish images using AI models (ResNet18, MobileNetV2).",
|
|
|
|
| 27 |
|
| 28 |
return gradio_interface
|
| 29 |
|
| 30 |
+
# Add the Gradio Interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
gradio_interface().launch(inline=True)
|