Skip to content

Commit c1c1cb3

Browse files
committed
Fix all linting errors
1 parent 0d2209b commit c1c1cb3

File tree

16 files changed

+191
-116
lines changed

16 files changed

+191
-116
lines changed

examples/layers/preprocessing/cut_mix_demo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""cut_mix_demo.py shows how to use the CutMix preprocessing layer.
22
3-
Operates on the oxford_flowers102 dataset. In this script the flowers
4-
are loaded, then are passed through the preprocessing layers.
3+
Operates on the oxford_flowers102 dataset. In this script the flowers
4+
are loaded, then are passed through the preprocessing layers.
55
Finally, they are shown using matplotlib.
66
"""
77
import matplotlib.pyplot as plt

examples/layers/preprocessing/mix_up_demo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""mix_up_demo.py shows how to use the MixUp preprocessing layer.
22
3-
Uses the oxford_flowers102 dataset. In this script the flowers
4-
are loaded, then are passed through the preprocessing layers.
3+
Uses the oxford_flowers102 dataset. In this script the flowers
4+
are loaded, then are passed through the preprocessing layers.
55
Finally, they are shown using matplotlib.
66
"""
77
import matplotlib.pyplot as plt

keras_cv/layers/preprocessing/cut_mix.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
import tensorflow as tf
15-
import tensorflow.keras as keras
1615
import tensorflow.keras.layers as layers
1716
from tensorflow.python.platform import tf_logging as logging
1817

