Load full H5 model via load_model; pin TF 2.20 to match training runtime
Browse files- app.py +18 -14
- requirements.txt +7 -7
app.py
CHANGED
|
@@ -86,29 +86,33 @@ def load_trained_model():
|
|
| 86 |
import os
|
| 87 |
from huggingface_hub import hf_hub_download
|
| 88 |
|
| 89 |
-
|
| 90 |
|
| 91 |
-
#
|
| 92 |
-
if os.path.exists(
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
model.load_weights(model_path)
|
| 96 |
-
return model
|
| 97 |
-
except Exception as e:
|
| 98 |
-
st.warning(f"Local model load failed: {e}. Downloading from HF Hub...")
|
| 99 |
-
|
| 100 |
-
# Download from HF Model Hub
|
| 101 |
-
try:
|
| 102 |
st.info("Downloading model from Hugging Face Hub...")
|
| 103 |
model_path = hf_hub_download(
|
| 104 |
repo_id="usamaalam/image-forgery-detection-model",
|
| 105 |
filename="M3_best.h5",
|
| 106 |
cache_dir=".cache"
|
| 107 |
)
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
| 110 |
st.success("Model loaded successfully!")
|
| 111 |
return model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
except Exception as e:
|
| 113 |
st.error(f"Failed to load model: {e}")
|
| 114 |
return None
|
|
|
|
| 86 |
import os
|
| 87 |
from huggingface_hub import hf_hub_download
|
| 88 |
|
| 89 |
+
local_path = 'M3_best.h5'
|
| 90 |
|
| 91 |
+
# Resolve a path to the H5 weights (local first, then HF Hub)
|
| 92 |
+
if os.path.exists(local_path):
|
| 93 |
+
model_path = local_path
|
| 94 |
+
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
st.info("Downloading model from Hugging Face Hub...")
|
| 96 |
model_path = hf_hub_download(
|
| 97 |
repo_id="usamaalam/image-forgery-detection-model",
|
| 98 |
filename="M3_best.h5",
|
| 99 |
cache_dir=".cache"
|
| 100 |
)
|
| 101 |
+
|
| 102 |
+
# Preferred: load the full saved model (architecture + weights) from H5
|
| 103 |
+
try:
|
| 104 |
+
model = tf.keras.models.load_model(model_path, compile=False)
|
| 105 |
st.success("Model loaded successfully!")
|
| 106 |
return model
|
| 107 |
+
except Exception as e:
|
| 108 |
+
st.warning(f"Full-model load failed ({e}); rebuilding architecture and loading weights...")
|
| 109 |
+
|
| 110 |
+
# Fallback: rebuild architecture and load weights by name
|
| 111 |
+
try:
|
| 112 |
+
model = build_model('M3')
|
| 113 |
+
model.load_weights(model_path, by_name=True, skip_mismatch=True)
|
| 114 |
+
st.success("Model loaded (weights-only fallback).")
|
| 115 |
+
return model
|
| 116 |
except Exception as e:
|
| 117 |
st.error(f"Failed to load model: {e}")
|
| 118 |
return None
|
requirements.txt
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
streamlit==1.35.0
|
| 2 |
-
tensorflow==2.
|
| 3 |
-
opencv-python-headless==4.
|
| 4 |
-
pillow==10.
|
| 5 |
-
numpy==1.
|
| 6 |
-
scikit-learn==1.
|
| 7 |
-
matplotlib==3.
|
| 8 |
-
huggingface-hub==0.
|
|
|
|
| 1 |
streamlit==1.35.0
|
| 2 |
+
tensorflow==2.20.0
|
| 3 |
+
opencv-python-headless==4.10.0.84
|
| 4 |
+
pillow==10.4.0
|
| 5 |
+
numpy==1.26.4
|
| 6 |
+
scikit-learn==1.5.2
|
| 7 |
+
matplotlib==3.9.2
|
| 8 |
+
huggingface-hub==0.25.2
|