Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +94 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,96 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
""
|
| 7 |
-
# Welcome to Streamlit!
|
| 8 |
-
|
| 9 |
-
Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
-
|
| 13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# ----------------------------
|
| 7 |
+
# Config
|
| 8 |
+
# ----------------------------
|
| 9 |
+
st.set_page_config(page_title="Fashion-MNIST CNN Classifier", page_icon="🧥", layout="centered")
|
| 10 |
+
|
| 11 |
+
CLASS_NAMES = [
|
| 12 |
+
"T-shirt/top", "Trouser", "Pullover", "Dress", "Coat",
|
| 13 |
+
"Sandal", "Shirt", "Sneaker", "Bag", "Ankle boot"
|
| 14 |
+
]
|
| 15 |
+
|
| 16 |
+
MODEL_PATH = "src/fashion_mnist_cnn.keras"
|
| 17 |
+
|
| 18 |
+
# ----------------------------
|
| 19 |
+
# Load model (cached)
|
| 20 |
+
# ----------------------------
|
| 21 |
+
@st.cache_resource
|
| 22 |
+
def load_model():
|
| 23 |
+
return tf.keras.models.load_model(MODEL_PATH)
|
| 24 |
+
|
| 25 |
+
# ----------------------------
|
| 26 |
+
# Preprocess image to (1, 28, 28, 1)
|
| 27 |
+
# ----------------------------
|
| 28 |
+
def preprocess_image(img: Image.Image, invert: bool = False):
|
| 29 |
+
img = img.convert("L") # grayscale
|
| 30 |
+
img = img.resize((28, 28)) # 28x28
|
| 31 |
+
|
| 32 |
+
arr = np.array(img).astype("float32")
|
| 33 |
+
|
| 34 |
+
if invert:
|
| 35 |
+
arr = 255.0 - arr # optional invert (useful for white background images)
|
| 36 |
+
|
| 37 |
+
arr = arr / 255.0
|
| 38 |
+
arr = arr.reshape(1, 28, 28, 1)
|
| 39 |
+
return arr
|
| 40 |
+
|
| 41 |
+
def softmax_topk(probs, k=3):
|
| 42 |
+
idx = np.argsort(probs)[::-1][:k]
|
| 43 |
+
return [(CLASS_NAMES[i], float(probs[i])) for i in idx]
|
| 44 |
+
|
| 45 |
+
# ----------------------------
|
| 46 |
+
# UI
|
| 47 |
+
# ----------------------------
|
| 48 |
+
st.title("🧥 Fashion-MNIST CNN Classifier")
|
| 49 |
+
st.write("Upload a clothing image and the CNN will predict one of 10 Fashion-MNIST classes.")
|
| 50 |
+
|
| 51 |
+
# Load model
|
| 52 |
+
try:
|
| 53 |
+
model = load_model()
|
| 54 |
+
except Exception as e:
|
| 55 |
+
st.error("Model could not be loaded. Make sure `fashion_mnist_cnn.keras` is in the repo root.")
|
| 56 |
+
st.exception(e)
|
| 57 |
+
st.stop()
|
| 58 |
+
|
| 59 |
+
st.sidebar.header("Options")
|
| 60 |
+
invert = st.sidebar.checkbox("Invert colors (try if prediction looks wrong)", value=False)
|
| 61 |
+
show_probs = st.sidebar.checkbox("Show probabilities", value=True)
|
| 62 |
+
|
| 63 |
+
uploaded = st.file_uploader("Upload an image (PNG/JPG)", type=["png", "jpg", "jpeg"])
|
| 64 |
+
|
| 65 |
+
if uploaded is None:
|
| 66 |
+
st.info("Please upload an image to start.")
|
| 67 |
+
st.stop()
|
| 68 |
+
|
| 69 |
+
# Display uploaded image
|
| 70 |
+
img = Image.open(uploaded)
|
| 71 |
+
st.image(img, caption="Uploaded image", use_container_width=True)
|
| 72 |
+
|
| 73 |
+
# Preprocess + predict
|
| 74 |
+
x = preprocess_image(img, invert=invert)
|
| 75 |
+
probs = model.predict(x, verbose=0)[0]
|
| 76 |
+
pred_idx = int(np.argmax(probs))
|
| 77 |
+
pred_label = CLASS_NAMES[pred_idx]
|
| 78 |
+
pred_conf = float(probs[pred_idx])
|
| 79 |
+
|
| 80 |
+
st.subheader("Prediction")
|
| 81 |
+
st.write(f"**Predicted class:** {pred_label}")
|
| 82 |
+
st.write(f"**Confidence:** {pred_conf:.3f}")
|
| 83 |
+
|
| 84 |
+
# Show top-3
|
| 85 |
+
top3 = softmax_topk(probs, k=3)
|
| 86 |
+
st.write("**Top 3:**")
|
| 87 |
+
for name, p in top3:
|
| 88 |
+
st.write(f"- {name}: {p:.3f}")
|
| 89 |
+
|
| 90 |
+
# Probability chart
|
| 91 |
+
if show_probs:
|
| 92 |
+
st.subheader("Class Probabilities")
|
| 93 |
+
chart_data = {CLASS_NAMES[i]: float(probs[i]) for i in range(10)}
|
| 94 |
+
st.bar_chart(chart_data)
|
| 95 |
|
| 96 |
+
st.caption("Tip: Fashion-MNIST images are 28×28 grayscale. For real photos, results may vary.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|