import torch import torch.nn.functional as F def gaussian_blur_2d(x, radius: float): if radius <= 0: return x sigma = radius * 0.5 k = int(max(3, radius * 2 + 1)) if k % 2 == 0: k += 1 device = x.device coords = torch.arange(k, device=device) - k // 2 g = torch.exp(-(coords ** 2) / (2 * sigma ** 2)) g = g / g.sum() # reshape for separable conv g_h = g.view(1, 1, 1, k) g_v = g.view(1, 1, k, 1) x = x.unsqueeze(1) # [B,1,H,W] # IMPORTANT: symmetric SAME padding pad = k // 2 x = F.conv2d(x, g_h, padding=(0, pad)) x = F.conv2d(x, g_v, padding=(pad, 0)) return x.squeeze(1)