lilanxiao / Rotated_IoU

Differentiable IoU of rotated bounding boxes using Pytorch
MIT License
412 stars 62 forks source link

backpropagation when Rotate_IOU = 0 #9

Closed JJangD closed 3 years ago

JJangD commented 3 years ago

Hi, @lilanxiao

I have question about backpropagation when rotate_IOU value is zero, a case when the number of vertices that intersect or inside another box are less than 3 . How do you manage to backpropagate?

lilanxiao commented 3 years ago

Hi, @JJangD In this case, both the IoU value and its gradient are zero.

That is actually the problem if IoU is used directly as a loss: if there is no intersection, there is no gradient and the learnable parameters would not be updated. And that's why people use generalized IoU loss (G-IoU loss) or distance IoU loss (D-IoU loss). Basically, these loss functions take more information into account (the area of the minimal enclosing box of the two boxes or the distance between the centers of the boxes) to make sure the gradient is non-zero even no intersection exists.

JJangD commented 3 years ago

@lilanxiao Thanks for your reply. I understood that the there will be no gradient updates to the box parameters such as x,y,w,h,th. But I'm confused. For example, let's say simply S = w x h and w = 0 , h = 5. In this case, it would be S = 0. But gradient dS/dw = 5

Although, I think it would be fine to let the gradient be zero might be ok when using G-IOU or D-IOU.

lilanxiao commented 3 years ago

In your case, backpropagation still works.

I think you meant this situation: w and h is the width and height of the intersection part. Because we want to maximize the IoU, we must use - S instead of S as a loss function (because people always minimize the loss function). The gradient w.r.t w is g = d(loss)/dw = -dS/dw = -5 If we use SGD to update w. We do: w_new = w - g * lr = 0 + 5 * lr As you can see, the the value of w is now greater than zero as long as the learning rate is positive. Thus, the two boxes have intersection after this update, which is what we want. image

For IoU calculation, the two boxes have three kinds of relationships. a) with the intersection. Both edges of the intersection area are non-zero. Backprop works. (left in the figure) b) without intersection. Both edges are zero. Backprop doesn't work. (right in the figure) c) the transition between a) and b) (middle in the figure)

The example you gave is actually the last relationship: the area is zero, but one edge of the intersection is still non-zero. In such case, backpropagation still works.

JJangD commented 3 years ago

Thanks for detailed explanation!!! I understood.