eaglelandsonce commited on
Commit
ae7a8d5
·
verified ·
1 Parent(s): 4c05b1a

Create 11_Cifar10_HF.py

Browse files
Files changed (1) hide show
  1. pages/11_Cifar10_HF.py +35 -0
pages/11_Cifar10_HF.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import ViTFeatureExtractor, ViTForImageClassification
3
+ from PIL import Image
4
+ import requests
5
+ import numpy as np
6
+ import torch
7
+
8
+ # Load pre-trained model and feature extractor
9
+ model_name = "google/vit-base-patch16-224"
10
+ feature_extractor = ViTFeatureExtractor.from_pretrained(model_name)
11
+ model = ViTForImageClassification.from_pretrained(model_name)
12
+
13
+ # CIFAR-10 class names
14
+ class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
15
+
16
+ # Streamlit app
17
+ st.title("CIFAR-10 Image Classification with Pre-trained Vision Transformer")
18
+
19
+ # Prediction on uploaded image
20
+ st.subheader("Make Predictions")
21
+ uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
22
+
23
+ if uploaded_file is not None:
24
+ # Preprocess the uploaded image
25
+ image = Image.open(uploaded_file).convert("RGB")
26
+ st.image(image, caption='Uploaded Image', use_column_width=True)
27
+
28
+ inputs = feature_extractor(images=image, return_tensors="pt")
29
+
30
+ if st.button("Predict"):
31
+ with st.spinner("Classifying..."):
32
+ outputs = model(**inputs)
33
+ logits = outputs.logits
34
+ predicted_class_idx = logits.argmax(-1).item()
35
+ st.write(f"Predicted Class: {predicted_class_idx} ({class_names[predicted_class_idx]})")