Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -216,7 +216,7 @@ def predict(image, brightness, contrast, hue, overlay_image, alpha, adversarial_
|
|
216 |
|
217 |
# Apply preprocessing
|
218 |
processed = apply_filters(image, brightness, contrast, hue)
|
219 |
-
final_image = superimpose_images(processed, overlay_image, alpha)
|
220 |
|
221 |
# Generate adversarial if enabled
|
222 |
if adversarial_switch:
|
@@ -228,25 +228,26 @@ def predict(image, brightness, contrast, hue, overlay_image, alpha, adversarial_
|
|
228 |
final_display = final_display.resize(orig_size) # Resize back to original size
|
229 |
model_input = transform_image(final_display)
|
230 |
else:
|
231 |
-
# Step 1: Resize to 224x224
|
232 |
resized_image = final_image.resize((224, 224))
|
233 |
-
# Step 2: Resize back to original size
|
234 |
final_display = resized_image.resize(orig_size)
|
235 |
-
# Model gets the 224x224 resized version
|
236 |
model_input = transform_image(resized_image)
|
237 |
|
238 |
# Get predictions
|
239 |
with torch.no_grad():
|
240 |
output = model(model_input)
|
241 |
probs = F.softmax(output, dim=1).cpu().numpy()[0]
|
|
|
|
|
|
|
|
|
242 |
|
243 |
-
# Generate Grad-CAM
|
244 |
heatmap, _ = gradcam.generate(model_input)
|
245 |
final_np = np.array(final_display)
|
246 |
heatmap = cv2.resize(heatmap, final_np.shape[:2][::-1])
|
247 |
heatmap = cv2.applyColorMap(np.uint8(255 * heatmap), cv2.COLORMAP_JET)
|
248 |
superimposed = cv2.addWeighted(cv2.cvtColor(heatmap, cv2.COLOR_BGR2RGB), 0.5, final_np, 0.5, 0)
|
249 |
-
|
250 |
# Create plot
|
251 |
fig, ax = plt.subplots(figsize=(6, 4))
|
252 |
top5_idx = np.argsort(probs)[-5:][::-1]
|
@@ -257,7 +258,6 @@ def predict(image, brightness, contrast, hue, overlay_image, alpha, adversarial_
|
|
257 |
|
258 |
return final_display, Image.fromarray(superimposed), fig
|
259 |
|
260 |
-
|
261 |
# Gradio Interface
|
262 |
with gr.Blocks() as interface:
|
263 |
gr.Markdown("<h2 style='text-align: center;'>ResNet Classifier with Adversarial Attacks</h2>")
|
|
|
216 |
|
217 |
# Apply preprocessing
|
218 |
processed = apply_filters(image, brightness, contrast, hue)
|
219 |
+
final_image = superimpose_images(processed, overlay_image, alpha)
|
220 |
|
221 |
# Generate adversarial if enabled
|
222 |
if adversarial_switch:
|
|
|
228 |
final_display = final_display.resize(orig_size) # Resize back to original size
|
229 |
model_input = transform_image(final_display)
|
230 |
else:
|
|
|
231 |
resized_image = final_image.resize((224, 224))
|
|
|
232 |
final_display = resized_image.resize(orig_size)
|
|
|
233 |
model_input = transform_image(resized_image)
|
234 |
|
235 |
# Get predictions
|
236 |
with torch.no_grad():
|
237 |
output = model(model_input)
|
238 |
probs = F.softmax(output, dim=1).cpu().numpy()[0]
|
239 |
+
|
240 |
+
# Reset Grad-CAM activations before generating new heatmap
|
241 |
+
gradcam.activations.clear()
|
242 |
+
gradcam.gradients.clear()
|
243 |
|
244 |
+
# Generate Grad-CAM
|
245 |
heatmap, _ = gradcam.generate(model_input)
|
246 |
final_np = np.array(final_display)
|
247 |
heatmap = cv2.resize(heatmap, final_np.shape[:2][::-1])
|
248 |
heatmap = cv2.applyColorMap(np.uint8(255 * heatmap), cv2.COLORMAP_JET)
|
249 |
superimposed = cv2.addWeighted(cv2.cvtColor(heatmap, cv2.COLOR_BGR2RGB), 0.5, final_np, 0.5, 0)
|
250 |
+
|
251 |
# Create plot
|
252 |
fig, ax = plt.subplots(figsize=(6, 4))
|
253 |
top5_idx = np.argsort(probs)[-5:][::-1]
|
|
|
258 |
|
259 |
return final_display, Image.fromarray(superimposed), fig
|
260 |
|
|
|
261 |
# Gradio Interface
|
262 |
with gr.Blocks() as interface:
|
263 |
gr.Markdown("<h2 style='text-align: center;'>ResNet Classifier with Adversarial Attacks</h2>")
|