Skip to content
This repository has been archived by the owner on Jul 2, 2021. It is now read-only.

mystery code about IoU calculate ? #303

Closed
machanic opened this issue Jun 26, 2017 · 4 comments
Closed

mystery code about IoU calculate ? #303

machanic opened this issue Jun 26, 2017 · 4 comments

Comments

@machanic
Copy link

in chainercv/chainercv/utils/bbox/bbox_iou.py

the below code is very hard to understand, Could you explain to me about how exactly this code works?

    if bbox_a.shape[1] != 4 or bbox_b.shape[1] != 4:
        raise IndexError
    xp = cuda.get_array_module(bbox_a)

    # top left
    tl = xp.maximum(bbox_a[:, None, :2], bbox_b[:, :2])
    # bottom right
    br = xp.minimum(bbox_a[:, None, 2:], bbox_b[:, 2:])

    area_i = xp.prod(br - tl, axis=2) * (tl < br).all(axis=2)
    area_a = xp.prod(bbox_a[:, 2:] - bbox_a[:, :2], axis=1)
    area_b = xp.prod(bbox_b[:, 2:] - bbox_b[:, :2], axis=1)
    return area_i / (area_a[:, None] + area_b - area_i)```
@yuyu2172
Copy link
Member

It calculates intersection over union between bounding boxes in bbox_a and bbox_b.
The formula to calculate IoU from a pair of bounding boxes can be found in the link below.

https://en.wikipedia.org/wiki/Jaccard_index

Perhaps, this is a little bit difficult to understand in the first hand because the code processes IoUs between collections of bounding boxes.

@machanic
Copy link
Author

A little bit? I must say you are really very powerful numpy user. very impressive code

@machanic
Copy link
Author

how to use numpy like you? do you have any learning numpy suggestion?

@yuyu2172
Copy link
Member

Thanks. This was a work by @Hakuyume .
It is always better to start from easy code and optimize it later.

If you have any more questions, please re-open this.

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

No branches or pull requests

2 participants