Skip to content

Commit

Permalink
- Correct pytest complexity
Browse files Browse the repository at this point in the history
- Make bounding box test utils use 256,256 image size
  • Loading branch information
sineeli committed Aug 16, 2024
1 parent e1d89e7 commit 58178c6
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 51 deletions.
6 changes: 4 additions & 2 deletions keras_cv/src/models/object_detection/__test_utils__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@


def _create_bounding_box_dataset(
bounding_box_format, use_dictionary_box_format=False
bounding_box_format,
image_shape=(256, 256, 3),
use_dictionary_box_format=False,
):
# Just about the easiest dataset you can have, all classes are 0, all boxes
# are exactly the same. [1, 1, 2, 2] are the coordinates in xyxy.
xs = np.random.normal(size=(1, 512, 512, 3))
xs = np.random.normal(size=(1,) + image_shape)
xs = np.tile(xs, [5, 1, 1, 1])

y_classes = np.zeros((5, 3), "float32")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_faster_rcnn_construction(self):
num_classes=80,
bounding_box_format="xyxy",
backbone=keras_cv.models.ResNet18V2Backbone(
input_shape=(512, 512, 3)
input_shape=(256, 256, 3)
),
)
faster_rcnn.compile(
Expand All @@ -50,16 +50,17 @@ def test_faster_rcnn_construction(self):
rpn_classification_loss="BinaryCrossentropy",
)

@pytest.mark.large()
@pytest.mark.skipif(not keras_3(), reason="disabling test for Keras 2")
def test_faster_rcnn_call(self):
faster_rcnn = FasterRCNN(
num_classes=80,
bounding_box_format="xywh",
backbone=keras_cv.models.ResNet18V2Backbone(
input_shape=(512, 512, 3)
input_shape=(256, 256, 3)
),
)
images = np.random.uniform(size=(2, 512, 512, 3))
images = np.random.uniform(size=(2, 256, 256, 3))
_ = faster_rcnn(images)
_ = faster_rcnn.predict(images)

Expand All @@ -69,7 +70,7 @@ def test_wrong_logits(self):
num_classes=80,
bounding_box_format="xywh",
backbone=keras_cv.models.ResNet18V2Backbone(
input_shape=(512, 512, 3)
input_shape=(256, 256, 3)
),
)

Expand All @@ -93,14 +94,15 @@ def test_wrong_logits(self):
),
)

@pytest.mark.large()
@pytest.mark.skipif(not keras_3(), reason="disabling test for Keras 2")
def test_weights_contained_in_trainable_variables(self):
bounding_box_format = "xyxy"
faster_rcnn = FasterRCNN(
num_classes=80,
bounding_box_format=bounding_box_format,
backbone=keras_cv.models.ResNet18V2Backbone(
input_shape=(512, 512, 3)
input_shape=(256, 256, 3)
),
)
faster_rcnn.backbone.trainable = False
Expand All @@ -124,7 +126,7 @@ def test_no_nans(self):
num_classes=80,
bounding_box_format="xyxy",
backbone=keras_cv.models.ResNet18V2Backbone(
input_shape=(512, 512, 3)
input_shape=(256, 256, 3)
),
)
faster_rcnn.compile(
Expand All @@ -136,7 +138,7 @@ def test_no_nans(self):
)

# only a -1 box
xs = np.ones((1, 512, 512, 3), "float32")
xs = np.ones((1, 256, 256, 3), "float32")
ys = {
"classes": np.array([[-1]], "float32"),
"boxes": np.array([[[0, 0, 0, 0]]], "float32"),
Expand All @@ -157,7 +159,7 @@ def test_weights_change(self):
num_classes=80,
bounding_box_format="xyxy",
backbone=keras_cv.models.ResNet18V2Backbone(
input_shape=(512, 512, 3)
input_shape=(256, 256, 3)
),
)
faster_rcnn.compile(
Expand All @@ -174,7 +176,7 @@ def test_weights_change(self):
).batch(5, drop_remainder=True)

