|
import streamlit as st |
|
from PIL import Image |
|
import torch |
|
import torch.nn as nn |
|
import torchvision.transforms as transforms |
|
import torchvision.models as models |
|
import numpy as np |
|
|
|
|
|
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') |
|
|
|
|
|
main_model = models.resnet18(pretrained=False) |
|
num_ftrs = main_model.fc.in_features |
|
main_model.fc = nn.Linear(num_ftrs, 3) |
|
main_model.load_state_dict(torch.load('Main_Classifier_best_model.pth', map_location=device)) |
|
main_model = main_model.to(device) |
|
main_model.eval() |
|
|
|
|
|
main_class_names = ['Clothing', 'Mobile Phones', 'Soda drinks'] |
|
|
|
|
|
def load_soda_drinks_model(): |
|
model = models.resnet18(pretrained=False) |
|
num_ftrs = model.fc.in_features |
|
model.fc = nn.Linear(num_ftrs, 3) |
|
model.load_state_dict(torch.load('Soda_drinks_best_model.pth', map_location=device)) |
|
model = model.to(device) |
|
model.eval() |
|
return model |
|
|
|
def load_clothing_model(): |
|
model = models.resnet18(pretrained=False) |
|
num_ftrs = model.fc.in_features |
|
model.fc = nn.Linear(num_ftrs, 2) |
|
model.load_state_dict(torch.load('Clothes_best_model.pth', map_location=device)) |
|
model = model.to(device) |
|
model.eval() |
|
return model |
|
|
|
def load_mobile_phones_model(): |
|
model = models.resnet18(pretrained=False) |
|
num_ftrs = model.fc.in_features |
|
model.fc = nn.Linear(num_ftrs, 2) |
|
model.load_state_dict(torch.load('Phone_best_model.pth', map_location=device)) |
|
model = model.to(device) |
|
model.eval() |
|
return model |
|
|
|
def convert_to_rgb(image): |
|
""" |
|
Converts 'P' mode images with transparency to 'RGBA', and then to 'RGB'. |
|
This is to avoid transparency issues during model training. |
|
""" |
|
if image.mode in ('P', 'RGBA'): |
|
return image.convert('RGB') |
|
return image |
|
|
|
|
|
preprocess = transforms.Compose([ |
|
transforms.Lambda(convert_to_rgb), |
|
transforms.Resize((224, 224)), |
|
transforms.ToTensor(), |
|
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) |
|
]) |
|
|
|
|
|
st.title("Main Classifier and Sub-Classifier System") |
|
st.write("Upload an image to classify whether it belongs to Clothing, Mobile Phones, or Soda Drinks. Based on the prediction, it will further classify within the subcategory.") |
|
|
|
|
|
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"]) |
|
|
|
if uploaded_file is not None: |
|
|
|
image = Image.open(uploaded_file) |
|
|
|
|
|
st.image(image, caption='Uploaded Image', use_column_width=True) |
|
st.write("") |
|
st.write("Classifying...") |
|
|
|
|
|
input_image = preprocess(image).unsqueeze(0).to(device) |
|
|
|
|
|
with torch.no_grad(): |
|
output = main_model(input_image) |
|
probabilities = torch.nn.functional.softmax(output[0], dim=0) |
|
confidence, predicted_class = torch.max(probabilities, 0) |
|
|
|
|
|
main_prediction = main_class_names[predicted_class] |
|
st.write(f"**Main Predicted Class:** {main_prediction}") |
|
st.write(f"**Confidence:** {confidence.item():.4f}") |
|
|
|
|
|
if main_prediction == 'Soda drinks': |
|
st.write("Loading Soda Drinks Model...") |
|
soda_model = load_soda_drinks_model() |
|
sub_class_names = ['Miranda', 'Pepsi', 'Seven Up'] |
|
elif main_prediction == 'Clothing': |
|
st.write("Loading Clothing Model...") |
|
clothing_model = load_clothing_model() |
|
sub_class_names = ['Pants', 'T-Shirt'] |
|
elif main_prediction == 'Mobile Phones': |
|
st.write("Loading Mobile Phones Model...") |
|
phones_model = load_mobile_phones_model() |
|
sub_class_names = ['Apple', 'Samsung'] |
|
|
|
|
|
with torch.no_grad(): |
|
if main_prediction == 'Soda drinks': |
|
sub_output = soda_model(input_image) |
|
elif main_prediction == 'Clothing': |
|
sub_output = clothing_model(input_image) |
|
elif main_prediction == 'Mobile Phones': |
|
sub_output = phones_model(input_image) |
|
|
|
sub_probabilities = torch.nn.functional.softmax(sub_output[0], dim=0) |
|
sub_confidence, sub_predicted_class = torch.max(sub_probabilities, 0) |
|
|
|
|
|
st.write(f"**Sub Predicted Class:** {sub_class_names[sub_predicted_class]}") |
|
st.write(f"**Confidence:** {sub_confidence.item():.4f}") |
|
|