You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在FGSM攻击函数中为生成的对抗样本添加中值滤波?

Adding Median Filtering to FGSM Adversarial Samples

Got it, let's break this down. You want to apply median filtering right after generating your FGSM adversarial samples—here's how to do it cleanly, either by modifying your existing attack function or handling it as a separate modular step.

First, Grab the Right Tool

PyTorch's torchvision library has a built-in, optimized median filter function that works seamlessly with tensor inputs, so we'll use that. Start by importing it:

from torchvision.transforms.functional import median_filter

Option 1: Modify the FGSM Function (With Optional Filtering)

This approach adds a toggle for median filtering directly to your attack function, so you can easily switch it on/off for comparisons. We'll also add a parameter to control the filter kernel size (3x3 is standard, but you can adjust it):

def fgsm_attack(image, epsilon, data_grad, apply_median_filter=False, filter_kernel_size=3):
    # Original FGSM attack logic
    sign_data_grad = data_grad.sign()
    perturbed_image = image + epsilon * sign_data_grad
    perturbed_image = torch.clamp(perturbed_image, 0, 1)
    
    # Apply median filtering if enabled
    if apply_median_filter:
        # median_filter handles both single images (C, H, W) and batches (B, C, H, W)
        perturbed_image = median_filter(perturbed_image, kernel_size=filter_kernel_size)
        # Filtering might nudge values slightly outside 0-1, so re-clamp to keep data valid
        perturbed_image = torch.clamp(perturbed_image, 0, 1)
    
    return perturbed_image

To use it with filtering, just set apply_median_filter=True when calling:

# Generate adversarial sample with median filtering
adv_image_filtered = fgsm_attack(your_image, 0.03, your_data_grad, apply_median_filter=True)

Option 2: Apply Filtering Separately (Keep FGSM Function Clean)

If you want to keep your original FGSM function untouched, just run the filter on the output afterward:

# Generate raw adversarial sample using your original function
adv_image_raw = fgsm_attack(your_image, 0.03, your_data_grad)

# Apply median filtering post-attack
adv_image_filtered = median_filter(adv_image_raw, kernel_size=3)
adv_image_filtered = torch.clamp(adv_image_filtered, 0, 1)

Quick Notes

  • Kernel Size: Adjust filter_kernel_size to make the filter more/less aggressive (e.g., 5x5 for stronger smoothing).
  • Data Compatibility: The median_filter function works with both single-image tensors (shape (C, H, W)) and batch tensors (shape (B, C, H, W)), so it fits most standard PyTorch image pipelines.
  • Re-Clamping: We re-run torch.clamp after filtering because median smoothing can occasionally push pixel values just outside the 0-1 range, which would break downstream model inputs.

内容的提问来源于stack exchange,提问作者user14419988

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 14:27:26