-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathline_iou.py
156 lines (134 loc) · 5.83 KB
/
line_iou.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""
Copyright (c) 2022 Kunyang Zhou
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import paddle
import paddle.nn as nn
from ..builder import LOSSES
def line_iou(pred, target, img_w, length=15, aligned=True):
'''
Calculate the line iou value between predictions and targets
Args:
pred: lane predictions, shape: (num_pred, 72)
target: ground truth, shape: (num_target, 72)
img_w: image width
length: extended radius
aligned: True for iou loss calculation, False for pair-wise ious in assign
'''
px1 = pred - length
px2 = pred + length
tx1 = target - length
tx2 = target + length
if aligned:
invalid_mask = target
ovr = paddle.minimum(px2, tx2) - paddle.maximum(px1, tx1)
union = paddle.maximum(px2, tx2) - paddle.minimum(px1, tx1)
else:
num_pred = pred.shape[0]
invalid_mask = target.tile([num_pred, 1, 1])
ovr = (paddle.minimum(px2[:, None, :], tx2[None, ...]) - paddle.maximum(
px1[:, None, :], tx1[None, ...]))
union = (paddle.maximum(px2[:, None, :], tx2[None, ...]) -
paddle.minimum(px1[:, None, :], tx1[None, ...]))
invalid_masks = (invalid_mask < 0) | (invalid_mask >= img_w)
ovr[invalid_masks] = 0.
union[invalid_masks] = 0.
iou = ovr.sum(axis=-1) / (union.sum(axis=-1) + 1e-9)
return iou
def liou_loss(pred, target, img_w, length=15):
return (1 - line_iou(pred, target, img_w, length)).mean()
def lane_iou(pred,
target,
img_h = 320,
img_w = 1640,
length=7.5,
max_dx=1e4,
aligned=True):
"""
Implementation of LaneIOU in CLRerNet
Calculate the LaneIoU value between predictions and targets
Args:
pred: lane predictions, shape: (Nl, Nr), relative coordinate.
target: ground truth, shape: (Nl, Nr), relative coordinate.
img_h (int): original image height corresponding to the feature map region. 320 is the height of the image on CULane.
img_w (int): original image width corresponding to the feature map region. 1640 is the width of the image on CULane.
Returns:
torch.Tensor: virtual lane half-widths for prediction at pre-defined rows, shape (Nl, Nr).
torch.Tensor: virtual lane half-widths for GT at pre-defined rows, shape (Nl, Nr).
Nl: number of lanes, Nr: number of rows.
"""
n_strips = pred.shape[1] - 1
dy = img_h / n_strips * 2 # two horizontal grids
_pred = pred.clone().detach()
pred_dx = (
_pred[:, 2:] - _pred[:, :-2]
) * img_w # pred x difference across two horizontal grids
pred_width = length * paddle.sqrt(pred_dx.pow(2) + dy**2) / dy
pred_width = paddle.concat(
[pred_width[:, 0:1], pred_width, pred_width[:, -1:]], axis=1
)
target_dx = (target[:, 2:] - target[:, :-2]) * img_w
target_dx[paddle.abs(target_dx) > max_dx] = 0
target_width = length * paddle.sqrt(target_dx.pow(2) + dy**2) / dy
target_width = paddle.concat(
[target_width[:, 0:1], target_width, target_width[:, -1:]], dim=1
)
px1 = pred - pred_width
px2 = pred + pred_width
tx1 = target - target_width
tx2 = target + target_width
if aligned:
invalid_mask = target
ovr = paddle.minimum(px2, tx2) - paddle.maximum(px1, tx1)
union = paddle.maximum(px2, tx2) - paddle.minimum(px1, tx1)
else:
num_pred = pred.shape[0]
invalid_mask = target.tile([num_pred, 1, 1])
ovr = (paddle.minimum(px2[:, None, :], tx2[None, ...]) - paddle.maximum(
px1[:, None, :], tx1[None, ...]))
union = (paddle.maximum(px2[:, None, :], tx2[None, ...]) -
paddle.minimum(px1[:, None, :], tx1[None, ...]))
invalid_masks = (invalid_mask < 0) | (invalid_mask >= img_w)
ovr[invalid_masks] = 0.
union[invalid_masks] = 0.
iou = ovr.sum(axis=-1) / (union.sum(axis=-1) + 1e-9)
return iou
def laneliou_loss(pred, target, img_w, length=7.5, **kwargs):
return (1 - lane_iou(pred, target, img_w, length, **kwargs)).mean()
@LOSSES.register()
class Liou_loss(nn.Layer):
"""
Line IoU loss in CLRNet
"""
def __init__(self,cfg):
super().__init__()
def forward(self,pred, target, img_w, length=15):
return liou_loss(pred, target, img_w, length)
@LOSSES.register()
class LaneIoU_loss(nn.Layer):
"""
Lane IoU loss in CLRerNet
"""
def __init__(self,cfg):
super().__init__()
def forward(self,
pred,
target,
img_w,
length=15,
**kwargs):
return laneliou_loss(pred, target, img_w, length, **kwargs)