We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Incomplete and wrong typing
It seems, there are many errors in the typed code. 

For examples 
1. https://github.com/tensorflow/addons/blob/master/tensorflow_addons/activations/rrelu.py#L28 (Optional[int])
2. https://github.com/tensorflow/addons/blob/master/tensorflow_addons/losses/giou_loss.py#L101 (TensorLike)

The integration of a type checker such as `mypy` can help to keep it clean if the goal is to integrate type checking.  


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `tensorflow_addons/losses/giou_loss.py`
Content:
```
1 # Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 # ==============================================================================
15 """Implements GIoU loss."""
16 
17 import tensorflow as tf
18 
19 from tensorflow_addons.utils.types import TensorLike
20 from typing import List
21 from typeguard import typechecked
22 
23 
24 @tf.keras.utils.register_keras_serializable(package='Addons')
25 class GIoULoss(tf.keras.losses.Loss):
26     """Implements the GIoU loss function.
27 
28     GIoU loss was first introduced in the
29     [Generalized Intersection over Union:
30     A Metric and A Loss for Bounding Box Regression]
31     (https://giou.stanford.edu/GIoU.pdf).
32     GIoU is an enhancement for models which use IoU in object detection.
33 
34     Usage:
35 
36     ```python
37     gl = tfa.losses.GIoULoss()
38     boxes1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]])
39     boxes2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0]])
40     loss = gl(boxes1, boxes2)
41     print('Loss: ', loss.numpy())  # Loss: [1.07500000298023224, 1.9333333373069763]
42     ```
43     Usage with tf.keras API:
44 
45     ```python
46     model = tf.keras.Model(inputs, outputs)
47     model.compile('sgd', loss=tfa.losses.GIoULoss())
48     ```
49 
50     Args:
51       mode: one of ['giou', 'iou'], decided to calculate GIoU or IoU loss.
52     """
53     @typechecked
54     def __init__(self,
55                  mode: str = 'giou',
56                  reduction: str = tf.keras.losses.Reduction.AUTO,
57                  name: str = 'giou_loss'):
58         if mode not in ['giou', 'iou']:
59             raise ValueError("Value of mode should be 'iou' or 'giou'")
60         super().__init__(name=name, reduction=reduction)
61         self.mode = mode
62 
63     def get_config(self):
64         base_config = super().get_config()
65         base_config['mode'] = self.mode
66         return base_config
67 
68     def call(self,
69              y_true,
70              y_pred):
71         return giou_loss(y_true, y_pred, mode=self.mode)
72 
73 
74 @tf.keras.utils.register_keras_serializable(package='Addons')
75 @tf.function
76 def giou_loss(y_true: TensorLike,
77               y_pred: TensorLike,
78               mode: str = 'giou') -> tf.Tensor:
79     """
80     Args:
81         y_true: true targets tensor. The coordinates of the each bounding
82             box in boxes are encoded as [y_min, x_min, y_max, x_max].
83         y_pred: predictions tensor. The coordinates of the each bounding
84             box in boxes are encoded as [y_min, x_min, y_max, x_max].
85         mode: one of ['giou', 'iou'], decided to calculate GIoU or IoU loss.
86 
87     Returns:
88         GIoU loss float `Tensor`.
89     """
90     if mode not in ['giou', 'iou']:
91         raise ValueError("Value of mode should be 'iou' or 'giou'")
92     y_pred = tf.convert_to_tensor(y_pred)
93     if not y_pred.dtype.is_floating:
94         y_pred = tf.cast(y_pred, tf.float32)
95     y_true = tf.cast(y_true, y_pred.dtype)
96     giou = _calculate_giou(y_pred, y_true, mode)
97 
98     return 1 - giou
99 
100 
101 def _calculate_giou(b1: List[int],
102                     b2: List[int],
103                     mode: str = 'giou') -> tf.Tensor:
104     """
105     Args:
106         b1: bounding box. The coordinates of the each bounding box in boxes are
107             encoded as [y_min, x_min, y_max, x_max].
108         b2: the other bounding box. The coordinates of the each bounding box
109             in boxes are encoded as [y_min, x_min, y_max, x_max].
110         mode: one of ['giou', 'iou'], decided to calculate GIoU or IoU loss.
111 
112     Returns:
113         GIoU loss float `Tensor`.
114     """
115     zero = tf.convert_to_tensor(0., b1.dtype)
116     b1_ymin, b1_xmin, b1_ymax, b1_xmax = tf.unstack(b1, 4, axis=-1)
117     b2_ymin, b2_xmin, b2_ymax, b2_xmax = tf.unstack(b2, 4, axis=-1)
118     b1_width = tf.maximum(zero, b1_xmax - b1_xmin)
119     b1_height = tf.maximum(zero, b1_ymax - b1_ymin)
120     b2_width = tf.maximum(zero, b2_xmax - b2_xmin)
121     b2_height = tf.maximum(zero, b2_ymax - b2_ymin)
122     b1_area = b1_width * b1_height
123     b2_area = b2_width * b2_height
124 
125     intersect_ymin = tf.maximum(b1_ymin, b2_ymin)
126     intersect_xmin = tf.maximum(b1_xmin, b2_xmin)
127     intersect_ymax = tf.minimum(b1_ymax, b2_ymax)
128     intersect_xmax = tf.minimum(b1_xmax, b2_xmax)
129     intersect_width = tf.maximum(zero, intersect_xmax - intersect_xmin)
130     intersect_height = tf.maximum(zero, intersect_ymax - intersect_ymin)
131     intersect_area = intersect_width * intersect_height
132 
133     union_area = b1_area + b2_area - intersect_area
134     iou = tf.math.divide_no_nan(intersect_area, union_area)
135     if mode == 'iou':
136         return iou
137 
138     enclose_ymin = tf.minimum(b1_ymin, b2_ymin)
139     enclose_xmin = tf.minimum(b1_xmin, b2_xmin)
140     enclose_ymax = tf.maximum(b1_ymax, b2_ymax)
141     enclose_xmax = tf.maximum(b1_xmax, b2_xmax)
142     enclose_width = tf.maximum(zero, enclose_xmax - enclose_xmin)
143     enclose_height = tf.maximum(zero, enclose_ymax - enclose_ymin)
144     enclose_area = enclose_width * enclose_height
145     giou = iou - tf.math.divide_no_nan(
146         (enclose_area - union_area), enclose_area)
147     return giou
148
```

