该损失关注图像纹理和边缘、支持Sobel、Prewitt、Roberts、Scharr四种算子。默认使用Sobel算子,并且会对输入图像在各通道维度上计算平均值(也就说,是在默认情况下,对于输入的RGB图像,会首先计算得出其灰度图像再计算损失)
import torch
import torch.nn as nn
import torch.nn.functional as F
class GradientLoss(nn.Module):
def __init__(self, operator="Sobel", channel_mean=True):
r"""
:param operator: in ['Sobel', 'Prewitt','Roberts','Scharr']
:param channel_mean: 是否在通道维度上计算均值
"""
super(GradientLoss, self).__init__()
assert operator in ['Sobel', 'Prewitt', 'Roberts', 'Scharr'], "Unsupported operator"
self.channel_mean = channel_mean
self.operators = {
"Sobel": {
'x': torch.tensor([[[[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]]], dtype=torch.float),
'y': torch.tensor([[[[-1, -2, -1], [0, 0, 0], [1, 2, 1]]]], dtype=torch.float)
},
"Prewitt": {
'x': torch.tensor([[[[1, 0, -1], [1, 0, -1], [1, 0, -1]]]], dtype=torch.float),
'y': torch.tensor([[[[-1, -1, -1], [0, 0, 0], [1, 1, 1]]]], dtype=torch.float)
},
"Roberts": {
'x': torch.tensor([[[[1, 0], [0, -1]]]], dtype=torch.float),
'y': torch.tensor([[[[0, -1], [1, 0]]]], dtype=torch.float)
},
"Scharr": {
'x': torch.tensor([[[[-3, 0, 3], [-10, 0, 10], [-3, 0, 3]]]], dtype=torch.float),
'y': torch.tensor([[[[-3, 10, -3], [0, 0, 0], [3, 10, 3]]]], dtype=torch.float)
},
}
self.op_x = self.operators[operator]['x']
self.op_y = self.operators[operator]['y']
def gradients(self, img_tensor):
op_x, op_y = self.op_x, self.op_y
if self.channel_mean:
img_tensor = img_tensor.mean(dim=1, keepdim=True)
groups = 1
else:
groups = img_tensor.shape[1]
op_x = op_x.repeat(groups, 1, 1, 1)
op_y = op_y.repeat(groups, 1, 1, 1)
grad_x = F.conv2d(img_tensor, op_x, groups=groups)
grad_y = F.conv2d(img_tensor, op_y, groups=groups)
return grad_x, grad_y
def forward(self, img1, img2):
grad_x1, grad_y1 = self.gradients(img1)
grad_x2, grad_y2 = self.gradients(img2)
diff_x = torch.abs(grad_x1 - grad_x2)
diff_y = torch.abs(grad_y1 - grad_y2)
total_loss = torch.mean(diff_x + diff_y)
return total_loss
if __name__ == '__main__':
x = torch.randn(5, 3, 32, 64)
print(GradientLoss()(x, x**2))