FishClassification / src /streamlit_app.py
Basak Tamer
Update src/streamlit_app.py
f0aa90f verified
Raw History Blame
1.99 kB
import streamlit as st
import tensorflow as tf
from PIL import Image
import numpy as np
import os
# --- CONFIG ---
st.set_page_config(page_title="Fish AI: Transfer Learning", page_icon="🐠")
# IMPORTANT: Labels must be in Alphabetical Order to match Keras training
labels = [
'Black Sea Sprat', 'Gilt-Head Bream', 'Hourse Mackerel',
'Red Mullet', 'Red Sea Bream', 'Sea Bass',
'Shrimp', 'Striped Red Mullet', 'Trout'
]
@st.cache_resource
def load_tl_model():
# 1. Reconstruct the Transfer Learning Architecture (Matches Cell 44)
# We build the 'skeleton' so your old Mac doesn't get the 'batch_shape' error
base_model = tf.keras.applications.MobileNetV2(
input_shape=(170, 170, 3),
include_top=False,
weights=None
)
model = tf.keras.Sequential([
base_model,
tf.keras.layers.GlobalAveragePooling2D(),
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(9, activation='softmax')
])
# 2. Load the specific Transfer Learning weights
model.load_weights('src/fish_transfer_model.keras')
return model
model = load_tl_model()
st.title("🐠 Fish Classifier (Transfer Learning)")
st.info("Architecture: MobileNetV2 Backbone | Test Accuracy: 99.00%")
file = st.file_uploader("Upload Fish Image", type=["jpg", "png", "jpeg"])
if file:
# Resize to 170x170 as used in your Kaggle training
img = Image.open(file).convert("RGB").resize((170, 170))
st.image(img, use_container_width=True)
# Preprocessing
x = np.array(img).astype('float32')
x = np.expand_dims(x, axis=0)
# CRITICAL: Transfer Learning (MobileNetV2) needs this specific scaling
x = tf.keras.applications.mobilenet_v2.preprocess_input(x)
# Predict
preds = model.predict(x)
idx = np.argmax(preds)
st.success(f"Prediction: **{labels[idx]}**")
st.metric("Confidence", f"{preds[0][idx]*100:.2f}%")