Path: `tensorflow_addons/activations/rrelu.py`
Content:
```
1 # Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 # ==============================================================================
15 
16 import tensorflow as tf
17 from tensorflow_addons.utils.types import Number
18 from tensorflow_addons.utils import types
19 from typing import Optional
20 
21 
22 @tf.keras.utils.register_keras_serializable(package="Addons")
23 def rrelu(
24     x: types.TensorLike,
25     lower: Number = 0.125,
26     upper: Number = 0.3333333333333333,
27     training: Optional[str] = None,
28     seed: Optional[str] = None,
29 ) -> tf.Tensor:
30     """rrelu function.
31 
32     Computes rrelu function:
33     `x if x > 0 else random(lower, upper) * x` or
34     `x if x > 0 else x * (lower + upper) / 2`
35     depending on whether training is enabled.
36 
37     See [Empirical Evaluation of Rectified Activations in Convolutional Network](https://arxiv.org/abs/1505.00853).
38 
39     Args:
40         x: A `Tensor`. Must be one of the following types:
41             `float16`, `float32`, `float64`.
42         lower: `float`, lower bound for random alpha.
43         upper: `float`, upper bound for random alpha.
44         training: `bool`, indicating whether the `call`
45         is meant for training or inference.
46         seed: `int`, this sets the operation-level seed.
47     Returns:
48         result: A `Tensor`. Has the same type as `x`.
49     """
50     x = tf.convert_to_tensor(x)
51     if training is None:
52         training = tf.keras.backend.learning_phase()
53         training = bool(tf.keras.backend.get_value(training))
54 
55     if training:
56         alpha = tf.random.uniform(
57             tf.shape(x), minval=lower, maxval=upper, dtype=x.dtype, seed=seed
58         )
59     else:
60         alpha = tf.cast((lower + upper) / 2, x.dtype)
61 
62     return tf.where(x >= 0, x, alpha * x)
63
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/tensorflow_addons/activations/rrelu.py b/tensorflow_addons/activations/rrelu.py
--- a/tensorflow_addons/activations/rrelu.py
+++ b/tensorflow_addons/activations/rrelu.py
@@ -24,8 +24,8 @@
     x: types.TensorLike,
     lower: Number = 0.125,
     upper: Number = 0.3333333333333333,
-    training: Optional[str] = None,
-    seed: Optional[str] = None,
+    training: Optional[bool] = None,
+    seed: Optional[int] = None,
 ) -> tf.Tensor:
     """rrelu function.
 
diff --git a/tensorflow_addons/losses/giou_loss.py b/tensorflow_addons/losses/giou_loss.py
--- a/tensorflow_addons/losses/giou_loss.py
+++ b/tensorflow_addons/losses/giou_loss.py
@@ -17,7 +17,7 @@
 import tensorflow as tf
 
 from tensorflow_addons.utils.types import TensorLike
-from typing import List
+from typing import Optional
 from typeguard import typechecked
 
 
@@ -54,7 +54,7 @@
     def __init__(self,
                  mode: str = 'giou',
                  reduction: str = tf.keras.losses.Reduction.AUTO,
-                 name: str = 'giou_loss'):
+                 name: Optional[str] = 'giou_loss'):
         if mode not in ['giou', 'iou']:
             raise ValueError("Value of mode should be 'iou' or 'giou'")
         super().__init__(name=name, reduction=reduction)
@@ -98,8 +98,8 @@
     return 1 - giou
 
 
-def _calculate_giou(b1: List[int],
-                    b2: List[int],
+def _calculate_giou(b1: TensorLike,
+                    b2: TensorLike,
                     mode: str = 'giou') -> tf.Tensor:
     """
     Args:

{"golden_diff": "diff --git a/tensorflow_addons/activations/rrelu.py b/tensorflow_addons/activations/rrelu.py\n--- a/tensorflow_addons/activations/rrelu.py\n+++ b/tensorflow_addons/activations/rrelu.py\n@@ -24,8 +24,8 @@\n     x: types.TensorLike,\n     lower: Number = 0.125,\n     upper: Number = 0.3333333333333333,\n-    training: Optional[str] = None,\n-    seed: Optional[str] = None,\n+    training: Optional[bool] = None,\n+    seed: Optional[int] = None,\n ) -> tf.Tensor:\n     \"\"\"rrelu function.\n \ndiff --git a/tensorflow_addons/losses/giou_loss.py b/tensorflow_addons/losses/giou_loss.py\n--- a/tensorflow_addons/losses/giou_loss.py\n+++ b/tensorflow_addons/losses/giou_loss.py\n@@ -17,7 +17,7 @@\n import tensorflow as tf\n \n from tensorflow_addons.utils.types import TensorLike\n-from typing import List\n+from typing import Optional\n from typeguard import typechecked\n \n \n@@ -54,7 +54,7 @@\n     def __init__(self,\n                  mode: str = 'giou',\n                  reduction: str = tf.keras.losses.Reduction.AUTO,\n-                 name: str = 'giou_loss'):\n+                 name: Optional[str] = 'giou_loss'):\n         if mode not in ['giou', 'iou']:\n             raise ValueError(\"Value of mode should be 'iou' or 'giou'\")\n         super().__init__(name=name, reduction=reduction)\n@@ -98,8 +98,8 @@\n     return 1 - giou\n \n \n-def _calculate_giou(b1: List[int],\n-                    b2: List[int],\n+def _calculate_giou(b1: TensorLike,\n+                    b2: TensorLike,\n                     mode: str = 'giou') -> tf.Tensor:\n     \"\"\"\n     Args:\n", "issue": "Incomplete and wrong typing\nIt seems, there are many errors in the typed code. \r\n\r\nFor examples \r\n1. https://github.com/tensorflow/addons/blob/master/tensorflow_addons/activations/rrelu.py#L28 (Optional[int])\r\n2. https://github.com/tensorflow/addons/blob/master/tensorflow_addons/losses/giou_loss.py#L101 (TensorLike)\r\n\r\nThe integration of a type checker such as `mypy` can help to keep it clean if the goal is to integrate type checking.  \r\n\n", "before_files": [{"content": "# Copyright 2019 The TensorFlow Authors. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n# ==============================================================================\n\"\"\"Implements GIoU loss.\"\"\"\n\nimport tensorflow as tf\n\nfrom tensorflow_addons.utils.types import TensorLike\nfrom typing import List\nfrom typeguard import typechecked\n\n\n@tf.keras.utils.register_keras_serializable(package='Addons')\nclass GIoULoss(tf.keras.losses.Loss):\n    \"\"\"Implements the GIoU loss function.\n\n    GIoU loss was first introduced in the\n    [Generalized Intersection over Union:\n    A Metric and A Loss for Bounding Box Regression]\n    (https://giou.stanford.edu/GIoU.pdf).\n    GIoU is an enhancement for models which use IoU in object detection.\n\n    Usage:\n\n    ```python\n    gl = tfa.losses.GIoULoss()\n    boxes1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]])\n    boxes2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0]])\n    loss = gl(boxes1, boxes2)\n    print('Loss: ', loss.numpy())  # Loss: [1.07500000298023224, 1.9333333373069763]\n    ```\n    Usage with tf.keras API:\n\n    ```python\n    model = tf.keras.Model(inputs, outputs)\n    model.compile('sgd', loss=tfa.losses.GIoULoss())\n    ```\n\n    Args:\n      mode: one of ['giou', 'iou'], decided to calculate GIoU or IoU loss.\n    \"\"\"\n    @typechecked\n    def __init__(self,\n                 mode: str = 'giou',\n                 reduction: str = tf.keras.losses.Reduction.AUTO,\n                 name: str = 'giou_loss'):\n        if mode not in ['giou', 'iou']:\n            raise ValueError(\"Value of mode should be 'iou' or 'giou'\")\n        super().__init__(name=name, reduction=reduction)\n        self.mode = mode\n\n    def get_config(self):\n        base_config = super().get_config()\n        base_config['mode'] = self.mode\n        return base_config\n\n    def call(self,\n             y_true,\n             y_pred):\n        return giou_loss(y_true, y_pred, mode=self.mode)\n\n\n@tf.keras.utils.register_keras_serializable(package='Addons')\n@tf.function\ndef giou_loss(y_true: TensorLike,\n              y_pred: TensorLike,\n              mode: str = 'giou') -> tf.Tensor:\n    \"\"\"\n    Args:\n        y_true: true targets tensor. The coordinates of the each bounding\n            box in boxes are encoded as [y_min, x_min, y_max, x_max].\n        y_pred: predictions tensor. The coordinates of the each bounding\n            box in boxes are encoded as [y_min, x_min, y_max, x_max].\n        mode: one of ['giou', 'iou'], decided to calculate GIoU or IoU loss.\n\n    Returns:\n        GIoU loss float `Tensor`.\n    \"\"\"\n    if mode not in ['giou', 'iou']:\n        raise ValueError(\"Value of mode should be 'iou' or 'giou'\")\n    y_pred = tf.convert_to_tensor(y_pred)\n    if not y_pred.dtype.is_floating:\n        y_pred = tf.cast(y_pred, tf.float32)\n    y_true = tf.cast(y_true, y_pred.dtype)\n    giou = _calculate_giou(y_pred, y_true, mode)\n\n    return 1 - giou\n\n\ndef _calculate_giou(b1: List[int],\n                    b2: List[int],\n                    mode: str = 'giou') -> tf.Tensor:\n    \"\"\"\n    Args:\n        b1: bounding box. The coordinates of the each bounding box in boxes are\n            encoded as [y_min, x_min, y_max, x_max].\n        b2: the other bounding box. The coordinates of the each bounding box\n            in boxes are encoded as [y_min, x_min, y_max, x_max].\n        mode: one of ['giou', 'iou'], decided to calculate GIoU or IoU loss.\n\n    Returns:\n        GIoU loss float `Tensor`.\n    \"\"\"\n    zero = tf.convert_to_tensor(0., b1.dtype)\n    b1_ymin, b1_xmin, b1_ymax, b1_xmax = tf.unstack(b1, 4, axis=-1)\n    b2_ymin, b2_xmin, b2_ymax, b2_xmax = tf.unstack(b2, 4, axis=-1)\n    b1_width = tf.maximum(zero, b1_xmax - b1_xmin)\n    b1_height = tf.maximum(zero, b1_ymax - b1_ymin)\n    b2_width = tf.maximum(zero, b2_xmax - b2_xmin)\n    b2_height = tf.maximum(zero, b2_ymax - b2_ymin)\n    b1_area = b1_width * b1_height\n    b2_area = b2_width * b2_height\n\n    intersect_ymin = tf.maximum(b1_ymin, b2_ymin)\n    intersect_xmin = tf.maximum(b1_xmin, b2_xmin)\n    intersect_ymax = tf.minimum(b1_ymax, b2_ymax)\n    intersect_xmax = tf.minimum(b1_xmax, b2_xmax)\n    intersect_width = tf.maximum(zero, intersect_xmax - intersect_xmin)\n    intersect_height = tf.maximum(zero, intersect_ymax - intersect_ymin)\n    intersect_area = intersect_width * intersect_height\n\n    union_area = b1_area + b2_area - intersect_area\n    iou = tf.math.divide_no_nan(intersect_area, union_area)\n    if mode == 'iou':\n        return iou\n\n    enclose_ymin = tf.minimum(b1_ymin, b2_ymin)\n    enclose_xmin = tf.minimum(b1_xmin, b2_xmin)\n    enclose_ymax = tf.maximum(b1_ymax, b2_ymax)\n    enclose_xmax = tf.maximum(b1_xmax, b2_xmax)\n    enclose_width = tf.maximum(zero, enclose_xmax - enclose_xmin)\n    enclose_height = tf.maximum(zero, enclose_ymax - enclose_ymin)\n    enclose_area = enclose_width * enclose_height\n    giou = iou - tf.math.divide_no_nan(\n        (enclose_area - union_area), enclose_area)\n    return giou\n", "path": "tensorflow_addons/losses/giou_loss.py"}, {"content": "# Copyright 2019 The TensorFlow Authors. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n# ==============================================================================\n\nimport tensorflow as tf\nfrom tensorflow_addons.utils.types import Number\nfrom tensorflow_addons.utils import types\nfrom typing import Optional\n\n\n@tf.keras.utils.register_keras_serializable(package=\"Addons\")\ndef rrelu(\n    x: types.TensorLike,\n    lower: Number = 0.125,\n    upper: Number = 0.3333333333333333,\n    training: Optional[str] = None,\n    seed: Optional[str] = None,\n) -> tf.Tensor:\n    \"\"\"rrelu function.\n\n    Computes rrelu function:\n    `x if x > 0 else random(lower, upper) * x` or\n    `x if x > 0 else x * (lower + upper) / 2`\n    depending on whether training is enabled.\n\n    See [Empirical Evaluation of Rectified Activations in Convolutional Network](https://arxiv.org/abs/1505.00853).\n\n    Args:\n        x: A `Tensor`. Must be one of the following types:\n            `float16`, `float32`, `float64`.\n        lower: `float`, lower bound for random alpha.\n        upper: `float`, upper bound for random alpha.\n        training: `bool`, indicating whether the `call`\n        is meant for training or inference.\n        seed: `int`, this sets the operation-level seed.\n    Returns:\n        result: A `Tensor`. Has the same type as `x`.\n    \"\"\"\n    x = tf.convert_to_tensor(x)\n    if training is None:\n        training = tf.keras.backend.learning_phase()\n        training = bool(tf.keras.backend.get_value(training))\n\n    if training:\n        alpha = tf.random.uniform(\n            tf.shape(x), minval=lower, maxval=upper, dtype=x.dtype, seed=seed\n        )\n    else:\n        alpha = tf.cast((lower + upper) / 2, x.dtype)\n\n    return tf.where(x >= 0, x, alpha * x)\n", "path": "tensorflow_addons/activations/rrelu.py"}], "after_files": [{"content": "# Copyright 2019 The TensorFlow Authors. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n# ==============================================================================\n\"\"\"Implements GIoU loss.\"\"\"\n\nimport tensorflow as tf\n\nfrom tensorflow_addons.utils.types import TensorLike\nfrom typing import Optional\nfrom typeguard import typechecked\n\n\n@tf.keras.utils.register_keras_serializable(package='Addons')\nclass GIoULoss(tf.keras.losses.Loss):\n    \"\"\"Implements the GIoU loss function.\n\n    GIoU loss was first introduced in the\n    [Generalized Intersection over Union:\n    A Metric and A Loss for Bounding Box Regression]\n    (https://giou.stanford.edu/GIoU.pdf).\n    GIoU is an enhancement for models which use IoU in object detection.\n\n    Usage:\n\n    ```python\n    gl = tfa.losses.GIoULoss()\n    boxes1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]])\n    boxes2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0]])\n    loss = gl(boxes1, boxes2)\n    print('Loss: ', loss.numpy())  # Loss: [1.07500000298023224, 1.9333333373069763]\n    ```\n    Usage with tf.keras API:\n\n    ```python\n    model = tf.keras.Model(inputs, outputs)\n    model.compile('sgd', loss=tfa.losses.GIoULoss())\n    ```\n\n    Args:\n      mode: one of ['giou', 'iou'], decided to calculate GIoU or IoU loss.\n    \"\"\"\n    @typechecked\n    def __init__(self,\n                 mode: str = 'giou',\n                 reduction: str = tf.keras.losses.Reduction.AUTO,\n                 name: Optional[str] = 'giou_loss'):\n        if mode not in ['giou', 'iou']:\n            raise ValueError(\"Value of mode should be 'iou' or 'giou'\")\n        super().__init__(name=name, reduction=reduction)\n        self.mode = mode\n\n    def get_config(self):\n        base_config = super().get_config()\n        base_config['mode'] = self.mode\n        return base_config\n\n    def call(self,\n             y_true,\n             y_pred):\n        return giou_loss(y_true, y_pred, mode=self.mode)\n\n\n@tf.keras.utils.register_keras_serializable(package='Addons')\n@tf.function\ndef giou_loss(y_true: TensorLike,\n              y_pred: TensorLike,\n              mode: str = 'giou') -> tf.Tensor:\n    \"\"\"\n    Args:\n        y_true: true targets tensor. The coordinates of the each bounding\n            box in boxes are encoded as [y_min, x_min, y_max, x_max].\n        y_pred: predictions tensor. The coordinates of the each bounding\n            box in boxes are encoded as [y_min, x_min, y_max, x_max].\n        mode: one of ['giou', 'iou'], decided to calculate GIoU or IoU loss.\n\n    Returns:\n        GIoU loss float `Tensor`.\n    \"\"\"\n    if mode not in ['giou', 'iou']:\n        raise ValueError(\"Value of mode should be 'iou' or 'giou'\")\n    y_pred = tf.convert_to_tensor(y_pred)\n    if not y_pred.dtype.is_floating:\n        y_pred = tf.cast(y_pred, tf.float32)\n    y_true = tf.cast(y_true, y_pred.dtype)\n    giou = _calculate_giou(y_pred, y_true, mode)\n\n    return 1 - giou\n\n\ndef _calculate_giou(b1: TensorLike,\n                    b2: TensorLike,\n                    mode: str = 'giou') -> tf.Tensor:\n    \"\"\"\n    Args:\n        b1: bounding box. The coordinates of the each bounding box in boxes are\n            encoded as [y_min, x_min, y_max, x_max].\n        b2: the other bounding box. The coordinates of the each bounding box\n            in boxes are encoded as [y_min, x_min, y_max, x_max].\n        mode: one of ['giou', 'iou'], decided to calculate GIoU or IoU loss.\n\n    Returns:\n        GIoU loss float `Tensor`.\n    \"\"\"\n    zero = tf.convert_to_tensor(0., b1.dtype)\n    b1_ymin, b1_xmin, b1_ymax, b1_xmax = tf.unstack(b1, 4, axis=-1)\n    b2_ymin, b2_xmin, b2_ymax, b2_xmax = tf.unstack(b2, 4, axis=-1)\n    b1_width = tf.maximum(zero, b1_xmax - b1_xmin)\n    b1_height = tf.maximum(zero, b1_ymax - b1_ymin)\n    b2_width = tf.maximum(zero, b2_xmax - b2_xmin)\n    b2_height = tf.maximum(zero, b2_ymax - b2_ymin)\n    b1_area = b1_width * b1_height\n    b2_area = b2_width * b2_height\n\n    intersect_ymin = tf.maximum(b1_ymin, b2_ymin)\n    intersect_xmin = tf.maximum(b1_xmin, b2_xmin)\n    intersect_ymax = tf.minimum(b1_ymax, b2_ymax)\n    intersect_xmax = tf.minimum(b1_xmax, b2_xmax)\n    intersect_width = tf.maximum(zero, intersect_xmax - intersect_xmin)\n    intersect_height = tf.maximum(zero, intersect_ymax - intersect_ymin)\n    intersect_area = intersect_width * intersect_height\n\n    union_area = b1_area + b2_area - intersect_area\n    iou = tf.math.divide_no_nan(intersect_area, union_area)\n    if mode == 'iou':\n        return iou\n\n    enclose_ymin = tf.minimum(b1_ymin, b2_ymin)\n    enclose_xmin = tf.minimum(b1_xmin, b2_xmin)\n    enclose_ymax = tf.maximum(b1_ymax, b2_ymax)\n    enclose_xmax = tf.maximum(b1_xmax, b2_xmax)\n    enclose_width = tf.maximum(zero, enclose_xmax - enclose_xmin)\n    enclose_height = tf.maximum(zero, enclose_ymax - enclose_ymin)\n    enclose_area = enclose_width * enclose_height\n    giou = iou - tf.math.divide_no_nan(\n        (enclose_area - union_area), enclose_area)\n    return giou\n", "path": "tensorflow_addons/losses/giou_loss.py"}, {"content": "# Copyright 2019 The TensorFlow Authors. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n# ==============================================================================\n\nimport tensorflow as tf\nfrom tensorflow_addons.utils.types import Number\nfrom tensorflow_addons.utils import types\nfrom typing import Optional\n\n\n@tf.keras.utils.register_keras_serializable(package=\"Addons\")\ndef rrelu(\n    x: types.TensorLike,\n    lower: Number = 0.125,\n    upper: Number = 0.3333333333333333,\n    training: Optional[bool] = None,\n    seed: Optional[int] = None,\n) -> tf.Tensor:\n    \"\"\"rrelu function.\n\n    Computes rrelu function:\n    `x if x > 0 else random(lower, upper) * x` or\n    `x if x > 0 else x * (lower + upper) / 2`\n    depending on whether training is enabled.\n\n    See [Empirical Evaluation of Rectified Activations in Convolutional Network](https://arxiv.org/abs/1505.00853).\n\n    Args:\n        x: A `Tensor`. Must be one of the following types:\n            `float16`, `float32`, `float64`.\n        lower: `float`, lower bound for random alpha.\n        upper: `float`, upper bound for random alpha.\n        training: `bool`, indicating whether the `call`\n        is meant for training or inference.\n        seed: `int`, this sets the operation-level seed.\n    Returns:\n        result: A `Tensor`. Has the same type as `x`.\n    \"\"\"\n    x = tf.convert_to_tensor(x)\n    if training is None:\n        training = tf.keras.backend.learning_phase()\n        training = bool(tf.keras.backend.get_value(training))\n\n    if training:\n        alpha = tf.random.uniform(\n            tf.shape(x), minval=lower, maxval=upper, dtype=x.dtype, seed=seed\n        )\n    else:\n        alpha = tf.cast((lower + upper) / 2, x.dtype)\n\n    return tf.where(x >= 0, x, alpha * x)\n", "path": "tensorflow_addons/activations/rrelu.py"}]}