forked from feel3x/differential_4DGS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_blur.py
More file actions
83 lines (67 loc) · 3.04 KB
/
test_blur.py
File metadata and controls
83 lines (67 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import torch
import torch.nn.functional as F
import math
def gaussian_blur_old(img: torch.Tensor, kernel_size: int) -> torch.Tensor:
"""Old incorrect separable convolution"""
if kernel_size <= 0:
return img
# Create Gaussian kernel
sigma = (kernel_size - 1) / 6.0
x = torch.arange(kernel_size, dtype=torch.float32, device=img.device) - (kernel_size-1)/2
kernel_1d = torch.exp(-x**2 / (2*sigma**2))
kernel_1d /= kernel_1d.sum()
kernel_2d = torch.outer(kernel_1d, kernel_1d).view(1, 1, kernel_size, kernel_size)
# Apply separable convolution for efficiency
channels = img.shape[-3]
kernel_2d = kernel_2d.expand(channels, 1, kernel_size, kernel_size)
padding = kernel_size // 2
return F.conv2d(
F.conv2d(img, kernel_2d, padding=padding, groups=channels),
kernel_2d.transpose(2, 3),
padding=padding,
groups=channels
)
def gaussian_blur_new(img: torch.Tensor, kernel_size: int) -> torch.Tensor:
"""New correct separable convolution"""
if kernel_size <= 0:
return img
# Create Gaussian kernel
sigma = (kernel_size - 1) / 6.0
x = torch.arange(kernel_size, dtype=torch.float32, device=img.device) - (kernel_size-1)/2
kernel_1d = torch.exp(-x**2 / (2*sigma**2))
kernel_1d /= kernel_1d.sum()
# Apply separable convolution for efficiency
channels = img.shape[-3]
padding = kernel_size // 2
# Horizontal pass
kernel_h = kernel_1d.view(1, 1, 1, kernel_size).expand(channels, 1, 1, kernel_size)
img = F.conv2d(img, kernel_h, padding=(0, padding), groups=channels)
# Vertical pass
kernel_v = kernel_1d.view(1, 1, kernel_size, 1).expand(channels, 1, kernel_size, 1)
img = F.conv2d(img, kernel_v, padding=(padding, 0), groups=channels)
return img
# Test
if __name__ == "__main__":
# Create test image
device = 'cuda' if torch.cuda.is_available() else 'cpu'
img = torch.randn(1, 3, 64, 64).to(device)
kernel_size = 5
# Apply both blurs
blurred_old = gaussian_blur_old(img, kernel_size)
blurred_new = gaussian_blur_new(img, kernel_size)
# Compare
diff = torch.abs(blurred_old - blurred_new).mean()
print(f"Mean difference between old and new blur: {diff.item()}")
# Also compare with standard 2D convolution
sigma = (kernel_size - 1) / 6.0
x = torch.arange(kernel_size, dtype=torch.float32, device=img.device) - (kernel_size-1)/2
kernel_1d = torch.exp(-x**2 / (2*sigma**2))
kernel_1d /= kernel_1d.sum()
kernel_2d = torch.outer(kernel_1d, kernel_1d).view(1, 1, kernel_size, kernel_size)
channels = img.shape[-3]
kernel_2d = kernel_2d.expand(channels, 1, kernel_size, kernel_size)
padding = kernel_size // 2
blurred_2d = F.conv2d(img, kernel_2d, padding=padding, groups=channels)
diff_new_2d = torch.abs(blurred_new - blurred_2d).mean()
print(f"Mean difference between new separable and 2D convolution: {diff_new_2d.item()}")
print("Test completed. New blur should match 2D convolution closely.")