Satwickchikkala1 commited on
Commit
2c503bf
·
verified ·
1 Parent(s): 9d3e381

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -11
app.py CHANGED
@@ -11,13 +11,15 @@ try:
11
  model = tf.keras.models.load_model("./model/deepfake_mobilenet_model.h5")
12
  except Exception as e:
13
  print(f"Error loading model. Make sure the path is correct. Error: {e}")
 
14
  inputs = tf.keras.Input(shape=(224, 224, 3))
15
  outputs = tf.keras.layers.Dense(1, activation="sigmoid")(tf.keras.layers.GlobalAveragePooling2D()(inputs))
16
  model = tf.keras.Model(inputs, outputs)
17
 
18
  # ==============================================================================
19
- # --- Grad-CAM Heatmap Generation Functions (with final fix) ---
20
  # ==============================================================================
 
21
  def get_last_conv_layer_name(model):
22
  """Finds the name of the last convolutional layer in the model."""
23
  for layer in reversed(model.layers):
@@ -26,20 +28,22 @@ def get_last_conv_layer_name(model):
26
  raise ValueError("Could not find a conv layer in the model")
27
 
28
  def make_gradcam_heatmap(img_array, model, last_conv_layer_name):
29
- """Generates the Grad-CAM heatmap."""
30
  grad_model = tf.keras.models.Model(
31
  model.inputs, [model.get_layer(last_conv_layer_name).output, model.output]
32
  )
33
  with tf.GradientTape() as tape:
34
  last_conv_layer_output, preds = grad_model([img_array], training=False)
35
- class_channel = preds[0]
 
 
 
 
36
 
37
  grads = tape.gradient(class_channel, last_conv_layer_output)
38
 
39
- # <-- FIX: Add a safety check in case the gradient does not exist.
40
  if grads is None:
41
- print("Warning: Gradient is None. Cannot compute heatmap. Returning a blank map.")
42
- # Return a blank (black) map of the same size as the feature map.
43
  h, w = last_conv_layer_output.shape[1:3]
44
  return np.zeros((h, w), dtype=np.float32)
45
 
@@ -51,12 +55,21 @@ def make_gradcam_heatmap(img_array, model, last_conv_layer_name):
51
  return heatmap.numpy()
52
 
53
  def superimpose_gradcam(original_img_pil, heatmap):
54
- """Overlays the heatmap on the original image."""
55
  original_img = np.array(original_img_pil)
 
56
  heatmap = cv2.resize(heatmap, (original_img.shape[1], original_img.shape[0]))
 
 
 
 
57
  heatmap = np.uint8(255 * heatmap)
58
- heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
 
 
 
59
  superimposed_img = cv2.addWeighted(original_img, 0.6, heatmap, 0.4, 0)
 
60
  return Image.fromarray(superimposed_img)
61
 
62
  # ==============================================================================
@@ -65,15 +78,16 @@ def superimpose_gradcam(original_img_pil, heatmap):
65
  last_conv_layer_name = get_last_conv_layer_name(model)
66
 
67
  def predict_and_visualize(img):
68
- """Performs prediction and generates the Grad-CAM heatmap."""
69
  try:
70
- if img is None: # Handle case where user clears the image
71
  return None, None
72
 
73
  img_resized = img.resize((224, 224))
74
  img_array = image.img_to_array(img_resized) / 255.0
75
  img_array_expanded = np.expand_dims(img_array, axis=0)
76
 
 
77
  prediction = model.predict(img_array_expanded, verbose=0)[0][0]
78
  real_conf = float(prediction)
79
  fake_conf = float(1 - prediction)
@@ -104,7 +118,7 @@ gr.Interface(
104
  title="✨ Deepfake Image Detector with Visual Explanation ✨",
105
  description="""
106
  **Detect whether an uploaded image is Real or AI-Generated (Deepfake).**
107
- The confidence bars show the model's certainty, and the heatmap highlights the regions the model focused on (red = most important).
108
  """,
109
  theme="default"
110
  ).launch()
 
