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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -58
app.py CHANGED
@@ -8,13 +8,11 @@ from tensorflow.keras.applications.xception import preprocess_input as xcp_pre
8
  from tensorflow.keras.applications.efficientnet import preprocess_input as eff_pre
9
  from huggingface_hub import hf_hub_download
10
  from mtcnn import MTCNN
11
- import matplotlib.pyplot as plt
12
 
13
- # Download models
14
  xcp_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-detector", filename="xception_model.h5")
15
  eff_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-detector", filename="efficientnet_model.h5")
16
 
17
- # Load models
18
  xcp_model = load_model(xcp_path)
19
  eff_model = load_model(eff_path)
20
 
@@ -25,80 +23,37 @@ def extract_face(image):
25
  faces = detector.detect_faces(image)
26
  if not faces:
27
  return None
28
- x, y, w, h = faces[0]['box']
29
  x, y = max(0, x), max(0, y)
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
39
- break
40
-
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)
59
- heatmap = heatmap / (tf.reduce_max(heatmap) + 1e-6)
60
- heatmap = cv2.resize(heatmap.numpy(), size)
61
- heatmap = np.uint8(255 * heatmap)
62
- heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
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
-
93
  interface = gr.Interface(
94
  fn=predict,
95
- inputs=gr.Image(type="numpy", label="image"),
96
- outputs=[
97
- gr.Label(label="Prediction"),
98
- gr.Image(type="numpy", label="Grad-CAM")
99
- ],
100
- title="Deepfake Image Detector (with Grad-CAM)",
101
- description="Upload an image. We detect the face, classify using an ensemble (Xception + EfficientNetB4), and explain the prediction using Grad-CAM on Xception."
102
  )
103
 
104
  interface.launch()
 
8
  from tensorflow.keras.applications.efficientnet import preprocess_input as eff_pre
9
  from huggingface_hub import hf_hub_download
10
  from mtcnn import MTCNN
 
11
 
12
+ # Load models
13
  xcp_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-detector", filename="xception_model.h5")
14
  eff_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-detector", filename="efficientnet_model.h5")
15
 
 
16
  xcp_model = load_model(xcp_path)
17
  eff_model = load_model(eff_path)
18
 
 
23
  faces = detector.detect_faces(image)
24
  if not faces:
25
  return None
26
+ x, y, w, h = faces[0]["box"]
27
  x, y = max(0, x), max(0, y)
28
  return image[y:y+h, x:x+w]
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  def predict(image):
31
  face = extract_face(image)
32
  if face is None:
33
+ return "No face detected"
34
 
35
+ # Xception
36
  xcp_img = cv2.resize(face, (299, 299))
37
  xcp_tensor = xcp_pre(xcp_img.astype(np.float32))[np.newaxis, ...]
38
  xcp_pred = xcp_model.predict(xcp_tensor, verbose=0)[0][0]
39
 
40
+ # EfficientNet
41
  eff_img = cv2.resize(face, (224, 224))
42
  eff_tensor = eff_pre(eff_img.astype(np.float32))[np.newaxis, ...]
43
  eff_pred = eff_model.predict(eff_tensor, verbose=0)[0][0]
44
 
45
+ # Ensemble
46
+ avg_pred = (xcp_pred + eff_pred) / 2.0
47
  label = "Fake" if avg_pred > 0.5 else "Real"
48
+ return label
49
 
50
+ # Gradio interface
 
 
 
 
 
 
51
  interface = gr.Interface(
52
  fn=predict,
53
+ inputs=gr.Image(type="numpy", label="Upload an image"),
54
+ outputs=gr.Label(label="Prediction"),
55
+ title="Deepfake Image Detector",
56
+ description="Upload an image. We detect the face and classify it using an ensemble of Xception + EfficientNetB4."
 
 
 
57
  )
58
 
59
  interface.launch()