Upload 1705_159_252.py
Browse files- 1705_159_252.py +44 -0
1705_159_252.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""1705.159.252
|
3 |
+
|
4 |
+
Automatically generated by Colab.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1SzDicEXWRqpiPKW8HkGdpUFIT-SbS8Xd
|
8 |
+
"""
|
9 |
+
|
10 |
+
import torch
|
11 |
+
|
12 |
+
def intersection_over_union(boxes_preds, boxes_labels, box_format="midpoint"):
|
13 |
+
|
14 |
+
if box_format == "midpoint":
|
15 |
+
box1_x1 = boxes_preds[..., 0:1] - boxes_preds[..., 2:3] / 2
|
16 |
+
box1_y1 = boxes_preds[..., 1:2] - boxes_preds[..., 3:4] / 2
|
17 |
+
box1_x2 = boxes_preds[..., 0:1] + boxes_preds[..., 2:3] / 2
|
18 |
+
box1_y2 = boxes_preds[..., 1:2] + boxes_preds[..., 3:4] / 2
|
19 |
+
box2_x1 = boxes_labels[..., 0:1] - boxes_labels[..., 2:3] / 2
|
20 |
+
box2_y1 = boxes_labels[..., 1:2] - boxes_labels[..., 3:4] / 2
|
21 |
+
box2_x2 = boxes_labels[..., 0:1] + boxes_labels[..., 2:3] / 2
|
22 |
+
box2_y2 = boxes_labels[..., 1:2] + boxes_labels[..., 3:4] / 2
|
23 |
+
|
24 |
+
elif box_format == "corners":
|
25 |
+
box1_x1 = boxes_preds[..., 0:1]
|
26 |
+
box_y1 = boxes_preds[..., 1:2]
|
27 |
+
box1_x2 = boxes_preds[..., 2:3]
|
28 |
+
box1_y2 = boxes_preds[..., 3:4]
|
29 |
+
box2_x1 = boxes_labels[...,0:1]
|
30 |
+
box2_y1 = boxes_labels[...,1:2]
|
31 |
+
box2_x2 = boxes_labels[...,2:3]
|
32 |
+
box2_y2 = boxes_labels[...,3:4]
|
33 |
+
|
34 |
+
x1 = torch.max(box1_x1, box2_x1)
|
35 |
+
y1 = torch.max(box1_y1, box2_y1)
|
36 |
+
x2 = torch.min(box1_x2, box2_x2)
|
37 |
+
y2 = torch.min(box1_y2, box2_y2)
|
38 |
+
|
39 |
+
intersection = (x2 - x1).clamp(0) * (y2- y1).clamp(0)
|
40 |
+
|
41 |
+
box1_area = abs((box1_x2 - box1_x1) * (box1_y1 - box1_y2))
|
42 |
+
box2_area = abs((box2_x2 - box2_x1) * (box2_y1 - box2_y2))
|
43 |
+
|
44 |
+
return intersection / (box1_area + box2_area - intersection + 1e-6)
|