Update src/streamlit_app.py
Browse files- src/streamlit_app.py +32 -183
src/streamlit_app.py
CHANGED
|
@@ -1,13 +1,15 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from pathlib import Path
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from app.config import DATA_ROOT, IMAGE_EXTENSIONS
|
| 6 |
|
| 7 |
# Paths inside the Space
|
| 8 |
PROJECT_ROOT = Path(__file__).resolve().parent
|
| 9 |
-
REPORTS_DIR = PROJECT_ROOT / "reports"
|
| 10 |
-
CONFUSION_IMAGE_PATH = REPORTS_DIR / "confusion_matrix_normalized.png"
|
| 11 |
|
| 12 |
# -------------------------------------------------------------------
|
| 13 |
# STATIC REPORT TEXTS (Block 8 and Block 10)
|
|
@@ -142,174 +144,7 @@ Predicting: 100%|βββββββββββββββββββββ
|
|
| 142 |
π Overall Accuracy: 0.9975 (99.75%)
|
| 143 |
π Average confidence: 0.8239
|
| 144 |
β Failed to process: 48 images
|
| 145 |
-
|
| 146 |
-
β WRONG PREDICTIONS DETAILS (44 total)
|
| 147 |
-
================================================================================
|
| 148 |
-
Classes with wrong predictions:
|
| 149 |
-
actual_class wrong_count predicted_as
|
| 150 |
-
pins_Brie Larson 4 pins_Emma Stone, pins_ellen page, pins_Jennifer Lawrence
|
| 151 |
-
pins_Jessica Barden 3 pins_Alex Lawther, pins_kiernen shipka, pins_Danielle Panabaker
|
| 152 |
-
pins_Logan Lerman 3 pins_Sarah Wayne Callies, pins_Leonardo DiCaprio, pins_Eliza Taylor
|
| 153 |
-
pins_scarlett johansson 2 pins_gal gadot, pins_Taylor Swift
|
| 154 |
-
pins_Emilia Clarke 2 pins_Marie Avgeropoulos, pins_Irina Shayk
|
| 155 |
-
pins_Brenton Thwaites 2 pins_Leonardo DiCaprio, pins_Bobby Morley
|
| 156 |
-
pins_elizabeth olsen 2 pins_ellen page, pins_Millie Bobby Brown
|
| 157 |
-
pins_Zendaya 2 pins_Lili Reinhart, pins_Selena Gomez
|
| 158 |
-
pins_Tom Holland 2 pins_Anne Hathaway, pins_Robert Downey Jr
|
| 159 |
-
pins_Tom Hiddleston 1 pins_Chris Evans
|
| 160 |
-
pins_Selena Gomez 1 pins_Emma Watson
|
| 161 |
-
pins_Taylor Swift 1 pins_Emma Stone
|
| 162 |
-
pins_Zoe Saldana 1 pins_Lindsey Morgan
|
| 163 |
-
pins_Richard Harmon 1 pins_Leonardo DiCaprio
|
| 164 |
-
pins_amber heard 1 pins_Brie Larson
|
| 165 |
-
pins_gal gadot 1 pins_Madelaine Petsch
|
| 166 |
-
pins_margot robbie 1 pins_Emma Watson
|
| 167 |
-
pins_Sarah Wayne Callies 1 pins_Lindsey Morgan
|
| 168 |
-
pins_Avril Lavigne 1 pins_Alexandra Daddario
|
| 169 |
-
pins_Natalie Portman 1 pins_Millie Bobby Brown
|
| 170 |
-
pins_Nadia Hilker 1 pins_Marie Avgeropoulos
|
| 171 |
-
pins_Marie Avgeropoulos 1 pins_Johnny Depp
|
| 172 |
-
pins_Lindsey Morgan 1 pins_Anne Hathaway
|
| 173 |
-
pins_Leonardo DiCaprio 1 pins_Brenton Thwaites
|
| 174 |
-
pins_Katherine Langford 1 pins_Madelaine Petsch
|
| 175 |
-
pins_Johnny Depp 1 pins_Leonardo DiCaprio
|
| 176 |
-
pins_Irina Shayk 1 pins_Maria Pedraza
|
| 177 |
-
pins_Hugh Jackman 1 pins_tom ellis
|
| 178 |
-
pins_Emma Stone 1 pins_margot robbie
|
| 179 |
-
pins_Chris Hemsworth 1 pins_Chris Evans
|
| 180 |
-
pins_Morena Baccarin 1 pins_camila mendes
|
| 181 |
-
--------------------------------------------------------------------------------
|
| 182 |
-
Individual wrong predictions (showing first 20):
|
| 183 |
-
--------------------------------------------------------------------------------
|
| 184 |
-
β’ amber heard214_323.jpg
|
| 185 |
-
Actual: pins_amber heard β Predicted: pins_Brie Larson (confidence: 0.275)
|
| 186 |
-
β’ Avril Lavigne238_664.jpg
|
| 187 |
-
Actual: pins_Avril Lavigne β Predicted: pins_Alexandra Daddario (confidence: 0.131)
|
| 188 |
-
β’ Brenton Thwaites46_885.jpg
|
| 189 |
-
Actual: pins_Brenton Thwaites β Predicted: pins_Leonardo DiCaprio (confidence: 0.273)
|
| 190 |
-
β’ Brenton Thwaites99_936.jpg
|
| 191 |
-
Actual: pins_Brenton Thwaites β Predicted: pins_Bobby Morley (confidence: 0.204)
|
| 192 |
-
β’ Brie Larson157_994.jpg
|
| 193 |
-
Actual: pins_Brie Larson β Predicted: pins_Emma Stone (confidence: 0.136)
|
| 194 |
-
β’ Brie Larson172_1007.jpg
|
| 195 |
-
Actual: pins_Brie Larson β Predicted: pins_ellen page (confidence: 0.253)
|
| 196 |
-
β’ Brie Larson187_1021.jpg
|
| 197 |
-
Actual: pins_Brie Larson β Predicted: pins_Jennifer Lawrence (confidence: 0.127)
|
| 198 |
-
β’ Brie Larson77_1088.jpg
|
| 199 |
-
Actual: pins_Brie Larson β Predicted: pins_margot robbie (confidence: 0.330)
|
| 200 |
-
β’ Chris Hemsworth1_384.jpg
|
| 201 |
-
Actual: pins_Chris Hemsworth β Predicted: pins_Chris Evans (confidence: 0.095)
|
| 202 |
-
β’ elizabeth olsen164_1173.jpg
|
| 203 |
-
Actual: pins_elizabeth olsen β Predicted: pins_ellen page (confidence: 0.355)
|
| 204 |
-
β’ elizabeth olsen170_1179.jpg
|
| 205 |
-
Actual: pins_elizabeth olsen β Predicted: pins_Millie Bobby Brown (confidence: 0.272)
|
| 206 |
-
β’ Emilia Clarke194_952.jpg
|
| 207 |
-
Actual: pins_Emilia Clarke β Predicted: pins_Marie Avgeropoulos (confidence: 0.150)
|
| 208 |
-
β’ Emilia Clarke48_1021.jpg
|
| 209 |
-
Actual: pins_Emilia Clarke β Predicted: pins_Irina Shayk (confidence: 0.069)
|
| 210 |
-
β’ Emma Stone36_1779.jpg
|
| 211 |
-
Actual: pins_Emma Stone β Predicted: pins_margot robbie (confidence: 0.282)
|
| 212 |
-
β’ gal gadot134_1690.jpg
|
| 213 |
-
Actual: pins_gal gadot β Predicted: pins_Madelaine Petsch (confidence: 0.234)
|
| 214 |
-
β’ Hugh Jackman118_1288.jpg
|
| 215 |
-
Actual: pins_Hugh Jackman β Predicted: pins_tom ellis (confidence: 0.128)
|
| 216 |
-
β’ Irina Shayk236_2335.jpg
|
| 217 |
-
Actual: pins_Irina Shayk β Predicted: pins_Maria Pedraza (confidence: 0.082)
|
| 218 |
-
β’ Jessica Barden211_1449.jpg
|
| 219 |
-
Actual: pins_Jessica Barden β Predicted: pins_Alex Lawther (confidence: 0.779)
|
| 220 |
-
β’ Jessica Barden31_1475.jpg
|
| 221 |
-
Actual: pins_Jessica Barden β Predicted: pins_kiernen shipka (confidence: 0.098)
|
| 222 |
-
β’ Jessica Barden34_1478.jpg
|
| 223 |
-
Actual: pins_Jessica Barden β Predicted: pins_Danielle Panabaker (confidence: 0.048)
|
| 224 |
-
... and 24 more wrong predictions (see CSV for details)
|
| 225 |
-
================================================================================
|
| 226 |
-
π― CLASSES WITH 100% ACCURACY (74 classes)
|
| 227 |
-
================================================================================
|
| 228 |
-
class_name total_count
|
| 229 |
-
pins_Adriana Lima 213
|
| 230 |
-
pins_Millie Bobby Brown 191
|
| 231 |
-
pins_Rihanna 132
|
| 232 |
-
pins_Rebecca Ferguson 178
|
| 233 |
-
pins_Rami Malek 160
|
| 234 |
-
pins_Penn Badgley 171
|
| 235 |
-
pins_Pedro Alonso 125
|
| 236 |
-
pins_Neil Patrick Harris 116
|
| 237 |
-
pins_Natalie Dormer 196
|
| 238 |
-
pins_Morgan Freeman 102
|
| 239 |
-
pins_Miley Cyrus 178
|
| 240 |
-
pins_Keanu Reeves 158
|
| 241 |
-
pins_Megan Fox 208
|
| 242 |
-
pins_Mark Zuckerberg 95
|
| 243 |
-
pins_Mark Ruffalo 177
|
| 244 |
-
pins_Alex Lawther 152
|
| 245 |
-
pins_Maisie Williams 193
|
| 246 |
-
pins_Madelaine Petsch 192
|
| 247 |
-
pins_Lionel Messi 86
|
| 248 |
-
pins_Lili Reinhart 150
|
| 249 |
-
... and 54 more classes
|
| 250 |
-
================================================================================
|
| 251 |
-
β οΈ CLASSES WITH LOWEST ACCURACY (Bottom 10)
|
| 252 |
-
================================================================================
|
| 253 |
-
class_name correct_count wrong_count total_count accuracy
|
| 254 |
-
pins_Taylor Swift 129 1 130 0.992308
|
| 255 |
-
pins_elizabeth olsen 219 2 221 0.990950
|
| 256 |
-
pins_Brenton Thwaites 207 2 209 0.990431
|
| 257 |
-
pins_Emilia Clarke 207 2 209 0.990431
|
| 258 |
-
pins_scarlett johansson 199 2 201 0.990050
|
| 259 |
-
pins_Tom Holland 187 2 189 0.989418
|
| 260 |
-
pins_Logan Lerman 209 3 212 0.985849
|
| 261 |
-
pins_Zendaya 135 2 137 0.985401
|
| 262 |
-
pins_Jessica Barden 138 3 141 0.978723
|
| 263 |
-
pins_Brie Larson 165 4 169 0.976331
|
| 264 |
-
================================================================================
|
| 265 |
-
β
CORRECT PREDICTIONS SAMPLE (showing 10 of 17442)
|
| 266 |
-
================================================================================
|
| 267 |
-
β Adriana Lima0_0.jpg: pins_Adriana Lima (confidence: 0.787)
|
| 268 |
-
β Adriana Lima101_3.jpg: pins_Adriana Lima (confidence: 0.946)
|
| 269 |
-
β Adriana Lima102_4.jpg: pins_Adriana Lima (confidence: 0.907)
|
| 270 |
-
β Adriana Lima103_5.jpg: pins_Adriana Lima (confidence: 0.752)
|
| 271 |
-
β Adriana Lima104_6.jpg: pins_Adriana Lima (confidence: 0.886)
|
| 272 |
-
β Adriana Lima105_7.jpg: pins_Adriana Lima (confidence: 0.779)
|
| 273 |
-
β Adriana Lima106_8.jpg: pins_Adriana Lima (confidence: 0.794)
|
| 274 |
-
β Adriana Lima107_9.jpg: pins_Adriana Lima (confidence: 0.930)
|
| 275 |
-
β Adriana Lima108_10.jpg: pins_Adriana Lima (confidence: 0.902)
|
| 276 |
-
β Adriana Lima109_11.jpg: pins_Adriana Lima (confidence: 0.375)
|
| 277 |
-
================================================================================
|
| 278 |
-
π OUTPUT FILES SAVED:
|
| 279 |
-
================================================================================
|
| 280 |
-
β
predictions_results.csv
|
| 281 |
-
β All predictions sorted (correct first, then wrong)
|
| 282 |
-
β Columns: filename, actual, predicted, confidence, status, top3
|
| 283 |
-
β
predictions_summary.csv
|
| 284 |
-
β Per-class accuracy summary
|
| 285 |
-
β Columns: class_name, correct_count, wrong_count, total_count, accuracy
|
| 286 |
-
================================================================================
|
| 287 |
-
================================================================================
|
| 288 |
-
β FAILED TO PROCESS (48 images)
|
| 289 |
-
================================================================================
|
| 290 |
-
β’ Anne Hathaway203_391.jpg: No face detected
|
| 291 |
-
β’ Avril Lavigne11_572.jpg: No face detected
|
| 292 |
-
β’ Avril Lavigne174_619.jpg: No face detected
|
| 293 |
-
β’ Avril Lavigne41_685.jpg: No face detected
|
| 294 |
-
β’ barbara palvin158_800.jpg: No face detected
|
| 295 |
-
β’ Cristiano Ronaldo209_1326.jpg: No face detected
|
| 296 |
-
β’ Cristiano Ronaldo226_1333.jpg: No face detected
|
| 297 |
-
β’ Eliza Taylor202_775.jpg: No face detected
|
| 298 |
-
β’ Elizabeth Lail102_1055.jpg: No face detected
|
| 299 |
-
β’ Elizabeth Lail102_1056.jpg: No face detected
|
| 300 |
-
β’ Elizabeth Lail194_1117.jpg: No face detected
|
| 301 |
-
β’ Emilia Clarke78_1050.jpg: No face detected
|
| 302 |
-
β’ Emma Stone73_1817.jpg: No face detected
|
| 303 |
-
β’ Hugh Jackman119_1289.jpg: No face detected
|
| 304 |
-
β’ jeff bezos112_2049.jpg: No face detected
|
| 305 |
-
β’ jeff bezos12_2052.jpg: No face detected
|
| 306 |
-
β’ jeff bezos160_2068.jpg: No face detected
|
| 307 |
-
β’ jeff bezos178_2073.jpg: No face detected
|
| 308 |
-
β’ Jeremy Renner175_2634.jpg: No face detected
|
| 309 |
-
β’ Johnny Depp23_1863.jpg: No face detected
|
| 310 |
-
... and 28 more
|
| 311 |
-
β
Failed images list saved to: failed_predictions.csv
|
| 312 |
-
================================================================================
|
| 313 |
β
PROCESSING COMPLETE!
|
| 314 |
================================================================================
|
| 315 |
"""
|
|
@@ -466,7 +301,7 @@ if page == "Home":
|
|
| 466 |
st.warning("Dataset not ready yet.")
|
| 467 |
selected_image_path = None
|
| 468 |
else:
|
| 469 |
-
#
|
| 470 |
person_folders = sorted(
|
| 471 |
[f for f in DATA_ROOT.iterdir() if f.is_dir()]
|
| 472 |
)
|
|
@@ -480,7 +315,6 @@ if page == "Home":
|
|
| 480 |
"Select Person Folder", ["All"] + person_names
|
| 481 |
)
|
| 482 |
|
| 483 |
-
# Collect images depending on selection
|
| 484 |
if person_option == "All":
|
| 485 |
image_files = [
|
| 486 |
p
|
|
@@ -499,7 +333,6 @@ if page == "Home":
|
|
| 499 |
st.sidebar.warning("No images found in the selected folder.")
|
| 500 |
selected_image_path = None
|
| 501 |
else:
|
| 502 |
-
# Show relative paths (class_name/image.jpg) to make dropdown clearer
|
| 503 |
display_names = [
|
| 504 |
img.relative_to(DATA_ROOT).as_posix() for img in image_files
|
| 505 |
]
|
|
@@ -546,17 +379,33 @@ elif page == "Training Report":
|
|
| 546 |
st.code(TRAINING_REPORT_TEXT, language="text")
|
| 547 |
|
| 548 |
st.markdown("---")
|
| 549 |
-
st.subheader("
|
| 550 |
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
|
|
|
|
|
|
| 555 |
else:
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 560 |
|
| 561 |
# -------------------------------------------------------------------
|
| 562 |
# PREDICTION REPORT PAGE
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from pathlib import Path
|
| 3 |
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import numpy as np
|
| 6 |
+
from sklearn.metrics import confusion_matrix
|
| 7 |
+
|
| 8 |
+
from app.inference import load_models, predict_image, evaluate_dataset
|
| 9 |
from app.config import DATA_ROOT, IMAGE_EXTENSIONS
|
| 10 |
|
| 11 |
# Paths inside the Space
|
| 12 |
PROJECT_ROOT = Path(__file__).resolve().parent
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# -------------------------------------------------------------------
|
| 15 |
# STATIC REPORT TEXTS (Block 8 and Block 10)
|
|
|
|
| 144 |
π Overall Accuracy: 0.9975 (99.75%)
|
| 145 |
π Average confidence: 0.8239
|
| 146 |
β Failed to process: 48 images
|
| 147 |
+
... (rest of your long text, keep as is)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
β
PROCESSING COMPLETE!
|
| 149 |
================================================================================
|
| 150 |
"""
|
|
|
|
| 301 |
st.warning("Dataset not ready yet.")
|
| 302 |
selected_image_path = None
|
| 303 |
else:
|
| 304 |
+
# List person folders
|
| 305 |
person_folders = sorted(
|
| 306 |
[f for f in DATA_ROOT.iterdir() if f.is_dir()]
|
| 307 |
)
|
|
|
|
| 315 |
"Select Person Folder", ["All"] + person_names
|
| 316 |
)
|
| 317 |
|
|
|
|
| 318 |
if person_option == "All":
|
| 319 |
image_files = [
|
| 320 |
p
|
|
|
|
| 333 |
st.sidebar.warning("No images found in the selected folder.")
|
| 334 |
selected_image_path = None
|
| 335 |
else:
|
|
|
|
| 336 |
display_names = [
|
| 337 |
img.relative_to(DATA_ROOT).as_posix() for img in image_files
|
| 338 |
]
|
|
|
|
| 379 |
st.code(TRAINING_REPORT_TEXT, language="text")
|
| 380 |
|
| 381 |
st.markdown("---")
|
| 382 |
+
st.subheader("Confusion Matrix (programmatically computed, subset)")
|
| 383 |
|
| 384 |
+
# Compute confusion matrix on a small subset to keep it lightweight
|
| 385 |
+
with st.spinner("Computing confusion matrix on a small subset of the dataset..."):
|
| 386 |
+
df, acc = evaluate_dataset(images_per_class=2, max_images=150)
|
| 387 |
+
|
| 388 |
+
if df is None or df.empty:
|
| 389 |
+
st.info("Could not compute confusion matrix: evaluation subset is empty.")
|
| 390 |
else:
|
| 391 |
+
labels = sorted(df["true_label"].unique())
|
| 392 |
+
cm = confusion_matrix(df["true_label"], df["predicted_label"], labels=labels)
|
| 393 |
+
|
| 394 |
+
fig, ax = plt.subplots(figsize=(8, 8))
|
| 395 |
+
im = ax.imshow(cm, interpolation="nearest")
|
| 396 |
+
ax.set_xticks(np.arange(len(labels)))
|
| 397 |
+
ax.set_yticks(np.arange(len(labels)))
|
| 398 |
+
ax.set_xticklabels(labels, rotation=90, fontsize=5)
|
| 399 |
+
ax.set_yticklabels(labels, fontsize=5)
|
| 400 |
+
ax.set_xlabel("Predicted label")
|
| 401 |
+
ax.set_ylabel("True label")
|
| 402 |
+
ax.set_title("Confusion Matrix (subset)")
|
| 403 |
+
plt.tight_layout()
|
| 404 |
+
|
| 405 |
+
st.pyplot(fig)
|
| 406 |
+
|
| 407 |
+
if acc is not None:
|
| 408 |
+
st.caption(f"Subset accuracy on this evaluation run: {acc:.4f}")
|
| 409 |
|
| 410 |
# -------------------------------------------------------------------
|
| 411 |
# PREDICTION REPORT PAGE
|