Basak Tamer commited on
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +30 -23
src/streamlit_app.py
CHANGED
|
@@ -2,51 +2,58 @@ import streamlit as st
|
|
| 2 |
import tensorflow as tf
|
| 3 |
from PIL import Image
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
|
| 6 |
# --- CONFIG ---
|
| 7 |
-
st.set_page_config(page_title="Fish AI:
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
@st.cache_resource
|
| 12 |
-
def
|
| 13 |
-
# 1. Reconstruct the
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
model = tf.keras.Sequential([
|
| 15 |
-
|
| 16 |
-
tf.keras.layers.
|
| 17 |
-
tf.keras.layers.
|
| 18 |
-
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
|
| 19 |
-
tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
|
| 20 |
-
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
|
| 21 |
-
tf.keras.layers.Conv2D(128, (3, 3), activation='relu'),
|
| 22 |
-
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
|
| 23 |
-
tf.keras.layers.Conv2D(128, (3, 3), activation='relu'),
|
| 24 |
-
tf.keras.layers.Flatten(),
|
| 25 |
-
tf.keras.layers.Dense(256, activation='relu'),
|
| 26 |
tf.keras.layers.Dropout(0.5),
|
| 27 |
tf.keras.layers.Dense(9, activation='softmax')
|
| 28 |
])
|
| 29 |
|
| 30 |
-
# 2. Load the weights
|
| 31 |
-
model.load_weights('src/
|
| 32 |
return model
|
| 33 |
|
| 34 |
-
model =
|
| 35 |
|
| 36 |
-
st.title("
|
| 37 |
-
st.info("Architecture:
|
| 38 |
|
| 39 |
file = st.file_uploader("Upload Fish Image", type=["jpg", "png", "jpeg"])
|
| 40 |
|
| 41 |
if file:
|
| 42 |
-
# Resize to 170x170
|
| 43 |
img = Image.open(file).convert("RGB").resize((170, 170))
|
| 44 |
st.image(img, use_container_width=True)
|
| 45 |
|
| 46 |
# Preprocessing
|
| 47 |
x = np.array(img).astype('float32')
|
| 48 |
x = np.expand_dims(x, axis=0)
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
|
| 51 |
# Predict
|
| 52 |
preds = model.predict(x)
|
|
|
|
| 2 |
import tensorflow as tf
|
| 3 |
from PIL import Image
|
| 4 |
import numpy as np
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
# --- CONFIG ---
|
| 8 |
+
st.set_page_config(page_title="Fish AI: Transfer Learning", page_icon="๐ ")
|
| 9 |
+
|
| 10 |
+
# IMPORTANT: Labels must be in Alphabetical Order to match Keras training
|
| 11 |
+
labels = [
|
| 12 |
+
'Black Sea Sprat', 'Gilt-Head Bream', 'Hourse Mackerel',
|
| 13 |
+
'Red Mullet', 'Red Sea Bream', 'Sea Bass',
|
| 14 |
+
'Shrimp', 'Striped Red Mullet', 'Trout'
|
| 15 |
+
]
|
| 16 |
|
| 17 |
@st.cache_resource
|
| 18 |
+
def load_tl_model():
|
| 19 |
+
# 1. Reconstruct the Transfer Learning Architecture (Matches Cell 44)
|
| 20 |
+
# We build the 'skeleton' so your old Mac doesn't get the 'batch_shape' error
|
| 21 |
+
base_model = tf.keras.applications.MobileNetV2(
|
| 22 |
+
input_shape=(170, 170, 3),
|
| 23 |
+
include_top=False,
|
| 24 |
+
weights=None
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
model = tf.keras.Sequential([
|
| 28 |
+
base_model,
|
| 29 |
+
tf.keras.layers.GlobalAveragePooling2D(),
|
| 30 |
+
tf.keras.layers.Dense(512, activation='relu'),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
tf.keras.layers.Dropout(0.5),
|
| 32 |
tf.keras.layers.Dense(9, activation='softmax')
|
| 33 |
])
|
| 34 |
|
| 35 |
+
# 2. Load the specific Transfer Learning weights
|
| 36 |
+
model.load_weights('src/fish_transfer_model.keras')
|
| 37 |
return model
|
| 38 |
|
| 39 |
+
model = load_tl_model()
|
| 40 |
|
| 41 |
+
st.title("๐ Fish Classifier (Transfer Learning)")
|
| 42 |
+
st.info("Architecture: MobileNetV2 Backbone | Test Accuracy: 99.00%")
|
| 43 |
|
| 44 |
file = st.file_uploader("Upload Fish Image", type=["jpg", "png", "jpeg"])
|
| 45 |
|
| 46 |
if file:
|
| 47 |
+
# Resize to 170x170 as used in your Kaggle training
|
| 48 |
img = Image.open(file).convert("RGB").resize((170, 170))
|
| 49 |
st.image(img, use_container_width=True)
|
| 50 |
|
| 51 |
# Preprocessing
|
| 52 |
x = np.array(img).astype('float32')
|
| 53 |
x = np.expand_dims(x, axis=0)
|
| 54 |
+
|
| 55 |
+
# CRITICAL: Transfer Learning (MobileNetV2) needs this specific scaling
|
| 56 |
+
x = tf.keras.applications.mobilenet_v2.preprocess_input(x)
|
| 57 |
|
| 58 |
# Predict
|
| 59 |
preds = model.predict(x)
|