Skip to content

Commit d9858a3

Browse files
Complete the black formatting migration. (#1155)
1 parent 08090d0 commit d9858a3

14 files changed

+471
-411
lines changed

pyproject.toml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,5 @@ exclude = '''
1414
| build
1515
| dist
1616
)/
17-
| tensorflow_addons/losses/__init__.py
18-
| tensorflow_addons/losses/focal_loss.py
19-
| tensorflow_addons/losses/giou_loss.py
20-
| tensorflow_addons/losses/giou_loss_test.py
21-
| tensorflow_addons/metrics/multilabel_confusion_matrix.py
22-
| tensorflow_addons/metrics/r_square.py
23-
| tensorflow_addons/optimizers/__init__.py
24-
| tensorflow_addons/optimizers/lookahead.py
25-
| tensorflow_addons/optimizers/moving_average.py
26-
| tensorflow_addons/optimizers/weight_decay_optimizers.py
27-
| tensorflow_addons/text/__init__.py
28-
| tensorflow_addons/text/crf.py
29-
| tensorflow_addons/text/crf_test.py
3017
)
3118
'''

tensorflow_addons/losses/__init__.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
"""Additional losses that conform to Keras API."""
1616

1717
from tensorflow_addons.losses.contrastive import contrastive_loss, ContrastiveLoss
18-
from tensorflow_addons.losses.focal_loss import sigmoid_focal_crossentropy, SigmoidFocalCrossEntropy
18+
from tensorflow_addons.losses.focal_loss import (
19+
sigmoid_focal_crossentropy,
20+
SigmoidFocalCrossEntropy,
21+
)
1922
from tensorflow_addons.losses.giou_loss import giou_loss, GIoULoss
2023
from tensorflow_addons.losses.lifted import lifted_struct_loss, LiftedStructLoss
2124
from tensorflow_addons.losses.sparsemax_loss import sparsemax_loss, SparsemaxLoss
@@ -30,5 +33,11 @@
3033
# Temporarily disable for windows
3134
# Remove after: https://github.com/tensorflow/addons/issues/838
3235
import os
33-
if os.name != 'nt':
34-
from tensorflow_addons.losses.npairs import npairs_loss, NpairsLoss, npairs_multilabel_loss, NpairsMultilabelLoss
36+
37+
if os.name != "nt":
38+
from tensorflow_addons.losses.npairs import (
39+
npairs_loss,
40+
NpairsLoss,
41+
npairs_multilabel_loss,
42+
NpairsMultilabelLoss,
43+
)

tensorflow_addons/losses/focal_loss.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from typeguard import typechecked
2222

2323

24-
@tf.keras.utils.register_keras_serializable(package='Addons')
24+
@tf.keras.utils.register_keras_serializable(package="Addons")
2525
class SigmoidFocalCrossEntropy(tf.keras.losses.Loss):
2626
"""Implements the focal loss function.
2727
@@ -66,27 +66,28 @@ class SigmoidFocalCrossEntropy(tf.keras.losses.Loss):
6666
"""
6767

6868
@typechecked
69-
def __init__(self,
70-
from_logits: bool = False,
71-
alpha: FloatTensorLike = 0.25,
72-
gamma: FloatTensorLike = 2.0,
73-
reduction: str = tf.keras.losses.Reduction.NONE,
74-
name: str = 'sigmoid_focal_crossentropy'):
69+
def __init__(
70+
self,
71+
from_logits: bool = False,
72+
alpha: FloatTensorLike = 0.25,
73+
gamma: FloatTensorLike = 2.0,
74+
reduction: str = tf.keras.losses.Reduction.NONE,
75+
name: str = "sigmoid_focal_crossentropy",
76+
):
7577
super().__init__(name=name, reduction=reduction)
7678

7779
self.from_logits = from_logits
7880
self.alpha = alpha
7981
self.gamma = gamma
8082

81-
def call(self,
82-
y_true,
83-
y_pred):
83+
def call(self, y_true, y_pred):
8484
return sigmoid_focal_crossentropy(
8585
y_true,
8686
y_pred,
8787
alpha=self.alpha,
8888
gamma=self.gamma,
89-
from_logits=self.from_logits)
89+
from_logits=self.from_logits,
90+
)
9091

