In the process of target detection , An important concept is that IOU. General reference model predicted bbox and Groud Truth The comparison between them .
What is cross comparison ?
I O U = A ∩ B A ∪ B IOU = \frac{A\cap B}{A\cup B} IOU=A∪BA∩B
aggregate A And set B The union of includes the above 3 Color regions .
aggregate C It's a collection A And set B Intersection of .
In the process of target detection ,IOU It's the ratio of the two sets above .
A ∪ B A \cup B A∪B In fact, it is A + B − C A + B - C A+B−C.
Then the formula can be transformed into :
I O U = A ∩ B A + B − ( A ∩ B ) IOU = \frac{A \cap B}{A + B - (A \cap B)} IOU=
A+B−(A∩B)A∩B
IOU Measure the degree of overlap between two sets .
* IOU by 0 Time , The two boxes do not overlap , There is no intersection .
* IOU by 1 Time , The two boxes overlap completely .
* IOU The value is 0 ~ 1 When the value between , Represents the degree of overlap between the two boxes , The higher the value , The higher the degree of overlap .
stay 2D Target detection , because bbox It's a rectangle , So it's easy to get IOU.
Box A and B intersect , Typical cases are as follows :
A and B It is easy to get the area of ,C The area is a little more complicated , But if you are patient and careful, you can get it .
If we use mathematical thinking , Careful arrangement , Area can be found C We just need to find the product of the side length , even if A and B The position of is relative , But with a little transformation, it can be found .
If W representative A and B Intersection of C Of x Side length in axis direction , So there is
W = m i n ( A . x 1 , B . x 1 ) − m a x ( A . x 0 , B . x 0 ) W =
min(A.x_{1},B.x_{1}) - max(A.x_{0},B.x_{0})W=min(A.x1,B.x1)−max(A.x0,B.x0)
In the same way ,
H = m i n ( A . y 1 , B . y 1 ) − m a x ( A . y 0 , B . y 0 ) H =
min(A.y_{1},B.y_{1}) - max(A.y_{0},B.y_{0})H=min(A.y1,B.y1)−max(A.y0,B.y0)
Look carefully at the corresponding relationship above , It can be found that the formula holds . The derivation of this formula is not difficult , nothing less than 4 Transformation of relative position of coordinates of vertices , You can figure it out for yourself .
If A And B It doesn't intersect at all .
At this time, we can find out W <= 0 or H <= 0.
Here is Python code .
class BBox: def __init__(self,x,y,w,h): self.x = x self.y = y self.w = w self.h
= h def iou(a,b): assert isinstance(a,BBox) assert isinstance(b,BBox) area_a = a
.w * a.h area_b = b.w * b.h w = min(b.x+b.w,a.x+a.w) - max(a.x,b.x) h = min(b.y+
b.h,a.y+a.h) - max(a.y,b.y) if w <= 0 or h <= 0: return 0 area_c = w * h return
area_c/ (area_a + area_b - area_c) if __name__ == '__main__': a = BBox(1,1,4,5)
b1= BBox(1,1,4,5) b2 = BBox(5,1,4,5) b3 = BBox(3,2,3,6) print("iou ",iou(a,b1))
print("iou ",iou(a,b2)) print("iou ",iou(a,b3))
The results are as follows :
iou 1.0 iou 0 iou 0.26666666666666666
Technology
Daily Recommendation