Skip to content

Commit 062dcd2

Browse files
committed
:%s/.shape/tf.shape()/g
Replace some shape calls in COCOBase with tf.shape calls to support running in graph mode.
1 parent 0f68153 commit 062dcd2

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

keras_cv/metrics/coco/base.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ def update_state(self, y_true, y_pred, sample_weight=None):
100100
raise NotImplementedError(
101101
"sample_weight is not yet supported in keras_cv COCO metrics."
102102
)
103-
num_images = y_true.shape[0]
103+
num_images = tf.shape(y_true)[0]
104104

105-
num_thresholds = self.iou_thresholds.shape[0]
106-
num_categories = self.category_ids.shape[0]
105+
num_thresholds = tf.shape(self.iou_thresholds)[0]
106+
num_categories = tf.shape(self.category_ids)[0]
107107

108108
# Sort by bbox.CONFIDENCE to make maxDetections easy to compute.
109109
y_pred = utils.sort_bboxes(y_pred, axis=bbox.CONFIDENCE)
@@ -151,9 +151,7 @@ def update_state(self, y_true, y_pred, sample_weight=None):
151151
false_positives = tf.cast(pred_matches == -1, tf.float32)
152152

153153
true_positives_sum = tf.math.reduce_sum(true_positives, axis=-1)
154-
false_positives_sum = tf.math.reduce_sum(
155-
false_positives, axis=-1
156-
)
154+
false_positives_sum = tf.math.reduce_sum(false_positives, axis=-1)
157155

158156
true_positives_update = tf.tensor_scatter_nd_add(
159157
true_positives_update, [indices], [true_positives_sum]

keras_cv/metrics/coco/recall_test.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,43 @@
1515

1616
import numpy as np
1717
import tensorflow as tf
18+
from tensorflow import keras
1819

1920
from keras_cv.metrics.coco.recall import COCORecall
2021

2122

2223
class COCORecallTest(tf.test.TestCase):
23-
def test_recall_area_range_filtering(self):
24+
25+
def test_runs_inside_model(self):
26+
i = keras.layers.Input((None, None, 6))
27+
model = keras.Model(i, i)
28+
2429
recall = COCORecall(
2530
max_detections=100,
2631
category_ids=[1],
27-
area_range=(32 ** 2, 64 ** 2),
32+
area_range=(0, 64 ** 2),
2833
)
2934

3035
# These would match if they were in the area range
31-
y_true = np.array([[[0, 0, 10, 10, 1], [5, 5, 10, 10, 1]]]).astype(
36+
y_true = np.array([[[0, 0, 10, 10, 1], [5, 5, 10, 10, 1]]]).astype(np.float32)
37+
y_pred = np.array([[[0, 0, 10, 10, 1, 1.0], [5, 5, 10, 10, 1, 0.9]]]).astype(
3238
np.float32
3339
)
40+
41+
model.compile(metrics=[recall])
42+
model.evaluate(y_pred, y_true)
43+
44+
self.assertAllEqual(recall.result(), 1.0)
45+
46+
def test_recall_area_range_filtering(self):
47+
recall = COCORecall(
48+
max_detections=100,
49+
category_ids=[1],
50+
area_range=(32 ** 2, 64 ** 2),
51+
)
52+
53+
# These would match if they were in the area range
54+
y_true = np.array([[[0, 0, 10, 10, 1], [5, 5, 10, 10, 1]]]).astype(np.float32)
3455
y_pred = np.array([[[0, 0, 10, 10, 1, 1.0], [5, 5, 10, 10, 1, 0.9]]]).astype(
3556
np.float32
3657
)

0 commit comments

Comments
 (0)