Course 4, Week 3: IOU for non-intersecting boxes

Hi, I get an error for the implementation of the IOU for non-intersecting boxes:

AssertionError Traceback (most recent call last)
12 box2 = (5,6,7,8)
13 print("iou for non-intersecting boxes = " + str(iou(box1,box2)))
—> 14 assert iou(box1, box2) == 0, “Intersection must be 0”
16 ## Test case 3: boxes intersect at vertices only

AssertionError: Intersection must be 0

When I draw it out, I get why I get this error, and why it needs to be 0. But I have trouble with coding this. When I calculate the inter_width and inter_height, these are both negative numbers, because the boxes do not intersect (both -2). When I then calculate the inter_area: -2*-2 = positive 4. That’s what’s giving me trouble. This should be 0, as there is not intersection. Can anyone help me how to fix this? I can add if and else statements, but I get the feeling it should be easier than that as there is no mention of this whatsoever in the assignments.

1 Like

Welcome to the community.

Please revisit descriptions for this Exercise. There are two good hints.

  • The two boxes may have no intersection. You can detect this if the intersection coordinates you calculate end up being the top right and/or bottom left corners of an intersection box. Another way to think of this is if you calculate the height (𝑦2−𝑦1) or width (𝑥2−𝑥1) and find that at least one of these lengths is negative, then there is no intersection (intersection area is zero).
  • inter_area = You can use max(height, 0) and max(width, 0)

So, your calculation is right. But, if it is a negative value, then, it means no overlap. Just like the 2nd bullet above, that value should be replaced by 0. Then, intersection area goes to 0, of course.

Hope this helps.

1 Like

Thank you! I missed the second hint of the use max(height,0). I was too focused on the explanation above it. It works now, although I’m still puzzled how this function works. Is there a documentation?

If you Google, then, there are lots of articles about IOU…
But, I think this paper is a good summary about IOU and related activities. (Note that this paper is about GIoU, Generalized IoU. But, it needed to explain IoU to compare to GIoU. So, it must be a good summary. :slight_smile: And, this paper is cited well.)

@AngeliquevV @anon57530071 I was also puzzled by the hint to use max(height, 0) or max(width, 0); I feel that this is an unnecessary complication added.

I understood the cases where there is no intersection or where there is only intersection at edges/vertices - but I think that here, just using an if-else statement makes the code a little clearer:

inter_area = 0 if <condition> else <some positive number> 

I do appreciate hints that clean up the code, but sometimes they are a bit distracting.

Just my two cents! :slight_smile: