Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zero question #62

Open
MrChenDingding opened this issue Nov 4, 2023 · 0 comments
Open

zero question #62

MrChenDingding opened this issue Nov 4, 2023 · 0 comments

Comments

@MrChenDingding
Copy link

It is a binary classification problem. After using the boundary loss proposed by you, all categories are judged as the background in the output result of the network, that is, the output result is 0, and no segmentation area is found. What is the problem causing this?

This code is to find the loss function and distance graph

label = Image.open(os.path.join(os.path.join(self.label_path, image_name)))
label = np.array(label)
seg_label = np.eye(self.num_classes)[label.reshape([-1])]
seg_label = seg_label.reshape((int(self.input_shape[0]), int(self.input_shape[1]), self.num_classes))
seg_label = np.transpose(seg_label, [2, 0, 1])
dist_map = self.one_hot2dist(seg_label)

def one_hot2dist(self, seg_label: np.ndarray) -> np.ndarray:
"""
根据独热编码分割图像计算距离图,用于后续损失计算
:param seg:
:return:
"""
C: int = len(seg_label)

res = np.zeros_like(seg_label)
for c in range(C):
    posmask = seg_label[c].astype(np.bool)

    if posmask.any():
        negmask = ~posmask
        # print('negmask:', negmask)
        # print('distance(negmask):', distance(negmask))
        res[c] = distance(negmask) * negmask - (distance(posmask) - 1) * posmask
        # print('res[c]', res[c])
return res

out = model(img)

----------------------#

损失计算

----------------------#

if ce_loss:
ce = CE_Loss(out, seg_labels, weights)
if focal_loss:
focal = Focal_Loss(out, seg_labels, weights)
if dice_loss:
dice = DiceLoss(out, seg_labels, idc=[0, 1])
if boundary_loss:
boundary = SurfaceLoss(out, dist_maps, idc=idc)

loss = (ce + focal + dice) * (1 - alpha) + alpha * boundary

def SurfaceLoss(inputs, dist_maps, idc=[1]):
temp_inputs = torch.softmax(inputs, dim=1)

temp_inputs = torch.softmax(inputs, dim=1)[:, idc, ...]

temp_dist_maps = dist_maps[:, idc, ...]

multipled = temp_inputs * temp_dist_maps

loss = multipled.mean()

return loss
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant