Basak Tamer commited on
Commit
cc68d89
·
verified ·
1 Parent(s): cf70303

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +26 -25
src/streamlit_app.py CHANGED
@@ -1,33 +1,34 @@
1
  import streamlit as st
2
  import tensorflow as tf
3
- import numpy as np
4
- from PIL import Image
5
 
6
- # --- Load Transfer Model ---
7
  @st.cache_resource
8
  def load_transfer():
9
- return tf.keras.models.load_model('src/malaria_transfer_model.keras')
 
10
 
11
- model = load_transfer()
12
-
13
- st.title("🧬 Malaria Detector: Transfer Learning")
14
- st.write("Validation Accuracy: **90.89%**")
 
 
 
 
 
 
 
 
 
 
 
15
 
16
- file = st.file_uploader("Upload Cell Image", type=["jpg", "png"])
 
 
 
17
 
18
- if file:
19
- img = Image.open(file).convert('RGB')
20
- st.image(img, width=300)
21
-
22
- # Preprocessing (ensure size matches your transfer model input)
23
- img_resized = img.resize((64, 64))
24
- img_array = np.array(img_resized) / 255.0
25
- img_array = np.expand_dims(img_array, axis=0)
26
-
27
- if st.button("Predict with Transfer Learning"):
28
- pred = model.predict(img_array)[0][0]
29
-
30
- if pred < 0.5:
31
- st.error(f"PARASITIZED (Confidence: {(1-pred)*100:.2f}%)")
32
- else:
33
- st.success(f"UNINFECTED (Confidence: {pred*100:.2f}%)")
 
1
  import streamlit as st
2
  import tensorflow as tf
3
+ import os
 
4
 
 
5
  @st.cache_resource
6
  def load_transfer():
7
+ current_dir = os.path.dirname(os.path.abspath(__file__))
8
+ model_path = os.path.join(current_dir, 'src/malaria_transfer_model.keras')
9
 
10
+ try:
11
+ # 1. Rebuild the exact architecture you used in the notebook
12
+ # Assuming you used VGG16 (standard for Malaria TL)
13
+ base_model = tf.keras.applications.VGG16(
14
+ weights=None, # We don't need imagenet weights, we have yours
15
+ include_top=False,
16
+ input_shape=(224, 224, 3)
17
+ )
18
+
19
+ model = tf.keras.Sequential([
20
+ base_model,
21
+ tf.keras.layers.GlobalAveragePooling2D(),
22
+ tf.keras.layers.Dense(256, activation='relu'), # Match your notebook's units
23
+ tf.keras.layers.Dense(1, activation='sigmoid')
24
+ ])
25
 
26
+ # 2. Load ONLY the weights from your file
27
+ # This bypasses the "2 input tensors" error in the config
28
+ model.load_weights(model_path)
29
+ return model
30
 
31
+ except Exception as e:
32
+ st.error(f"Manual load failed: {e}")
33
+ st.info("If this fails, ensure the Layer units (e.g., 256) match your notebook exactly.")
34
+ return None