Pant0x commited on
Commit
42cbca5
Β·
verified Β·
1 Parent(s): 3562ea0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -71
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
- # CONFIG
9
- # -----------------------------
10
- st.set_page_config(
11
- page_title="CIFAR-10 Classifier",
12
- page_icon="πŸ–ΌοΈ",
13
- layout="centered",
14
- )
15
- st.title("πŸš€ CIFAR-10 Image Classifier")
16
- st.markdown("Upload an image and see what the model predicts!")
17
-
18
- # -----------------------------
19
- # LOAD MODEL AND LABELS
20
- # -----------------------------
21
- @st.cache_resource
22
- def load_model_and_labels():
23
- model = tf.keras.models.load_model("models/cifar10_cnn.keras")
24
- with open("models/labels_map.json", "r") as f:
25
- labels = json.load(f)
26
- return model, labels
27
-
28
- model, labels = load_model_and_labels()
29
-
30
- # -----------------------------
31
- # IMAGE UPLOAD
32
- # -----------------------------
33
- uploaded_file = st.file_uploader("Upload an image (PNG/JPG)", type=["png","jpg","jpeg"])
34
-
35
- if uploaded_file:
36
- img = Image.open(uploaded_file).convert("RGB")
37
- st.image(img, caption="Uploaded Image", use_column_width=False)
38
-
39
- # -----------------------------
40
- # PREPROCESS IMAGE
41
- # -----------------------------
42
- def preprocess_image(img):
43
- img = img.resize((32,32)) # CIFAR-10 input size
44
- img = np.array(img)/255.0
45
- return img
46
-
47
- x = preprocess_image(img)
48
-
49
- # -----------------------------
50
- # PREDICTION
51
- # -----------------------------
52
- with st.spinner("Predicting..."):
53
- x_input = x.reshape(1,32,32,3)
54
- preds = model.predict(x_input)[0]
55
- top_idx = preds.argsort()[-3:][::-1]
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.")