# call once
_ = faster_rcnn(ops.ones((1, 512, 512, 3)))
_ = faster_rcnn(ops.ones((1, 256, 256, 3)))
original_fpn_weights = faster_rcnn.feature_pyramid.get_weights()
original_rpn_head_weights = faster_rcnn.rpn_head.get_weights()
original_rcnn_head_weights = faster_rcnn.rcnn_head.get_weights()
Expand Down Expand Up @@ -205,10 +207,10 @@ def test_saved_model(self):
num_classes=80,
bounding_box_format="xyxy",
backbone=keras_cv.models.ResNet18V2Backbone(
input_shape=(512, 512, 3)
input_shape=(256, 256, 3)
),
)
input_batch = ops.ones(shape=(1, 512, 512, 3))
input_batch = ops.ones(shape=(1, 256, 256, 3))
model_output = model(input_batch)
save_path = os.path.join(self.get_temp_dir(), "faster_rcnn.keras")
model.save(save_path)
Expand All @@ -231,9 +233,10 @@ def test_saved_model(self):
# https://github.com/keras-team/keras-cv/pull/1882
@parameterized.parameters(
((2, 640, 384, 3),),
((2, 512, 512, 3),),
((2, 256, 256, 3),),
((2, 128, 128, 3),),
)
@pytest.mark.large
@pytest.mark.skipif(not keras_3(), reason="disabling test for Keras 2")
def test_faster_rcnn_infer(self, batch_shape):
batch_size = batch_shape[0]
Expand All @@ -254,9 +257,10 @@ def test_faster_rcnn_infer(self, batch_shape):

@parameterized.parameters(
((2, 640, 384, 3),),
((2, 512, 512, 3),),
((2, 256, 256, 3),),
((2, 128, 128, 3),),
)
@pytest.mark.large
@pytest.mark.skipif(not keras_3(), reason="disabling test for Keras 2")
def test_faster_rcnn_train(self, batch_shape):
batch_size = batch_shape[0]
Expand All @@ -280,7 +284,7 @@ def test_invalid_compile(self):
num_classes=80,
bounding_box_format="yxyx",
backbone=keras_cv.models.ResNet18V2Backbone(
input_shape=(512, 512, 3)
input_shape=(256, 256, 3)
),
)
with self.assertRaisesRegex(ValueError, "expects"):
Expand All @@ -302,7 +306,7 @@ def test_faster_rcnn_with_dictionary_input_format(self):
num_classes=20,
bounding_box_format="xywh",
backbone=keras_cv.models.ResNet18V2Backbone(
input_shape=(512, 512, 3)
input_shape=(256, 256, 3)
),
)

Expand Down Expand Up @@ -330,7 +334,7 @@ def test_fit_with_no_valid_gt_bbox(self):
num_classes=20,
bounding_box_format=bounding_box_format,
backbone=keras_cv.models.ResNet18V2Backbone(
input_shape=(512, 512, 3)
input_shape=(256, 256, 3)
),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ class FeaturePyramidTest(TestCase):
@pytest.mark.skipif(not keras_3(), reason="disabling test for Keras 2")
def test_return_type_dict(self):
layer = FeaturePyramid(min_level=2, max_level=5)
c2 = np.ones([2, 64, 64, 3])
c3 = np.ones([2, 32, 32, 3])
c4 = np.ones([2, 16, 16, 3])
c5 = np.ones([2, 8, 8, 3])
c2 = np.ones([2, 32, 32, 3])
c3 = np.ones([2, 16, 16, 3])
c4 = np.ones([2, 8, 8, 3])
c5 = np.ones([2, 4, 4, 3])

inputs = {"P2": c2, "P3": c3, "P4": c4, "P5": c5}
output = layer(inputs)
Expand All @@ -38,10 +38,10 @@ def test_return_type_dict(self):
@pytest.mark.skipif(not keras_3(), reason="disabling test for Keras 2")
def test_result_shapes(self):
layer = FeaturePyramid(min_level=2, max_level=5)
c2 = np.ones([2, 64, 64, 3])
c3 = np.ones([2, 32, 32, 3])
c4 = np.ones([2, 16, 16, 3])
c5 = np.ones([2, 8, 8, 3])
c2 = np.ones([2, 32, 32, 3])
c3 = np.ones([2, 16, 16, 3])
c4 = np.ones([2, 8, 8, 3])
c5 = np.ones([2, 4, 4, 3])

inputs = {"P2": c2, "P3": c3, "P4": c4, "P5": c5}
output = layer(inputs)
Expand All @@ -68,10 +68,10 @@ def test_result_shapes(self):
def test_with_keras_input_tensor(self):
# This mimic the model building with Backbone network
layer = FeaturePyramid(min_level=2, max_level=5)
c2 = keras.layers.Input([64, 64, 3])
c3 = keras.layers.Input([32, 32, 3])
c4 = keras.layers.Input([16, 16, 3])
c5 = keras.layers.Input([8, 8, 3])
c2 = keras.layers.Input([32, 32, 3])
c3 = keras.layers.Input([16, 16, 3])
c4 = keras.layers.Input([8, 8, 3])
c5 = keras.layers.Input([4, 4, 3])

