Update app.py
Browse files
app.py
CHANGED
|
@@ -1,71 +1,55 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import tensorflow as tf
|
| 3 |
-
import numpy as np
|
| 4 |
-
from PIL import Image
|
| 5 |
-
import json
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
st.
|
| 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 |
-
st.subheader("β
Prediction")
|
| 58 |
-
st.write(f"**Top-1:** {labels[str(top_idx[0])]} ({preds[top_idx[0]]*100:.2f}%)")
|
| 59 |
-
|
| 60 |
-
st.subheader("π Top-3 Predictions")
|
| 61 |
-
for i in top_idx:
|
| 62 |
-
st.write(f"{labels[str(i)]}: {preds[i]*100:.2f}%")
|
| 63 |
-
|
| 64 |
-
# -----------------------------
|
| 65 |
-
# BAR CHART
|
| 66 |
-
# -----------------------------
|
| 67 |
-
st.subheader("π All Class Probabilities")
|
| 68 |
-
st.bar_chart({labels[str(i)]: float(preds[i]) for i in range(len(labels))})
|
| 69 |
-
|
| 70 |
-
else:
|
| 71 |
-
st.info("Upload an image to see predictions.")
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import json
|
| 6 |
+
|
| 7 |
+
st.set_page_config(
|
| 8 |
+
page_title="CIFAR-10 Classifier",
|
| 9 |
+
page_icon="πΌοΈ",
|
| 10 |
+
layout="centered",
|
| 11 |
+
)
|
| 12 |
+
st.title("π CIFAR-10 Image Classifier")
|
| 13 |
+
st.markdown("Upload an image and see what the model predicts!")
|
| 14 |
+
|
| 15 |
+
@st.cache_resource
|
| 16 |
+
def load_model_and_labels():
|
| 17 |
+
model = tf.keras.models.load_model("models/cifar10_cnn.keras")
|
| 18 |
+
with open("models/labels_map.json", "r") as f:
|
| 19 |
+
labels = json.load(f)
|
| 20 |
+
return model, labels
|
| 21 |
+
|
| 22 |
+
model, labels = load_model_and_labels()
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
uploaded_file = st.file_uploader("Upload an image (PNG/JPG)", type=["png","jpg","jpeg"])
|
| 26 |
+
|
| 27 |
+
if uploaded_file:
|
| 28 |
+
img = Image.open(uploaded_file).convert("RGB")
|
| 29 |
+
st.image(img, caption="Uploaded Image", use_column_width=False)
|
| 30 |
+
|
| 31 |
+
def preprocess_image(img):
|
| 32 |
+
img = img.resize((32,32))
|
| 33 |
+
img = np.array(img)/255.0
|
| 34 |
+
return img
|
| 35 |
+
|
| 36 |
+
x = preprocess_image(img)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
with st.spinner("Predicting..."):
|
| 40 |
+
x_input = x.reshape(1,32,32,3)
|
| 41 |
+
preds = model.predict(x_input)[0]
|
| 42 |
+
top_idx = preds.argsort()[-3:][::-1]
|
| 43 |
+
|
| 44 |
+
st.subheader("β
Prediction")
|
| 45 |
+
st.write(f"**Top-1:** {labels[str(top_idx[0])]} ({preds[top_idx[0]]*100:.2f}%)")
|
| 46 |
+
|
| 47 |
+
st.subheader("π Top-3 Predictions")
|
| 48 |
+
for i in top_idx:
|
| 49 |
+
st.write(f"{labels[str(i)]}: {preds[i]*100:.2f}%")
|
| 50 |
+
|
| 51 |
+
st.subheader("π All Class Probabilities")
|
| 52 |
+
st.bar_chart({labels[str(i)]: float(preds[i]) for i in range(len(labels))})
|
| 53 |
+
|
| 54 |
+
else:
|
| 55 |
+
st.info("Upload an image to see predictions.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|