11
  model = tf.keras.models.load_model("./model/deepfake_mobilenet_model.h5")
12
  except Exception as e:
13
  print(f"Error loading model. Make sure the path is correct. Error: {e}")
14
+ # Fallback dummy model for deployment debugging
15
  inputs = tf.keras.Input(shape=(224, 224, 3))
16
  outputs = tf.keras.layers.Dense(1, activation="sigmoid")(tf.keras.layers.GlobalAveragePooling2D()(inputs))
17
  model = tf.keras.Model(inputs, outputs)
18
 
19
  # ==============================================================================
20
+ # --- Grad-CAM Heatmap Generation Functions (Improved Logic) ---
21
  # ==============================================================================
22
+
23
  def get_last_conv_layer_name(model):
24
  """Finds the name of the last convolutional layer in the model."""
25
  for layer in reversed(model.layers):
 
28
  raise ValueError("Could not find a conv layer in the model")
29
 
30
  def make_gradcam_heatmap(img_array, model, last_conv_layer_name):
31
+ """Generates the Grad-CAM heatmap with robust logic."""
32
  grad_model = tf.keras.models.Model(
33
  model.inputs, [model.get_layer(last_conv_layer_name).output, model.output]
34
  )
35
  with tf.GradientTape() as tape:
36
  last_conv_layer_output, preds = grad_model([img_array], training=False)
37
+
38
+ # <-- IMPROVEMENT: Use a robust method to get the prediction value.
39
+ # This works correctly regardless of tensor shape (e.g., (1,1) vs (1,))
40
+ # and fixes the root cause of previous errors and poor quality.
41
+ class_channel = tf.reduce_mean(preds[0])
42
 
43
  grads = tape.gradient(class_channel, last_conv_layer_output)
44
 
 
45
  if grads is None:
46
+ print("Warning: Gradient is None. Cannot compute heatmap.")
 
47
  h, w = last_conv_layer_output.shape[1:3]
48
  return np.zeros((h, w), dtype=np.float32)
49
 
 
55
  return heatmap.numpy()
56
 
57
  def superimpose_gradcam(original_img_pil, heatmap):
58
+ """Overlays the heatmap on the original image with visual improvements."""
59
  original_img = np.array(original_img_pil)
60
+
61
  heatmap = cv2.resize(heatmap, (original_img.shape[1], original_img.shape[0]))
62
+
63
+ # <-- IMPROVEMENT: Add a blur to make the heatmap smoother.
64
+ heatmap = cv2.GaussianBlur(heatmap, (15, 15), 0)
65
+
66
  heatmap = np.uint8(255 * heatmap)
67
+
68
+ # <-- IMPROVEMENT: Use a better color map.
69
+ heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_VIRIDIS)
70
+
71
  superimposed_img = cv2.addWeighted(original_img, 0.6, heatmap, 0.4, 0)
72
+
73
  return Image.fromarray(superimposed_img)
74
 
75
  # ==============================================================================
 
78
  last_conv_layer_name = get_last_conv_layer_name(model)
79
 
80
  def predict_and_visualize(img):
81
+ """Performs prediction and generates the improved Grad-CAM heatmap."""
82
  try:
83
+ if img is None:
84
  return None, None
85
 
86
  img_resized = img.resize((224, 224))
87
  img_array = image.img_to_array(img_resized) / 255.0
88
  img_array_expanded = np.expand_dims(img_array, axis=0)
89
 
90
+ # The prediction logic remains unchanged and should work as before
91
  prediction = model.predict(img_array_expanded, verbose=0)[0][0]
92
  real_conf = float(prediction)
93
  fake_conf = float(1 - prediction)
 
118
  title="✨ Deepfake Image Detector with Visual Explanation ✨",
119
  description="""
120
  **Detect whether an uploaded image is Real or AI-Generated (Deepfake).**
121
+ The confidence bars show the model's certainty, and the heatmap highlights the regions the model focused on.
122
  """,
123
  theme="default"
124
  ).launch()