Update model_utilis.py
Browse files- model_utilis.py +44 -48
model_utilis.py
CHANGED
|
@@ -2,58 +2,54 @@ 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. Input shape =", model.input_shape)
|
| 8 |
-
return model
|
| 9 |
|
| 10 |
def prepare_input_for_model(features, model):
|
| 11 |
-
features = np.asarray(features, dtype=np.float32)
|
| 12 |
-
input_shape = model.input_shape[1:]
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
else:
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
if target_T is not None:
|
| 24 |
-
cur_T = features.shape[0]
|
| 25 |
-
if cur_T < target_T:
|
| 26 |
-
pad = target_T - cur_T
|
| 27 |
-
features = np.pad(features, ((0, pad), (0, 0)), mode="constant")
|
| 28 |
-
elif cur_T > target_T:
|
| 29 |
-
features = features[:target_T, :]
|
| 30 |
-
|
| 31 |
-
elif len(input_shape) == 1:
|
| 32 |
-
flat = features.flatten()
|
| 33 |
-
target_len = input_shape[0]
|
| 34 |
-
if flat.shape[0] < target_len:
|
| 35 |
-
flat = np.pad(flat, (0, target_len - flat.shape[0]), mode="constant")
|
| 36 |
-
else:
|
| 37 |
-
flat = flat[:target_len]
|
| 38 |
-
features = flat
|
| 39 |
|
| 40 |
-
else:
|
| 41 |
-
|
| 42 |
|
| 43 |
-
return np.expand_dims(features, axis=0)
|
| 44 |
-
```
|
| 45 |
|
| 46 |
def interpret_prediction(raw_pred):
|
| 47 |
-
raw = np.asarray(raw_pred).squeeze()
|
| 48 |
-
if raw.size == 1:
|
| 49 |
-
val = float(raw)
|
| 50 |
-
prob_fake = val if 0.0 <= val <= 1.0 else 1.0 / (1.0 + np.exp(-val))
|
| 51 |
-
else:
|
| 52 |
-
probs = tf.nn.softmax(raw).numpy()
|
| 53 |
-
prob_fake = float(probs[1]) if probs.size >= 2 else float(probs.max())
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
return {"Fake": prob_fake, "Real": prob_real}
|
| 59 |
-
```
|
|
|
|
| 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. Input shape =", model.input_shape)
|
| 8 |
+
return model
|
| 9 |
|
| 10 |
def prepare_input_for_model(features, model):
|
| 11 |
+
features = np.asarray(features, dtype=np.float32)
|
| 12 |
+
input_shape = model.input_shape[1:]
|
| 13 |
+
|
| 14 |
+
if len(input_shape) == 2:
|
| 15 |
+
target_T, target_D = input_shape
|
| 16 |
+
if target_D is not None and target_D != features.shape[1]:
|
| 17 |
+
if target_D == features.shape[0]:
|
| 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.pad(features, ((0, pad), (0, 0)), mode="constant")
|
| 27 |
+
elif cur_T > target_T:
|
| 28 |
+
features = features[:target_T, :]
|
| 29 |
+
|
| 30 |
+
elif len(input_shape) == 1:
|
| 31 |
+
flat = features.flatten()
|
| 32 |
+
target_len = input_shape[0]
|
| 33 |
+
if flat.shape[0] < target_len:
|
| 34 |
+
flat = np.pad(flat, (0, target_len - flat.shape[0]), mode="constant")
|
| 35 |
else:
|
| 36 |
+
flat = flat[:target_len]
|
| 37 |
+
features = flat
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
else:
|
| 40 |
+
raise ValueError(f"Unsupported model input shape: {input_shape}")
|
| 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.0 / (1.0 + np.exp(-val))
|
| 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 |
+
prob_fake = float(np.clip(prob_fake, 0.0, 1.0))
|
| 54 |
+
prob_real = 1.0 - prob_fake
|
| 55 |
+
return {"Fake": prob_fake, "Real": prob_real}
|
|
|
|
|
|