-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
77 lines (57 loc) · 2.67 KB
/
app.py
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import streamlit as st
import numpy as np
import tensorflow as tf
from PIL import Image
# Function for MobileNetV2 ImageNet model
def mobilenetv2_imagenet():
st.title("Image Classification with MobileNetV2")
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png"])
if uploaded_file is not None:
image = Image.open(uploaded_file)
st.image(image, caption='Uploaded Image', use_column_width=True)
st.write("Classifying...")
# Load MobileNetV2 model
model = tf.keras.applications.MobileNetV2(weights='imagenet')
# Preprocess the image
img = image.resize((224, 224))
img_array = np.array(img)
img_array = np.expand_dims(img_array, axis=0)
img_array = tf.keras.applications.mobilenet_v2.preprocess_input(img_array)
# Make predictions
predictions = model.predict(img_array)
decoded_predictions = tf.keras.applications.mobilenet_v2.decode_predictions(predictions, top=1)[0]
for i, (imagenet_id, label, score) in enumerate(decoded_predictions):
st.write(f"{label}: {score * 100:.2f}%")
# Function for CIFAR-10 model
def cifar10_classification():
st.title("CIFAR-10 Image Classification")
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png"])
if uploaded_file is not None:
image = Image.open(uploaded_file)
st.image(image, caption='Uploaded Image', use_column_width=True)
st.write("Classifying...")
# Load CIFAR-10 model
model = tf.keras.models.load_model('cifar10_model.h5')
# CIFAR-10 class names
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
# Preprocess the image
img = image.resize((32, 32))
img_array = np.array(img)
img_array = img_array.astype('float32') / 255.0
img_array = np.expand_dims(img_array, axis=0)
# Make predictions
predictions = model.predict(img_array)
predicted_class = np.argmax(predictions, axis=1)[0]
confidence = np.max(predictions)
st.write(f"Predicted Class: {class_names[predicted_class]}")
st.write(f"Confidence: {confidence * 100:.2f}%")
# Main function to control the navigation
def main():
st.sidebar.title("Navigation")
choice = st.sidebar.selectbox("Choose Model", ("CIFAR-10","MobileNetV2 (ImageNet)"))
if choice == "MobileNetV2 (ImageNet)":
mobilenetv2_imagenet()
elif choice == "CIFAR-10":
cifar10_classification()
if __name__ == "__main__":
main()