canny边缘loss

Loss=L边缘+L全局
在这里插入图片描述

通过canny得出预测图像与mask图像的边缘,然后计算损失。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
bce_loss = nn.BCELoss(size_average=True)  #二分类交叉熵

def opencv(images):
for i in range(images.shape[0]):
image = images[i, 0, :, :]
image = image // 0.5000001 * 255 # 二值化
image_2 = image.cpu().detach().numpy()
image_2 = image_2.astype(np.uint8)
img = cv2.Canny(image_2, 30, 150)
img = img.astype(np.float32)
img = torch.from_numpy(img)
img.type = torch.float32
if i != 0:
if i != 1:
img_final = torch.cat((img_final, img), 0)
else:
img_final = torch.cat((img_first, img), 0)
else:
img_first = img
return img_final / 255

loss=bce_loss(opencv(pre),opencv(label))

两种方法加上这个边缘Loss:
1.每一种输出都算上
2.只算一种输出