File size: 1,547 Bytes
ae7a8d5
 
 
 
 
a982b06
 
ae7a8d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a982b06
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import streamlit as st
from transformers import ViTFeatureExtractor, ViTForImageClassification
from PIL import Image
import torch

# Load pre-trained model and feature extractor for CIFAR-10
model_name = "aaraki/vit-base-patch16-224-in21k-finetuned-cifar10"
feature_extractor = ViTFeatureExtractor.from_pretrained(model_name)
model = ViTForImageClassification.from_pretrained(model_name)

# CIFAR-10 class names
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']

# Streamlit app
st.title("CIFAR-10 Image Classification with Pre-trained Vision Transformer")

# Prediction on uploaded image
st.subheader("Make Predictions")
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])

if uploaded_file is not None:
    # Preprocess the uploaded image
    image = Image.open(uploaded_file).convert("RGB")
    st.image(image, caption='Uploaded Image', use_column_width=True)
    
    inputs = feature_extractor(images=image, return_tensors="pt")
    
    if st.button("Predict"):
        with st.spinner("Classifying..."):
            outputs = model(**inputs)
            logits = outputs.logits
            predicted_class_idx = logits.argmax(-1).item()
            
            # Check if the predicted_class_idx is within bounds
            if 0 <= predicted_class_idx < len(class_names):
                st.write(f"Predicted Class: {predicted_class_idx} ({class_names[predicted_class_idx]})")
            else:
                st.error("Prediction index out of range.")