inputs = {"P2": c2, "P3": c3, "P4": c4, "P5": c5}
output = layer(inputs)
Expand Down Expand Up @@ -126,10 +126,10 @@ def test_invalid_output_layers(self):
def test_invalid_input_features(self):
layer = FeaturePyramid(min_level=2, max_level=5)

c2 = np.ones([2, 64, 64, 3])
c3 = np.ones([2, 32, 32, 3])
c4 = np.ones([2, 16, 16, 3])
c5 = np.ones([2, 8, 8, 3])
c2 = np.ones([2, 32, 32, 3])
c3 = np.ones([2, 16, 16, 3])
c4 = np.ones([2, 8, 8, 3])
c5 = np.ones([2, 4, 4, 3])
inputs = {"P2": c2, "P3": c3, "P4": c4, "P5": c5}
# Build required for Keas 3
_ = layer(inputs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

class RCNNHeadTest(TestCase):
@parameterized.parameters(
(2, 512, 20, 7, 256),
(1, 1000, 80, 14, 512),
(2, 256, 20, 7, 256),
(1, 512, 80, 14, 512),
)
@pytest.mark.skipif(not keras_3(), reason="disabling test for Keras 2")
def test_rcnn_head_output_shapes(
Expand Down
30 changes: 15 additions & 15 deletions keras_cv/src/models/object_detection/faster_rcnn/rpn_head_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ def test_return_type_dict(
self,
):
layer = RPNHead()
c2 = ops.ones([2, 128, 128, 256])
c3 = ops.ones([2, 64, 64, 256])
c4 = ops.ones([2, 32, 32, 256])
c5 = ops.ones([2, 16, 16, 256])
c6 = ops.ones([2, 8, 8, 256])
c2 = ops.ones([2, 64, 64, 256])
c3 = ops.ones([2, 32, 32, 256])
c4 = ops.ones([2, 16, 16, 256])
c5 = ops.ones([2, 8, 8, 256])
c6 = ops.ones([2, 4, 4, 256])

inputs = {"P2": c2, "P3": c3, "P4": c4, "P5": c5, "P6": c6}
rpn_boxes, rpn_scores = layer(inputs)
Expand All @@ -48,11 +48,11 @@ def test_return_type_dict(
@pytest.mark.skipif(not keras_3(), reason="disabling test for Keras 2")
def test_return_type_list(self):
layer = RPNHead()
c2 = ops.ones([2, 128, 128, 256])
c3 = ops.ones([2, 64, 64, 256])
c4 = ops.ones([2, 32, 32, 256])
c5 = ops.ones([2, 16, 16, 256])
c6 = ops.ones([2, 8, 8, 256])
c2 = ops.ones([2, 64, 64, 256])
c3 = ops.ones([2, 32, 32, 256])
c4 = ops.ones([2, 16, 16, 256])
c5 = ops.ones([2, 8, 8, 256])
c6 = ops.ones([2, 4, 4, 256])

inputs = [c2, c3, c4, c5, c6]
rpn_boxes, rpn_scores = layer(inputs)
Expand All @@ -66,11 +66,11 @@ def test_return_type_list(self):
)
def test_with_keras_input_tensor_and_num_anchors(self, num_anchors):
layer = RPNHead(num_anchors_per_location=num_anchors)
c2 = keras.layers.Input([128, 128, 256])
c3 = keras.layers.Input([64, 64, 256])
c4 = keras.layers.Input([32, 32, 256])
c5 = keras.layers.Input([16, 16, 256])
c6 = keras.layers.Input([8, 8, 256])
c2 = keras.layers.Input([64, 64, 256])
c3 = keras.layers.Input([32, 32, 256])
c4 = keras.layers.Input([16, 16, 256])
c5 = keras.layers.Input([8, 8, 256])
c6 = keras.layers.Input([4, 4, 256])

inputs = {"P2": c2, "P3": c3, "P4": c4, "P5": c5, "P6": c6}
rpn_boxes, rpn_scores = layer(inputs)
Expand Down

0 comments on commit 58178c6

Please sign in to comment.