@@ -27,95 +27,93 @@ class GIoULossTest(tf.test.TestCase, parameterized.TestCase):
27
27
"""GIoU test class."""
28
28
29
29
def test_config (self ):
30
- gl_obj = GIoULoss (
31
- reduction = tf .keras .losses .Reduction .NONE , name = 'giou_loss' )
32
- self .assertEqual (gl_obj .name , 'giou_loss' )
30
+ gl_obj = GIoULoss (reduction = tf .keras .losses .Reduction .NONE , name = "giou_loss" )
31
+ self .assertEqual (gl_obj .name , "giou_loss" )
33
32
self .assertEqual (gl_obj .reduction , tf .keras .losses .Reduction .NONE )
34
33
35
- @parameterized .named_parameters (( "float16" , np . float16 ),
36
- ("float32" , np .float32 ),
37
- ( "float64" , np . float64 ) )
34
+ @parameterized .named_parameters (
35
+ ( "float16" , np . float16 ), ("float32" , np .float32 ), ( "float64" , np . float64 )
36
+ )
38
37
def test_iou (self , dtype ):
39
- boxes1 = tf .constant ([[4.0 , 3.0 , 7.0 , 5.0 ], [5.0 , 6.0 , 10.0 , 7.0 ]],
40
- dtype = dtype )
41
- boxes2 = tf . constant ( [[3.0 , 4.0 , 6.0 , 8.0 ], [14.0 , 14.0 , 15.0 , 15.0 ]],
42
- dtype = dtype )
43
- expected_result = tf .constant ([0.875 , 1. ], dtype = dtype )
44
- loss = giou_loss (boxes1 , boxes2 , mode = ' iou' )
38
+ boxes1 = tf .constant ([[4.0 , 3.0 , 7.0 , 5.0 ], [5.0 , 6.0 , 10.0 , 7.0 ]], dtype = dtype )
39
+ boxes2 = tf . constant (
40
+ [[3.0 , 4.0 , 6.0 , 8.0 ], [14.0 , 14.0 , 15.0 , 15.0 ]], dtype = dtype
41
+ )
42
+ expected_result = tf .constant ([0.875 , 1.0 ], dtype = dtype )
43
+ loss = giou_loss (boxes1 , boxes2 , mode = " iou" )
45
44
self .assertAllCloseAccordingToType (loss , expected_result )
46
45
47
- @parameterized .named_parameters (( "float16" , np . float16 ),
48
- ("float32" , np .float32 ),
49
- ( "float64" , np . float64 ) )
46
+ @parameterized .named_parameters (
47
+ ( "float16" , np . float16 ), ("float32" , np .float32 ), ( "float64" , np . float64 )
48
+ )
50
49
def test_giou_loss (self , dtype ):
51
- boxes1 = tf .constant ([[4.0 , 3.0 , 7.0 , 5.0 ], [5.0 , 6.0 , 10.0 , 7.0 ]],
52
- dtype = dtype )
53
- boxes2 = tf . constant ( [[3.0 , 4.0 , 6.0 , 8.0 ], [14.0 , 14.0 , 15.0 , 15.0 ]],
54
- dtype = dtype )
50
+ boxes1 = tf .constant ([[4.0 , 3.0 , 7.0 , 5.0 ], [5.0 , 6.0 , 10.0 , 7.0 ]], dtype = dtype )
51
+ boxes2 = tf . constant (
52
+ [[3.0 , 4.0 , 6.0 , 8.0 ], [14.0 , 14.0 , 15.0 , 15.0 ]], dtype = dtype
53
+ )
55
54
expected_result = tf .constant (
56
- [1.07500000298023224 , 1.9333333373069763 ], dtype = dtype )
55
+ [1.07500000298023224 , 1.9333333373069763 ], dtype = dtype
56
+ )
57
57
loss = giou_loss (boxes1 , boxes2 )
58
58
self .assertAllCloseAccordingToType (loss , expected_result )
59
59
60
60
def test_with_integer (self ):
61
61
boxes1 = tf .constant ([[4 , 3 , 7 , 5 ], [5 , 6 , 10 , 7 ]], dtype = tf .int32 )
62
62
boxes2 = tf .constant ([[3 , 4 , 6 , 8 ], [14 , 14 , 15 , 15 ]], dtype = tf .int32 )
63
63
expected_result = tf .constant (
64
- [1.07500000298023224 , 1.9333333373069763 ], dtype = tf .float32 )
64
+ [1.07500000298023224 , 1.9333333373069763 ], dtype = tf .float32
65
+ )
65
66
loss = giou_loss (boxes1 , boxes2 )
66
67
self .assertAllCloseAccordingToType (loss , expected_result )
67
68
68
- @parameterized .named_parameters (( "float16" , np . float16 ),
69
- ("float32" , np .float32 ),
70
- ( "float64" , np . float64 ) )
69
+ @parameterized .named_parameters (
70
+ ( "float16" , np . float16 ), ("float32" , np .float32 ), ( "float64" , np . float64 )
71
+ )
71
72
def test_different_shapes (self , dtype ):
72
- boxes1 = tf .constant ([[4.0 , 3.0 , 7.0 , 5.0 ], [5.0 , 6.0 , 10.0 , 7.0 ]],
73
- dtype = dtype )
73
+ boxes1 = tf .constant ([[4.0 , 3.0 , 7.0 , 5.0 ], [5.0 , 6.0 , 10.0 , 7.0 ]], dtype = dtype )
74
74
boxes2 = tf .constant ([[3.0 , 4.0 , 6.0 , 8.0 ]], dtype = dtype )
75
75
tf .expand_dims (boxes1 , - 2 )
76
76
tf .expand_dims (boxes2 , 0 )
77
- expected_result = tf .constant ([1.07500000298023224 , 1.366071 ],
78
- dtype = dtype )
77
+ expected_result = tf .constant ([1.07500000298023224 , 1.366071 ], dtype = dtype )
79
78
loss = giou_loss (boxes1 , boxes2 )
80
79
self .assertAllCloseAccordingToType (loss , expected_result )
81
80
82
- boxes1 = tf .constant ([[4.0 , 3.0 , 7.0 , 5.0 ], [5.0 , 6.0 , 10.0 , 7.0 ]],
83
- dtype = dtype )
81
+ boxes1 = tf .constant ([[4.0 , 3.0 , 7.0 , 5.0 ], [5.0 , 6.0 , 10.0 , 7.0 ]], dtype = dtype )
84
82
boxes2 = tf .constant ([[3.0 , 4.0 , 6.0 , 8.0 ]], dtype = dtype )
85
83
tf .expand_dims (boxes1 , 0 )
86
84
tf .expand_dims (boxes2 , - 2 )
87
- expected_result = tf .constant ([1.07500000298023224 , 1.366071 ],
88
- dtype = dtype )
85
+ expected_result = tf .constant ([1.07500000298023224 , 1.366071 ], dtype = dtype )
89
86
loss = giou_loss (boxes1 , boxes2 )
90
87
self .assertAllCloseAccordingToType (loss , expected_result )
91
88
92
- @parameterized .named_parameters (( "float16" , np . float16 ),
93
- ("float32" , np .float32 ),
94
- ( "float64" , np . float64 ) )
89
+ @parameterized .named_parameters (
90
+ ( "float16" , np . float16 ), ("float32" , np .float32 ), ( "float64" , np . float64 )
91
+ )
95
92
def test_one_bbox (self , dtype ):
96
93
boxes1 = tf .constant ([4.0 , 3.0 , 7.0 , 5.0 ], dtype = dtype )
97
94
boxes2 = tf .constant ([3.0 , 4.0 , 6.0 , 8.0 ], dtype = dtype )
98
95
expected_result = tf .constant (1.07500000298023224 , dtype = dtype )
99
96
loss = giou_loss (boxes1 , boxes2 )
100
97
self .assertAllCloseAccordingToType (loss , expected_result )
101
98
102
- @parameterized .named_parameters (( "float16" , np . float16 ),
103
- ("float32" , np .float32 ),
104
- ( "float64" , np . float64 ) )
99
+ @parameterized .named_parameters (
100
+ ( "float16" , np . float16 ), ("float32" , np .float32 ), ( "float64" , np . float64 )
101
+ )
105
102
def test_keras_model (self , dtype ):
106
- boxes1 = tf .constant ([[4.0 , 3.0 , 7.0 , 5.0 ], [5.0 , 6.0 , 10.0 , 7.0 ]],
107
- dtype = dtype )
108
- boxes2 = tf . constant ( [[3.0 , 4.0 , 6.0 , 8.0 ], [14.0 , 14.0 , 15.0 , 15.0 ]],
109
- dtype = dtype )
103
+ boxes1 = tf .constant ([[4.0 , 3.0 , 7.0 , 5.0 ], [5.0 , 6.0 , 10.0 , 7.0 ]], dtype = dtype )
104
+ boxes2 = tf . constant (
105
+ [[3.0 , 4.0 , 6.0 , 8.0 ], [14.0 , 14.0 , 15.0 , 15.0 ]], dtype = dtype
106
+ )
110
107
expected_result = tf .constant (
111
- [1.07500000298023224 , 1.9333333373069763 ], dtype = dtype )
108
+ [1.07500000298023224 , 1.9333333373069763 ], dtype = dtype
109
+ )
112
110
model = tf .keras .Sequential ()
113
111
model .compile (
114
- optimizer = ' adam' ,
115
- loss = GIoULoss ( reduction = tf . keras . losses . Reduction . NONE ) )
112
+ optimizer = " adam" , loss = GIoULoss ( reduction = tf . keras . losses . Reduction . NONE )
113
+ )
116
114
loss = model .evaluate (boxes1 , boxes2 , batch_size = 2 , steps = 1 )
117
115
self .assertAllCloseAccordingToType (loss , expected_result )
118
116
119
117
120
- if __name__ == ' __main__' :
118
+ if __name__ == " __main__" :
121
119
tf .test .main ()
0 commit comments