SML中的边界抑制以及高斯平滑
边界平滑抑制类
class BoundarySuppressionWithSmoothing(nn.Module):
"""
Apply boundary suppression and dilated smoothing 边界抑制,扩张平滑
"""
初始化
def __init__(self, boundary_suppression=True, boundary_width=4, boundary_iteration=4,
dilated_smoothing=True, kernel_size=7, dilation=6):
定义一些参数
super(BoundarySuppressionWithSmoothing, self).__init__()
self.kernel_size = kernel_size # 卷积核大小
self.dilation = dilation # 扩张
self.boundary_suppression = boundary_suppression # 边界抑制
self.boundary_width = boundary_width # 边界宽度
self.boundary_iteration = boundary_iteration # 边界迭代
创建高斯核
sigma = 1.0
size = 7
# function为二维高斯分布的概率密度函数
gaussian_kernel = np.fromfunction(lambda x, y:
(1/(2*math.pi*sigma**2)) * math.e ** ((-1*((x-(size-1)/2)**2+(y-(size-1)/2)**2))/(2*sigma**2)),
(size, size)) # 构造高斯核 (7,7) 3 * sigma + 1
gaussian_kernel /= np.sum(gaussian_kernel) # 除以高斯核中所有元素之和(加权平均,避免图像像素溢出)
gaussian_kernel = torch.Tensor(gaussian_kernel).unsqueeze(0).unsqueeze(0)
self.dilated_smoothing = dilated_smoothing # 扩张平滑
l a m b d a ( x , y ) = 1 2 ∗ π ∗ σ 2 e x p ( − ( x − s i z e − 1 2 ) 2 + ( y − s i z e − 1 2 ) 2 2 ∗ σ 2 ) lambda(x,y) = \frac{1}{2 * \pi * \sigma^2} exp(-\frac{(x - \frac{size-1}{2})^2 + (y - \frac{size-1}{2})^2}{2 * \sigma^2}) lambda(x,y)