Basak Tamer commited on
Commit
f0aa90f
ยท
verified ยท
1 Parent(s): 282a8de

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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: Custom CNN", page_icon="๐ŸŸ")
8
- labels = ['Hourse Mackerel', 'Black Sea Sprat','Sea Bass','Red Mullet','Trout', 'Striped Red Mullet', 'Shrimp','Gilt-Head Bream',
9
- 'Red Sea Bream']
 
 
 
 
 
10
 
11
  @st.cache_resource
12
- def load_cnn_model():
13
- # 1. Reconstruct the architecture (Matches your Cell 33)
 
 
 
 
 
 
 
14
  model = tf.keras.Sequential([
15
- tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(170, 170, 3)),
16
- tf.keras.layers.BatchNormalization(),
17
- tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
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/fish_cnnmodel.keras')
32
  return model
33
 
34
- model = load_cnn_model()
35
 
36
- st.title("๐ŸŸ Fish Classifier (Custom CNN)")
37
- st.info("Architecture: 5-Layer Custom CNN | Accuracy: 98.56%")
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
- x = x / 255.0 # Manual scaling for custom model
 
 
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)