Update model_utilis.py
Browse files- model_utilis.py +22 -26
model_utilis.py
CHANGED
|
@@ -2,54 +2,50 @@ import numpy as np
|
|
| 2 |
import tensorflow as tf
|
| 3 |
|
| 4 |
def load_model(model_path):
|
| 5 |
-
print(f"Loading model from {model_path}
|
| 6 |
model = tf.keras.models.load_model(model_path)
|
| 7 |
-
print("Model loaded
|
| 8 |
return model
|
| 9 |
|
| 10 |
def prepare_input_for_model(features, model):
|
|
|
|
| 11 |
features = np.asarray(features, dtype=np.float32)
|
| 12 |
-
|
| 13 |
|
| 14 |
-
if len(
|
| 15 |
-
target_T, target_D =
|
| 16 |
if target_D is not None and target_D != features.shape[1]:
|
| 17 |
-
|
| 18 |
-
features = features.T
|
| 19 |
-
else:
|
| 20 |
-
raise ValueError(f"Model expects feature dim {target_D}, got {features.shape[1]}")
|
| 21 |
-
|
| 22 |
if target_T is not None:
|
| 23 |
cur_T = features.shape[0]
|
| 24 |
if cur_T < target_T:
|
| 25 |
-
pad = target_T - cur_T
|
| 26 |
-
features = np.
|
| 27 |
-
|
| 28 |
features = features[:target_T, :]
|
| 29 |
-
|
| 30 |
-
elif len(input_shape) == 1:
|
| 31 |
flat = features.flatten()
|
| 32 |
-
target_len =
|
| 33 |
-
if
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
| 37 |
features = flat
|
| 38 |
-
|
| 39 |
else:
|
| 40 |
-
raise ValueError(f"Unsupported
|
| 41 |
|
| 42 |
return np.expand_dims(features, axis=0)
|
| 43 |
|
| 44 |
def interpret_prediction(raw_pred):
|
|
|
|
| 45 |
raw = np.asarray(raw_pred).squeeze()
|
| 46 |
if raw.size == 1:
|
| 47 |
val = float(raw)
|
| 48 |
-
prob_fake = val if 0.0 <= val <= 1.0 else 1
|
| 49 |
else:
|
| 50 |
probs = tf.nn.softmax(raw).numpy()
|
| 51 |
prob_fake = float(probs[1]) if probs.size >= 2 else float(probs.max())
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
prob_real = 1.0 - prob_fake
|
| 55 |
return {"Fake": prob_fake, "Real": prob_real}
|
|
|
|
| 2 |
import tensorflow as tf
|
| 3 |
|
| 4 |
def load_model(model_path):
|
| 5 |
+
print(f"Loading model from {model_path}...")
|
| 6 |
model = tf.keras.models.load_model(model_path)
|
| 7 |
+
print("Model loaded successfully.")
|
| 8 |
return model
|
| 9 |
|
| 10 |
def prepare_input_for_model(features, model):
|
| 11 |
+
"""Resize and batch the features to match model input."""
|
| 12 |
features = np.asarray(features, dtype=np.float32)
|
| 13 |
+
target_shape = model.input_shape[1:]
|
| 14 |
|
| 15 |
+
if len(target_shape) == 2:
|
| 16 |
+
target_T, target_D = target_shape
|
| 17 |
if target_D is not None and target_D != features.shape[1]:
|
| 18 |
+
features = features.T
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
if target_T is not None:
|
| 20 |
cur_T = features.shape[0]
|
| 21 |
if cur_T < target_T:
|
| 22 |
+
pad = np.zeros((target_T - cur_T, features.shape[1]))
|
| 23 |
+
features = np.vstack([features, pad])
|
| 24 |
+
else:
|
| 25 |
features = features[:target_T, :]
|
| 26 |
+
elif len(target_shape) == 1:
|
|
|
|
| 27 |
flat = features.flatten()
|
| 28 |
+
target_len = target_shape[0]
|
| 29 |
+
if target_len is not None:
|
| 30 |
+
if flat.shape[0] < target_len:
|
| 31 |
+
flat = np.pad(flat, (0, target_len - flat.shape[0]))
|
| 32 |
+
else:
|
| 33 |
+
flat = flat[:target_len]
|
| 34 |
features = flat
|
|
|
|
| 35 |
else:
|
| 36 |
+
raise ValueError(f"Unsupported input shape {target_shape}")
|
| 37 |
|
| 38 |
return np.expand_dims(features, axis=0)
|
| 39 |
|
| 40 |
def interpret_prediction(raw_pred):
|
| 41 |
+
"""Turn model output into readable Real/Fake probabilities."""
|
| 42 |
raw = np.asarray(raw_pred).squeeze()
|
| 43 |
if raw.size == 1:
|
| 44 |
val = float(raw)
|
| 45 |
+
prob_fake = val if 0.0 <= val <= 1.0 else 1 / (1 + np.exp(-val))
|
| 46 |
else:
|
| 47 |
probs = tf.nn.softmax(raw).numpy()
|
| 48 |
prob_fake = float(probs[1]) if probs.size >= 2 else float(probs.max())
|
| 49 |
+
prob_fake = float(np.clip(prob_fake, 0, 1))
|
| 50 |
+
prob_real = 1 - prob_fake
|
|
|
|
| 51 |
return {"Fake": prob_fake, "Real": prob_real}
|