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

Problem about calculation of loss #5

Closed
Retiina opened this issue Sep 11, 2019 · 2 comments
Closed

Problem about calculation of loss #5

Retiina opened this issue Sep 11, 2019 · 2 comments

Comments

@Retiina
Copy link

Retiina commented Sep 11, 2019

Dear author, I am really puzzled about the loss calculation in the following code segmentation in free_anchor_loss.py, could you explain to me please?

Thanks in advance!

with torch.set_grad_enabled(False):
    box_localization = self.box_coder.decode(box_regression_, anchors_.bbox)
    object_box_iou = boxlist_iou(
        targets_,
        BoxList(box_localization, anchors_.size, mode='xyxy')
    )
    H = object_box_iou.max(dim=1, keepdim=True).values.clamp(
        min=self.bbox_threshold + 1e-12)
    object_box_prob = (
            (object_box_iou - self.bbox_threshold) / (H - self.bbox_threshold)
    ).clamp(min=0, max=1)

    indices = torch.stack(
        [torch.arange(len(labels_)).type_as(labels_), labels_], dim=0)

    """
    to implement image_box_iou = torch.sparse.max(
                      torch.sparse_coo_tensor(indices, object_box_iou), dim=0
                 )
    """
    # start
    indices = torch.nonzero(torch.sparse.sum(
        torch.sparse_coo_tensor(indices, object_box_prob), dim=0
    ).to_dense()).t_()

    if indices.numel() == 0:
        image_box_prob = torch.zeros(anchors_.bbox.size(0),
                                     self.num_classes).type_as(object_box_prob)
    else:
        nonzero_box_prob = torch.where(
            (labels_.unsqueeze(dim=-1) == indices[0]),
            object_box_prob[:, indices[1]],
            torch.tensor([0]).type_as(object_box_prob)
        ).max(dim=0).values

        image_box_prob = torch.sparse_coo_tensor(
            indices.flip([0]), nonzero_box_prob,
            size=(anchors_.bbox.size(0), self.num_classes)
        ).to_dense()
    # end
@zhangxiaosong18
Copy link
Owner

This part of the code is really complicated, I have added more explanations and correspondences with formulas, hope it can help you.

with torch.set_grad_enabled(False):
    # box_localization: a_{j}^{loc}, shape: [j, 4]
    box_localization = self.box_coder.decode(box_regression_, anchors_.bbox)

    # object_box_iou: IoU_{ij}^{loc}, shape: [i, j]
    object_box_iou = boxlist_iou(
        targets_,
        BoxList(box_localization, anchors_.size, mode='xyxy')
    )

    t1 = self.bbox_threshold
    t2 = object_box_iou.max(dim=1, keepdim=True).values.clamp(min=t1 + 1e-12)

    # object_box_prob: P{a_{j} -> b_{i}}, shape: [i, j]
    object_box_prob = (
        (object_box_iou - t1) / (t2 - t1)
    ).clamp(min=0, max=1)

    indices = torch.stack([torch.arange(len(labels_)).type_as(labels_), labels_], dim=0)

    # object_cls_box_prob: P{a_{j} -> b_{i}}, shape: [i, c, j]
    object_cls_box_prob = torch.sparse_coo_tensor(indices, object_box_prob)

    # image_box_iou: P{a_{j} \in A_{+}}, shape: [j, c]
    """
    from "start" to "end" implement:
    
    image_box_iou = torch.sparse.max(object_cls_box_prob, dim=0).t()
    
    """
    # start
    indices = torch.nonzero(torch.sparse.sum(
        object_cls_box_prob, dim=0
    ).to_dense()).t_()

    if indices.numel() == 0:
        image_box_prob = torch.zeros(anchors_.bbox.size(0), self.num_classes).type_as(object_box_prob)
    else:
        nonzero_box_prob = torch.where(
            (labels_.unsqueeze(dim=-1) == indices[0]),
            object_box_prob[:, indices[1]],
            torch.tensor([0]).type_as(object_box_prob)
        ).max(dim=0).values

        image_box_prob = torch.sparse_coo_tensor(
            indices.flip([0]), nonzero_box_prob,
            size=(anchors_.bbox.size(0), self.num_classes)
        ).to_dense()
    # end

@Retiina
Copy link
Author

Retiina commented Sep 12, 2019

Thank you again sir!

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

2 participants