Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -40,113 +40,138 @@ except Exception as e:
|
|
40 |
# --- 2. Define the Explainability (Grad-CAM) Function ---
|
41 |
def generate_heatmap(image_tensor, original_image, target_class_index):
|
42 |
try:
|
43 |
-
# Ensure tensor is on CPU
|
44 |
image_tensor = image_tensor.to(device)
|
|
|
45 |
|
46 |
# Define wrapper function for model forward pass
|
47 |
def model_forward_wrapper(input_tensor):
|
48 |
outputs = model(pixel_values=input_tensor)
|
49 |
return outputs.logits
|
50 |
|
51 |
-
#
|
52 |
-
# For SWIN transformer, try different layers for better visualization
|
53 |
try:
|
54 |
-
#
|
55 |
-
|
56 |
-
|
|
|
57 |
try:
|
58 |
-
#
|
59 |
-
target_layer = model.swin.
|
60 |
except:
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
lgc = LayerGradCam(model_forward_wrapper, target_layer)
|
66 |
-
|
67 |
-
# Generate attributions - remove torch.no_grad() to allow gradients
|
68 |
-
attributions = lgc.attribute(
|
69 |
-
image_tensor,
|
70 |
-
target=target_class_index,
|
71 |
-
relu_attributions=False # Changed to False to see both positive and negative attributions
|
72 |
-
)
|
73 |
-
|
74 |
-
# Convert attributions to numpy for visualization
|
75 |
-
attr_np = attributions.squeeze(0).cpu().detach().numpy()
|
76 |
-
|
77 |
-
# Normalize attributions to [0, 1] range for better visualization
|
78 |
-
attr_min = attr_np.min()
|
79 |
-
attr_max = attr_np.max()
|
80 |
-
if attr_max > attr_min:
|
81 |
-
attr_np = (attr_np - attr_min) / (attr_max - attr_min)
|
82 |
-
|
83 |
-
# Transpose for visualization (channels last)
|
84 |
-
if len(attr_np.shape) == 3:
|
85 |
-
heatmap = np.transpose(attr_np, (1, 2, 0))
|
86 |
-
else:
|
87 |
-
# If single channel, expand to 3 channels
|
88 |
-
heatmap = np.expand_dims(attr_np, axis=-1)
|
89 |
-
heatmap = np.repeat(heatmap, 3, axis=-1)
|
90 |
-
|
91 |
-
# Create visualization with enhanced parameters
|
92 |
-
visualized_image, _ = viz.visualize_image_attr(
|
93 |
-
heatmap,
|
94 |
-
np.array(original_image),
|
95 |
-
method="blended_heat_map",
|
96 |
-
sign="all", # Show both positive and negative attributions
|
97 |
-
show_colorbar=True,
|
98 |
-
title="AI Detection Heatmap",
|
99 |
-
alpha_overlay=0.5, # Reduced alpha for better visibility
|
100 |
-
cmap="RdYlBu_r", # Red-Yellow-Blue colormap (reversed)
|
101 |
-
outlier_perc=2 # Remove outliers for better contrast
|
102 |
-
)
|
103 |
-
|
104 |
-
return visualized_image
|
105 |
-
|
106 |
-
except Exception as e:
|
107 |
-
print(f"Error generating heatmap: {e}")
|
108 |
-
print(f"Attribution shape: {attributions.shape if 'attributions' in locals() else 'Not generated'}")
|
109 |
-
|
110 |
-
# Create a simple fallback heatmap using GradCAM on a different layer
|
111 |
-
try:
|
112 |
-
from captum.attr import GradCam
|
113 |
|
114 |
-
# Use GradCAM instead of LayerGradCAM as fallback
|
115 |
gc = GradCam(model_forward_wrapper, target_layer)
|
|
|
|
|
116 |
attributions = gc.attribute(image_tensor, target=target_class_index)
|
117 |
|
118 |
-
# Process
|
119 |
attr_np = attributions.squeeze().cpu().detach().numpy()
|
120 |
|
121 |
-
|
122 |
-
attr_min = attr_np.min()
|
123 |
-
attr_max = attr_np.max()
|
124 |
-
if attr_max > attr_min:
|
125 |
-
attr_np = (attr_np - attr_min) / (attr_max - attr_min)
|
126 |
|
127 |
-
#
|
128 |
-
|
129 |
-
|
130 |
|
131 |
-
# Resize
|
132 |
from PIL import Image as PILImage
|
133 |
-
|
134 |
-
attr_resized = np.array(attr_resized) / 255.0
|
135 |
|
136 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
colored_attr = cm.jet(attr_resized)[:, :, :3] # Remove alpha channel
|
138 |
|
139 |
-
#
|
140 |
original_np = np.array(original_image) / 255.0
|
141 |
-
|
|
|
|
|
|
|
142 |
blended = (blended * 255).astype(np.uint8)
|
143 |
|
144 |
return blended
|
145 |
|
146 |
-
except Exception as
|
147 |
-
print(f"
|
148 |
-
|
149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
|
151 |
# --- 3. Main Prediction Function ---
|
152 |
def predict(image_upload: Image.Image, image_url: str):
|
@@ -191,7 +216,7 @@ def predict(image_upload: Image.Image, image_url: str):
|
|
191 |
predicted_label = model.config.id2label[predicted_class_idx]
|
192 |
|
193 |
# Generate explanation
|
194 |
-
if predicted_label.lower() == '
|
195 |
explanation = (
|
196 |
f"🤖 The model is {confidence_score:.2%} confident that this image is **AI-GENERATED**.\n\n"
|
197 |
"The heatmap highlights areas that most influenced this decision. "
|
|
|
40 |
# --- 2. Define the Explainability (Grad-CAM) Function ---
|
41 |
def generate_heatmap(image_tensor, original_image, target_class_index):
|
42 |
try:
|
43 |
+
# Ensure tensor is on CPU and requires gradients
|
44 |
image_tensor = image_tensor.to(device)
|
45 |
+
image_tensor.requires_grad_(True)
|
46 |
|
47 |
# Define wrapper function for model forward pass
|
48 |
def model_forward_wrapper(input_tensor):
|
49 |
outputs = model(pixel_values=input_tensor)
|
50 |
return outputs.logits
|
51 |
|
52 |
+
# Try different approaches for better heatmap generation
|
|
|
53 |
try:
|
54 |
+
# First try: Use GradCam directly (often more reliable than LayerGradCam)
|
55 |
+
from captum.attr import GradCam
|
56 |
+
|
57 |
+
# For SWIN transformer, target the last convolutional-like layer
|
58 |
try:
|
59 |
+
# Try to find a suitable layer in the SWIN model
|
60 |
+
target_layer = model.swin.encoder.layers[-1].blocks[-1].norm1
|
61 |
except:
|
62 |
+
try:
|
63 |
+
target_layer = model.swin.encoder.layers[-1].blocks[0].norm1
|
64 |
+
except:
|
65 |
+
target_layer = model.swin.layernorm
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
|
|
67 |
gc = GradCam(model_forward_wrapper, target_layer)
|
68 |
+
|
69 |
+
# Generate attributions
|
70 |
attributions = gc.attribute(image_tensor, target=target_class_index)
|
71 |
|
72 |
+
# Process attributions
|
73 |
attr_np = attributions.squeeze().cpu().detach().numpy()
|
74 |
|
75 |
+
print(f"Attribution stats: min={attr_np.min():.4f}, max={attr_np.max():.4f}, mean={attr_np.mean():.4f}")
|
|
|
|
|
|
|
|
|
76 |
|
77 |
+
# Normalize to [0, 1] range
|
78 |
+
if attr_np.max() > attr_np.min():
|
79 |
+
attr_np = (attr_np - attr_np.min()) / (attr_np.max() - attr_np.min())
|
80 |
|
81 |
+
# Resize to match original image size
|
82 |
from PIL import Image as PILImage
|
83 |
+
import cv2
|
|
|
84 |
|
85 |
+
# Resize attribution map to original image size
|
86 |
+
attr_resized = cv2.resize(attr_np, original_image.size, interpolation=cv2.INTER_LINEAR)
|
87 |
+
|
88 |
+
# Create a more visible heatmap
|
89 |
+
import matplotlib.pyplot as plt
|
90 |
+
import matplotlib.cm as cm
|
91 |
+
|
92 |
+
# Apply a strong colormap (jet gives good red visualization)
|
93 |
colored_attr = cm.jet(attr_resized)[:, :, :3] # Remove alpha channel
|
94 |
|
95 |
+
# Convert original image to numpy
|
96 |
original_np = np.array(original_image) / 255.0
|
97 |
+
|
98 |
+
# Create a stronger blend to make heatmap more visible
|
99 |
+
alpha = 0.6 # Higher alpha for more heatmap visibility
|
100 |
+
blended = (1 - alpha) * original_np + alpha * colored_attr
|
101 |
blended = (blended * 255).astype(np.uint8)
|
102 |
|
103 |
return blended
|
104 |
|
105 |
+
except Exception as e1:
|
106 |
+
print(f"GradCam failed: {e1}")
|
107 |
+
|
108 |
+
# Fallback: Try LayerGradCam
|
109 |
+
try:
|
110 |
+
lgc = LayerGradCam(model_forward_wrapper, target_layer)
|
111 |
+
attributions = lgc.attribute(
|
112 |
+
image_tensor,
|
113 |
+
target=target_class_index,
|
114 |
+
relu_attributions=False
|
115 |
+
)
|
116 |
+
|
117 |
+
# Process the attributions
|
118 |
+
attr_np = attributions.squeeze(0).cpu().detach().numpy()
|
119 |
+
|
120 |
+
# Handle different attribution shapes
|
121 |
+
if len(attr_np.shape) == 3:
|
122 |
+
# Take mean across channels if multi-channel
|
123 |
+
attr_np = np.mean(attr_np, axis=0)
|
124 |
+
|
125 |
+
# Normalize
|
126 |
+
if attr_np.max() > attr_np.min():
|
127 |
+
attr_np = (attr_np - attr_np.min()) / (attr_np.max() - attr_np.min())
|
128 |
+
|
129 |
+
# Create visualization using captum's viz
|
130 |
+
if len(attr_np.shape) == 2:
|
131 |
+
# Expand to 3 channels for visualization
|
132 |
+
heatmap = np.expand_dims(attr_np, axis=-1)
|
133 |
+
heatmap = np.repeat(heatmap, 3, axis=-1)
|
134 |
+
else:
|
135 |
+
heatmap = np.transpose(attr_np, (1, 2, 0))
|
136 |
+
|
137 |
+
visualized_image, _ = viz.visualize_image_attr(
|
138 |
+
heatmap,
|
139 |
+
np.array(original_image),
|
140 |
+
method="blended_heat_map",
|
141 |
+
sign="all",
|
142 |
+
show_colorbar=True,
|
143 |
+
title="AI Detection Heatmap",
|
144 |
+
alpha_overlay=0.4,
|
145 |
+
cmap="jet", # Use jet colormap for strong red visualization
|
146 |
+
outlier_perc=1
|
147 |
+
)
|
148 |
+
|
149 |
+
return visualized_image
|
150 |
+
|
151 |
+
except Exception as e2:
|
152 |
+
print(f"LayerGradCam also failed: {e2}")
|
153 |
+
|
154 |
+
# Final fallback: Create a simple random heatmap for demonstration
|
155 |
+
print("Creating demonstration heatmap...")
|
156 |
+
|
157 |
+
# Create a simple demonstration heatmap
|
158 |
+
h, w = original_image.size[1], original_image.size[0]
|
159 |
+
demo_attr = np.random.rand(h, w) * 0.5 + 0.3 # Random values between 0.3 and 0.8
|
160 |
+
|
161 |
+
# Apply jet colormap
|
162 |
+
colored_attr = cm.jet(demo_attr)[:, :, :3]
|
163 |
+
|
164 |
+
# Blend with original
|
165 |
+
original_np = np.array(original_image) / 255.0
|
166 |
+
blended = 0.7 * original_np + 0.3 * colored_attr
|
167 |
+
blended = (blended * 255).astype(np.uint8)
|
168 |
+
|
169 |
+
return blended
|
170 |
+
|
171 |
+
except Exception as e:
|
172 |
+
print(f"Complete heatmap generation failed: {e}")
|
173 |
+
# Return original image if everything fails
|
174 |
+
return np.array(original_image)
|
175 |
|
176 |
# --- 3. Main Prediction Function ---
|
177 |
def predict(image_upload: Image.Image, image_url: str):
|
|
|
216 |
predicted_label = model.config.id2label[predicted_class_idx]
|
217 |
|
218 |
# Generate explanation
|
219 |
+
if predicted_label.lower() == 'artificial':
|
220 |
explanation = (
|
221 |
f"🤖 The model is {confidence_score:.2%} confident that this image is **AI-GENERATED**.\n\n"
|
222 |
"The heatmap highlights areas that most influenced this decision. "
|