16
16
from absl .testing import parameterized
17
17
18
18
import keras_cv
19
+ from keras_cv .src .backend import ops
19
20
from keras_cv .src .tests .test_case import TestCase
20
21
21
22
@@ -42,3 +43,28 @@ def test_proper_output_shapes(self, reduction, target_size):
42
43
y_pred = np .random .uniform (size = (2 , 10 , 6 + 2 * 4 )),
43
44
)
44
45
self .assertEqual (result .shape , target_size )
46
+
47
+ def test_heading_regression_loss (self ):
48
+ num_heading_bins = 4
49
+ loss = keras_cv .losses .CenterNetBoxLoss (
50
+ num_heading_bins = num_heading_bins , anchor_size = [1.0 , 1.0 , 1.0 ]
51
+ )
52
+ heading_true = np .array (
53
+ [[0 , (1 / 2.0 ) * np .pi , np .pi , (3.0 / 2.0 ) * np .pi ]]
54
+ )
55
+ heading_pred = np .array (
56
+ [
57
+ [
58
+ [1.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 ],
59
+ [0.0 , 1.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 ],
60
+ [0.0 , 0.0 , 1.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 ],
61
+ [0.0 , 0.0 , 0.0 , 1.0 , 0.0 , 0.0 , 0.0 , 0.0 ],
62
+ ]
63
+ ]
64
+ )
65
+ heading_loss = loss .heading_regression_loss (
66
+ heading_true = heading_true , heading_pred = heading_pred
67
+ )
68
+ ce_loss = - np .log (np .exp (1 ) / np .exp ([1 , 0 , 0 , 0 ]).sum ())
69
+ expected_loss = ce_loss * num_heading_bins
70
+ self .assertAllClose (ops .sum (heading_loss ), expected_loss )
0 commit comments