Zeyadd-Mostaffa commited on
Commit
3b7fc6c
·
verified ·
1 Parent(s): 0786c74

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -19
app.py CHANGED
@@ -30,10 +30,9 @@ def extract_face(image):
30
  return image[y:y+h, x:x+w]
31
 
32
  def grad_cam(model, image, size, preprocess):
33
- img_input = tf.expand_dims(image, axis=0) # Shape: (1, H, W, 3)
34
 
35
  last_conv_layer_name = None
36
- # Auto-detect last conv layer
37
  for layer in reversed(model.layers):
38
  if isinstance(layer, tf.keras.layers.Conv2D):
39
  last_conv_layer_name = layer.name
@@ -42,22 +41,18 @@ def grad_cam(model, image, size, preprocess):
42
  if not last_conv_layer_name:
43
  raise ValueError("No Conv2D layer found in the model.")
44
 
45
- grad_model = tf.keras.models.Model(
46
- [model.inputs], [model.get_layer(last_conv_layer_name).output, model.output]
47
- )
48
 
49
  with tf.GradientTape() as tape:
50
  conv_outputs, predictions = grad_model(img_input)
51
- loss = predictions[:, 0] # Assuming binary classification
52
 
53
  grads = tape.gradient(loss, conv_outputs)
54
-
55
- # Ensure correct shape
56
  if len(grads.shape) != 4:
57
  raise ValueError(f"Expected 4D tensor for grads, got shape: {grads.shape}")
58
 
59
- pooled_grads = tf.reduce_mean(grads, axis=(0, 1, 2)) # (C,)
60
- conv_outputs = conv_outputs[0] # (H, W, C)
61
  cam = tf.reduce_sum(conv_outputs * pooled_grads, axis=-1)
62
 
63
  heatmap = np.maximum(cam, 0)
@@ -68,32 +63,30 @@ def grad_cam(model, image, size, preprocess):
68
 
69
  face_rgb = (image.numpy() * 255).astype(np.uint8)
70
  face_rgb = cv2.resize(face_rgb, size)
71
-
72
  overlaid = cv2.addWeighted(face_rgb, 0.6, heatmap, 0.4, 0)
73
  return overlaid
74
 
75
-
76
  def predict(image):
77
  face = extract_face(image)
78
  if face is None:
79
  return "No face detected", None
80
 
81
- # Xception
82
  xcp_img = cv2.resize(face, (299, 299))
83
  xcp_tensor = xcp_pre(xcp_img.astype(np.float32))[np.newaxis, ...]
84
- xcp_pred = xcp_model.predict(xcp_tensor)[0][0]
85
 
86
- # EfficientNet
87
  eff_img = cv2.resize(face, (224, 224))
88
  eff_tensor = eff_pre(eff_img.astype(np.float32))[np.newaxis, ...]
89
- eff_pred = eff_model.predict(eff_tensor)[0][0]
90
 
91
- # Ensemble average
92
  avg_pred = (xcp_pred + eff_pred) / 2
93
  label = "Fake" if avg_pred > 0.5 else "Real"
94
 
95
- # Grad-CAM on Xception
96
- cam_img = grad_cam(xcp_model, face, (299, 299), xcp_pre)
 
 
97
 
98
  return label, cam_img
99
 
 
30
  return image[y:y+h, x:x+w]
31
 
32
  def grad_cam(model, image, size, preprocess):
33
+ img_input = tf.expand_dims(image, axis=0)
34
 
35
  last_conv_layer_name = None
 
36
  for layer in reversed(model.layers):
37
  if isinstance(layer, tf.keras.layers.Conv2D):
38
  last_conv_layer_name = layer.name
 
41
  if not last_conv_layer_name:
42
  raise ValueError("No Conv2D layer found in the model.")
43
 
44
+ grad_model = tf.keras.models.Model([model.inputs], [model.get_layer(last_conv_layer_name).output, model.output])
 
 
45
 
46
  with tf.GradientTape() as tape:
47
  conv_outputs, predictions = grad_model(img_input)
48
+ loss = predictions[:, 0]
49
 
50
  grads = tape.gradient(loss, conv_outputs)
 
 
51
  if len(grads.shape) != 4:
52
  raise ValueError(f"Expected 4D tensor for grads, got shape: {grads.shape}")
53
 
54
+ pooled_grads = tf.reduce_mean(grads, axis=(0, 1, 2))
55
+ conv_outputs = conv_outputs[0]
56
  cam = tf.reduce_sum(conv_outputs * pooled_grads, axis=-1)
57
 
58
  heatmap = np.maximum(cam, 0)
 
63
 
64
  face_rgb = (image.numpy() * 255).astype(np.uint8)
65
  face_rgb = cv2.resize(face_rgb, size)
 
66
  overlaid = cv2.addWeighted(face_rgb, 0.6, heatmap, 0.4, 0)
67
  return overlaid
68
 
 
69
  def predict(image):
70
  face = extract_face(image)
71
  if face is None:
72
  return "No face detected", None
73
 
74
+ # Preprocess faces for both models
75
  xcp_img = cv2.resize(face, (299, 299))
76
  xcp_tensor = xcp_pre(xcp_img.astype(np.float32))[np.newaxis, ...]
77
+ xcp_pred = xcp_model.predict(xcp_tensor, verbose=0)[0][0]
78
 
 
79
  eff_img = cv2.resize(face, (224, 224))
80
  eff_tensor = eff_pre(eff_img.astype(np.float32))[np.newaxis, ...]
81
+ eff_pred = eff_model.predict(eff_tensor, verbose=0)[0][0]
82
 
 
83
  avg_pred = (xcp_pred + eff_pred) / 2
84
  label = "Fake" if avg_pred > 0.5 else "Real"
85
 
86
+ # Grad-CAM (resize face correctly)
87
+ face_for_gradcam = cv2.resize(face, (299, 299))
88
+ face_for_gradcam = tf.convert_to_tensor(face_for_gradcam / 255.0, dtype=tf.float32)
89
+ cam_img = grad_cam(xcp_model, face_for_gradcam, (299, 299), xcp_pre)
90
 
91
  return label, cam_img
92