9192
def get_config(self):
9293
config = {
@@ -98,13 +99,15 @@ def get_config(self):
9899
return {**base_config, **config}
99100

100101

101-
@tf.keras.utils.register_keras_serializable(package='Addons')
102+
@tf.keras.utils.register_keras_serializable(package="Addons")
102103
@tf.function
103-
def sigmoid_focal_crossentropy(y_true: TensorLike,
104-
y_pred: TensorLike,
105-
alpha: FloatTensorLike = 0.25,
106-
gamma: FloatTensorLike = 2.0,
107-
from_logits: bool = False) -> tf.Tensor:
104+
def sigmoid_focal_crossentropy(
105+
y_true: TensorLike,
106+
y_pred: TensorLike,
107+
alpha: FloatTensorLike = 0.25,
108+
gamma: FloatTensorLike = 2.0,
109+
from_logits: bool = False,
110+
) -> tf.Tensor:
108111
"""
109112
Args
110113
y_true: true targets tensor.
@@ -117,8 +120,7 @@ def sigmoid_focal_crossentropy(y_true: TensorLike,
117120
same shape as `y_true`; otherwise, it is scalar.
118121
"""
119122
if gamma and gamma < 0:
120-
raise ValueError(
121-
"Value of gamma should be greater than or equal to zero")
123+
raise ValueError("Value of gamma should be greater than or equal to zero")
122124

123125
y_pred = tf.convert_to_tensor(y_pred)
124126
y_true = tf.convert_to_tensor(y_true, dtype=y_pred.dtype)
@@ -138,7 +140,7 @@ def sigmoid_focal_crossentropy(y_true: TensorLike,
138140

139141
if alpha:
140142
alpha = tf.convert_to_tensor(alpha, dtype=K.floatx())
141-
alpha_factor = (y_true * alpha + (1 - y_true) * (1 - alpha))
143+
alpha_factor = y_true * alpha + (1 - y_true) * (1 - alpha)
142144

143145
if gamma:
144146
gamma = tf.convert_to_tensor(gamma, dtype=K.floatx())

tensorflow_addons/losses/giou_loss.py

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from typeguard import typechecked
2222

2323

24-
@tf.keras.utils.register_keras_serializable(package='Addons')
24+
@tf.keras.utils.register_keras_serializable(package="Addons")
2525
class GIoULoss(tf.keras.losses.Loss):
2626
"""Implements the GIoU loss function.
2727
@@ -50,32 +50,31 @@ class GIoULoss(tf.keras.losses.Loss):
5050
Args:
5151
mode: one of ['giou', 'iou'], decided to calculate GIoU or IoU loss.
5252
"""
53+
5354
@typechecked
54-
def __init__(self,
55-
mode: str = 'giou',
56-
reduction: str = tf.keras.losses.Reduction.AUTO,
57-
name: Optional[str] = 'giou_loss'):
58-
if mode not in ['giou', 'iou']:
55+
def __init__(
56+
self,
57+
mode: str = "giou",
58+
reduction: str = tf.keras.losses.Reduction.AUTO,
59+
name: Optional[str] = "giou_loss",
60+
):
61+
if mode not in ["giou", "iou"]:
5962
raise ValueError("Value of mode should be 'iou' or 'giou'")
6063
super().__init__(name=name, reduction=reduction)
6164
self.mode = mode
6265

6366
def get_config(self):
6467
base_config = super().get_config()
65-
base_config['mode'] = self.mode
68+
base_config["mode"] = self.mode
6669
return base_config
6770

68-
def call(self,
69-
y_true,
70-
y_pred):
71+
def call(self, y_true, y_pred):
7172
return giou_loss(y_true, y_pred, mode=self.mode)
7273

7374

74-
@tf.keras.utils.register_keras_serializable(package='Addons')
75+
@tf.keras.utils.register_keras_serializable(package="Addons")
7576
@tf.function
76-
def giou_loss(y_true: TensorLike,
77-
y_pred: TensorLike,
78-
mode: str = 'giou') -> tf.Tensor:
77+
def giou_loss(y_true: TensorLike, y_pred: TensorLike, mode: str = "giou") -> tf.Tensor:
7978
"""
8079
Args:
8180
y_true: true targets tensor. The coordinates of the each bounding
@@ -87,7 +86,7 @@ def giou_loss(y_true: TensorLike,
8786
Returns:
8887
GIoU loss float `Tensor`.
8988
"""
90-
if mode not in ['giou', 'iou']:
89+
if mode not in ["giou", "iou"]:
9190
raise ValueError("Value of mode should be 'iou' or 'giou'")
9291
y_pred = tf.convert_to_tensor(y_pred)
9392
if not y_pred.dtype.is_floating:
@@ -98,9 +97,7 @@ def giou_loss(y_true: TensorLike,
9897
return 1 - giou
9998

10099

101-
def _calculate_giou(b1: TensorLike,
102-
b2: TensorLike,
103-
mode: str = 'giou') -> tf.Tensor:
100+
def _calculate_giou(b1: TensorLike, b2: TensorLike, mode: str = "giou") -> tf.Tensor:
104101
"""
105102
Args:
106103
b1: bounding box. The coordinates of the each bounding box in boxes are
@@ -112,7 +109,7 @@ def _calculate_giou(b1: TensorLike,
112109
Returns:
113110
GIoU loss float `Tensor`.
114111
"""
115-
zero = tf.convert_to_tensor(0., b1.dtype)
112+
zero = tf.convert_to_tensor(0.0, b1.dtype)
116113
b1_ymin, b1_xmin, b1_ymax, b1_xmax = tf.unstack(b1, 4, axis=-1)
117114
b2_ymin, b2_xmin, b2_ymax, b2_xmax = tf.unstack(b2, 4, axis=-1)
118115
b1_width = tf.maximum(zero, b1_xmax - b1_xmin)
@@ -132,7 +129,7 @@ def _calculate_giou(b1: TensorLike,
132129

133130
union_area = b1_area + b2_area - intersect_area
134131
iou = tf.math.divide_no_nan(intersect_area, union_area)
135-
if mode == 'iou':
132+
if mode == "iou":
136133
return iou
137134

138135
enclose_ymin = tf.minimum(b1_ymin, b2_ymin)
@@ -142,6 +139,5 @@ def _calculate_giou(b1: TensorLike,
142139
enclose_width = tf.maximum(zero, enclose_xmax - enclose_xmin)
143140
enclose_height = tf.maximum(zero, enclose_ymax - enclose_ymin)
144141
enclose_area = enclose_width * enclose_height
145-
giou = iou - tf.math.divide_no_nan(
146-
(enclose_area - union_area), enclose_area)
142+
giou = iou - tf.math.divide_no_nan((enclose_area - union_area), enclose_area)
147143
return giou

tensorflow_addons/losses/giou_loss_test.py

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -27,95 +27,93 @@ class GIoULossTest(tf.test.TestCase, parameterized.TestCase):
2727
"""GIoU test class."""
2828

2929
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")
3332
self.assertEqual(gl_obj.reduction, tf.keras.losses.Reduction.NONE)
3433

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+
)
3837
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")
4544
self.assertAllCloseAccordingToType(loss, expected_result)
4645

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+
)
5049
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+
)
5554
expected_result = tf.constant(
56-
[1.07500000298023224, 1.9333333373069763], dtype=dtype)
55+
[1.07500000298023224, 1.9333333373069763], dtype=dtype
56+
)
5757
loss = giou_loss(boxes1, boxes2)
5858
self.assertAllCloseAccordingToType(loss, expected_result)
5959

6060
def test_with_integer(self):
6161
boxes1 = tf.constant([[4, 3, 7, 5], [5, 6, 10, 7]], dtype=tf.int32)
6262
boxes2 = tf.constant([[3, 4, 6, 8], [14, 14, 15, 15]], dtype=tf.int32)
6363
expected_result = tf.constant(
64-
[1.07500000298023224, 1.9333333373069763], dtype=tf.float32)
64+
[1.07500000298023224, 1.9333333373069763], dtype=tf.float32
65+
)
6566
loss = giou_loss(boxes1, boxes2)
6667
self.assertAllCloseAccordingToType(loss, expected_result)
6768

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+
)
7172
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)
7474
boxes2 = tf.constant([[3.0, 4.0, 6.0, 8.0]], dtype=dtype)
7575
tf.expand_dims(boxes1, -2)
7676
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)
7978
loss = giou_loss(boxes1, boxes2)
8079
self.assertAllCloseAccordingToType(loss, expected_result)
8180

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)
8482
boxes2 = tf.constant([[3.0, 4.0, 6.0, 8.0]], dtype=dtype)
8583
tf.expand_dims(boxes1, 0)
8684
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)
8986
loss = giou_loss(boxes1, boxes2)
9087
self.assertAllCloseAccordingToType(loss, expected_result)
9188

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+
)
9592
def test_one_bbox(self, dtype):
9693
boxes1 = tf.constant([4.0, 3.0, 7.0, 5.0], dtype=dtype)
9794
boxes2 = tf.constant([3.0, 4.0, 6.0, 8.0], dtype=dtype)
9895
expected_result = tf.constant(1.07500000298023224, dtype=dtype)
9996
loss = giou_loss(boxes1, boxes2)
10097
self.assertAllCloseAccordingToType(loss, expected_result)
10198

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+
)
105102
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+
)
110107
expected_result = tf.constant(
111-
[1.07500000298023224, 1.9333333373069763], dtype=dtype)
108+
[1.07500000298023224, 1.9333333373069763], dtype=dtype
109+
)
112110
model = tf.keras.Sequential()
113111
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+
)
116114
loss = model.evaluate(boxes1, boxes2, batch_size=2, steps=1)
117115
self.assertAllCloseAccordingToType(loss, expected_result)
118116

119117

120-
if __name__ == '__main__':
118+
if __name__ == "__main__":
121119
tf.test.main()

0 commit comments

Comments
 (0)