File size: 1,985 Bytes
6952f13
282a8de
 
 
44bb52b
6952f13
282a8de
f0aa90f
 
 
 
 
 
 
 
282a8de
 
f0aa90f
 
 
 
 
 
 
 
 
282a8de
f0aa90f
 
 
282a8de
 
 
 
f0aa90f
 
282a8de
 
f0aa90f
282a8de
f0aa90f
 
282a8de
 
 
 
f0aa90f
282a8de
 
 
 
 
 
f0aa90f
 
 
282a8de
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import streamlit as st
import tensorflow as tf
from PIL import Image
import numpy as np


# --- 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}%")