@@ -22,13 +21,14 @@ class CutMix(layers.Layer):
2221
2322
Args:
2423
rate: Float between 0 and 1. The fraction of samples to augment.
25-
alpha: Float between 0 and 1. Inverse scale parameter for the gamma distribution.
26-
This controls the shape of the distribution from which the smoothing values are
27-
sampled. Defaults 1.0, which is a recommended value when training an imagenet1k
28-
classification model.
29-
label_smoothing: Float in [0, 1]. When > 0, label values are smoothed, meaning the
30-
confidence on label values are relaxed. e.g. label_smoothing=0.2 means that we
31-
will use a value of 0.1 for label 0 and 0.9 for label 1. Defaults 0.0.
24+
alpha: Float between 0 and 1. Inverse scale parameter for the gamma
25+
distribution. This controls the shape of the distribution from which the
26+
smoothing values are sampled. Defaults 1.0, which is a recommended value
27+
when training an imagenet1k classification model.
28+
label_smoothing: Float in [0, 1]. When > 0, label values are smoothed,
29+
meaning the confidence on label values are relaxed. e.g.
30+
label_smoothing=0.2 means that we will use a value of 0.1 for label 0 and
31+
0.9 for label 1. Defaults 0.0.
3232
References:
3333
[CutMix paper]( https://arxiv.org/abs/1905.04899).
3434
@@ -57,17 +57,21 @@ def call(self, images, labels):
5757
"""call method for the CutMix layer.
5858
5959
Args:
60-
images: Tensor representing images of shape [batch_size, width, height, channels], with dtype tf.float32.
61-
labels: One hot encoded tensor of labels for the images, with dtype tf.float32.
60+
images: Tensor representing images of shape:
61+
[batch_size, width, height, channels], with dtype tf.float32.
62+
labels: One hot encoded tensor of labels for the images, with dtype
63+
tf.float32.
6264
Returns:
6365
images: augmented images, same shape as input.
64-
labels: updated labels with both label smoothing and the cutmix updates applied.
66+
labels: updated labels with both label smoothing and the cutmix updates
67+
applied.
6568
"""
6669

6770
if tf.shape(images)[0] == 1:
6871
logging.warning(
69-
"CutMix received a single image to `call`. The layer relies on combining multiple examples, "
70-
"and as such will not behave as expected. Please call the layer with 2 or more samples."
72+
"CutMix received a single image to `call`. The layer relies on "
73+
"combining multiple examples, and as such will not behave as "
74+
"expected. Please call the layer with 2 or more samples."
7175
)
7276

7377
augment_cond = tf.less(

keras_cv/layers/preprocessing/mix_up.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
import tensorflow as tf
15-
import tensorflow.keras as keras
1615
import tensorflow.keras.layers as layers
1716
from tensorflow.python.platform import tf_logging as logging
1817

@@ -22,13 +21,14 @@ class MixUp(layers.Layer):
2221
2322
Args:
2423
rate: Float between 0 and 1. The fraction of samples to augment.
25-
alpha: Float between 0 and 1. Inverse scale parameter for the gamma distribution.
26-
This controls the shape of the distribution from which the smoothing values are
27-
sampled. Defaults 0.2, which is a recommended value when training an imagenet1k
28-
classification model.
29-
label_smoothing: Float in [0, 1]. When > 0, label values are smoothed, meaning the
30-
confidence on label values are relaxed. e.g. label_smoothing=0.2 means that we
31-
will use a value of 0.1 for label 0 and 0.9 for label 1. Defaults 0.0.
24+
alpha: Float between 0 and 1. Inverse scale parameter for the gamma
25+
distribution. This controls the shape of the distribution from which the
26+
smoothing values are sampled. Defaults 0.2, which is a recommended value
27+
when training an imagenet1k classification model.
28+
label_smoothing: Float in [0, 1]. When > 0, label values are smoothed,
29+
meaning the confidence on label values are relaxed. e.g.
30+
label_smoothing=0.2 means that we will use a value of 0.1 for label 0 and
31+
0.9 for label 1. Defaults 0.0.
3232
References:
3333
[MixUp paper](https://arxiv.org/abs/1710.09412).
3434
@@ -57,17 +57,21 @@ def call(self, images, labels):
5757
"""call method for the MixUp layer.
5858
5959
Args:
60-
images: Tensor representing images of shape [batch_size, width, height, channels], with dtype tf.float32.
61-
labels: One hot encoded tensor of labels for the images, with dtype tf.float32.
60+
images: Tensor representing images of shape
61+
[batch_size, width, height, channels], with dtype tf.float32.
62+
labels: One hot encoded tensor of labels for the images, with dtype
63+
tf.float32.
6264
Returns:
6365
images: augmented images, same shape as input.
64-
labels: updated labels with both label smoothing and the cutmix updates applied.
66+
labels: updated labels with both label smoothing and the cutmix updates
67+
applied.
6568
"""
6669

6770
if tf.shape(images)[0] == 1:
6871
logging.warning(
69-
"MixUp received a single image to `call`. The layer relies on combining multiple examples, "
70-
"and as such will not behave as expected. Please call the layer with 2 or more samples."
72+
"CutMix received a single image to `call`. The layer relies on "
73+
"combining multiple examples, and as such will not behave as "
74+
"expected. Please call the layer with 2 or more samples."
7175
)
7276

7377
augment_cond = tf.less(

keras_cv/metrics/coco/base.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
# Copyright 2022 The KerasCV Authors
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+
# https://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.
114
import tensorflow as tf
215
import tensorflow.keras as keras
316
import tensorflow.keras.initializers as initializers
@@ -11,22 +24,23 @@ class COCOBase(keras.metrics.Metric):
1124
"""COCOBase serves as a base for COCORecall and COCOPrecision.
1225
1326
Args:
14-
iou_thresholds: defaults to [0.5:0.05:0.95]. Dimension T=len(iou_thresholds), defaults to 10.
15-
category_ids: no default, users must provide. K=len(category_ids)
27+
iou_thresholds: defaults to [0.5:0.05:0.95].
28+
category_ids: no default, users must provide.
1629
area_range: area range to consider bounding boxes in. Defaults to all.
1730
max_detections: number of maximum detections a model is allowed to make.
1831
1932
Internally the COCOBase class tracks the following values:
20-
21-
- TruePositives: tf.Tensor with shape [TxK] precision for every evaluation setting.
22-
- FalsePositives: tf.Tensor with shape [TxK] precision for every evaluation setting.
23-
- GroundTruthBoxes: tf.Tensor with shape [K] max recall for every evaluation setting.
33+
T=len(iou_thresholds)
34+
K=len(category_ids)
35+
- TruePositives: tf.Tensor with shape [TxK].
36+
- FalsePositives: tf.Tensor with shape [TxK].
37+
- GroundTruthBoxes: tf.Tensor with shape [K].
2438
"""
2539

2640
def __init__(
2741
self,
42+
category_ids,
2843
iou_thresholds=None,
29-
category_ids=None,
3044
area_range=(0, 1e9 ** 2),
3145
max_detections=100,
3246
**kwargs
@@ -39,7 +53,7 @@ def __init__(
3953
self.iou_thresholds = self._add_constant_weight(
4054
"iou_thresholds", self._user_iou_thresholds
4155
)
42-
# TODO(lukewood): support inference of category_ids based on update_state calls.
56+
# TODO(lukewood): support inference of category_ids based on update_state.
4357
self.category_ids = self._add_constant_weight("category_ids", category_ids)
4458

4559
self.area_range = area_range
@@ -201,8 +215,9 @@ def _match_boxes(self, y_true, y_pred, threshold, ious):
201215
# TODO(lukewood): update clause to account for gtIg
202216
# if m > -1 and gtIg[m] == 0 and gtIg[gind] == 1:
203217

204-
if not ious[gt_idx, detection_idx] >= threshold:
218+
if not ious[gt_idx, detection_idx] >= iou:
205219
continue
220+
206221
iou = ious[gt_idx, detection_idx]
207222
match_index = gt_idx
208223

keras_cv/metrics/coco/base_test.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
1+
# Copyright 2022 The KerasCV Authors
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+
# https://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.
114
"""Tests for bbox functions."""
215

3-
import numpy as np
416
import tensorflow as tf
517

618
from keras_cv.metrics.coco.base import COCOBase

keras_cv/metrics/coco/iou.py

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,29 @@
1+
# Copyright 2022 The KerasCV Authors
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+
# https://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.
114
"""Contains functions to compute ious of bounding boxes."""
215
import tensorflow as tf
316

4-
from keras_cv.util import bbox
5-
6-
7-
def _compute_single_iou(bboxes1, bb2):
8-
"""computes the intersection over union between bboxes1 and bb2.
9-
10-
bboxes1 and bb2 are expected to come in the 'corners' format, or:
11-
[left, right, top, bottom].
12-
For example, the bounding box with it's left side at 100, right side at 200,
13-
top at 101, and bottom at 201 would be represented as:
14-
[100, 200, 101, 201]
15-
16-
Args:
17-
bboxes1: Tensor representing the first bounding box in 'corners' format.
18-
bb2: Tensor representing the second bounding box in 'corners' format.
19-
20-
Returns:
21-
iou: Tensor representing the intersection over union of the two bounding
22-
boxes.
23-
"""
24-
# bounding box indices
25-
26-
intersection_left = tf.math.maximum(bboxes1[bbox.LEFT], bb2[bbox.LEFT])
27-
intersection_right = tf.math.minimum(bboxes1[bbox.RIGHT], bb2[bbox.RIGHT])
28-
29-
intersection_top = tf.math.maximum(bboxes1[bbox.TOP], bb2[bbox.TOP])
30-
intersection_bottom = tf.math.minimum(bboxes1[bbox.BOTTOM], bb2[bbox.BOTTOM])
31-
32-
area_intersection = _area(
33-
intersection_left, intersection_right, intersection_top, intersection_bottom
34-
)
35-
area_bboxes1 = _area(
36-
bb1[bbox.LEFT], bb1[bbox.RIGHT], bb1[bbox.TOP], bb1[bbox.BOTTOM]
37-
)
38-
area_bb2 = _area(
39-
bb2[bbox.LEFT], bb2[bbox.RIGHT], bb2[bbox.TOP], bb2[bbox.BOTTOM]
40-
)
41-
42-
area_union = area_bboxes1 + area_bb2 - area_intersection
43-
return tf.math.divide_no_nan(area_intersection, area_union)
44-
4517

4618
def compute_ious_for_image(boxes1, boxes2):
4719
"""computes a lookup table vector containing the ious for a given set boxes.
4820
4921
The lookup vector is to be indexed by [`boxes1_index`,`boxes2_index`].
5022
51-
Bounding boxes are expected to be in the corners format of `[bbox.LEFT, bbox.RIGHT,
52-
bbox.TOP, bbox.BOTTOM]`. For example, the bounding box with it's left side at 100,
53-
bbox.RIGHT side at 200, bbox.TOP at 101, and bbox.BOTTOM at 201 would be represented
54-
as:
23+
Bounding boxes are expected to be in the corners format of
24+
`[bbox.LEFT, bbox.RIGHT, bbox.TOP, bbox.BOTTOM]`. For example, the bounding box
25+
with it's left side at 100, bbox.RIGHT side at 200, bbox.TOP at 101, and
26+
bbox.BOTTOM at 201 would be represented as:
5527
> [100, 200, 101, 201]
5628
5729
Args:

keras_cv/metrics/coco/iou_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
# Copyright 2022 The KerasCV Authors
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+
# https://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.
114
"""Tests for iou functions."""
215

316
import numpy as np

keras_cv/metrics/coco/numerical_tests/recall_correctness_test.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,28 @@
1818
import numpy as np
1919
import tensorflow as tf
2020

21-
from keras_cv.metrics.coco import iou as iou_lib
2221
from keras_cv.metrics.coco.recall import COCORecall
2322
from keras_cv.util import bbox
2423

2524
SAMPLE_FILE = os.path.dirname(os.path.abspath(__file__)) + "/sample_boxes.npz"
2625

2726

2827
class RecallCorrectnessTest(tf.test.TestCase):
29-
"""Unit tests that test Keras COCO metric results against the known good ones of cocoeval.py.
30-
The bounding boxes in sample_boxes.npz were given to cocoeval.py which output the following values:
31-
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.643
32-
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 1.000
33-
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.729
34-
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.644
35-
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.633
36-
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.689
37-
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.504
38-
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.660
39-
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.660
40-
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.652
41-
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.644
42-
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.696
28+
"""Unit tests that test Keras COCO metric results against the known good ones of
29+
cocoeval.py. The bounding boxes in sample_boxes.npz were given to cocoeval.py
30+
which output the following values:
31+
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.643
32+
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 1.000
33+
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.729
34+
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.644
35+
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.633
36+
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.689
37+
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.504
38+
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.660
39+
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.660
40+
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.652
41+
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.644
42+
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.696
4343
"""
4444

4545
def test_recall_correctness_maxdets_1(self):

keras_cv/metrics/coco/recall.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
# Copyright 2022 The KerasCV Authors
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+
# https://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.
114
import tensorflow as tf
215

316
from keras_cv.metrics.coco.base import COCOBase

0 commit comments

Comments
 (0)