Spaces:
Sleeping
Sleeping
Basak Tamer commited on
Update src/streamlit_app.py
Browse files- 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
|
| 4 |
-
from PIL import Image
|
| 5 |
|
| 6 |
-
# --- Load Transfer Model ---
|
| 7 |
@st.cache_resource
|
| 8 |
def load_transfer():
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|