Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: VectorizedBaseImageAugmentationLayer fails to process unbatched bounding_boxes #1512

Closed
james77777778 opened this issue Mar 15, 2023 · 3 comments · Fixed by #1523
Closed
Labels

Comments

@james77777778
Copy link
Contributor

I find that current implementation of VectorizedBaseImageAugmentationLayer fails to correctly process unbatched bounding_boxes

Here is a standalone script:

import tensorflow as tf
from keras_cv.layers import preprocessing

if __name__ == "__main__":
    # construct unbatched input (images, bounding_boxes)
    images = tf.zeros([8, 8, 3])
    bounding_boxes = {
        "boxes": tf.ragged.constant(
            [[0.0, 0.0, 0.0, 0.0], [1.0, 1.0, 1.0, 1.0]],
            dtype=tf.float32,
        ),
        "classes": tf.RaggedTensor.from_tensor(tf.zeros([2, 1])),
    }
    inputs = {"images": images, "bounding_boxes": bounding_boxes}
    # any vectorized layers, take RandomZoom as example
    layer = preprocessing.RandomZoom(0.5, 0.5)
    outputs = layer(inputs, training=True)  # raises ValueError

Reason: VectorizedBaseImageAugmentationLayer tries to tf.expand_dims (tf.squeeze) all values of inputs (outputs) including bounding_boxes which is a dict

if inputs["images"].shape.rank == 3:
for key in list(inputs.keys()):
inputs[key] = tf.expand_dims(inputs[key], axis=0)

if not metadata[BATCHED]:
for key in list(output.keys()):
output[key] = tf.squeeze(output[key], axis=0)

The possible solution:

            # _format_inputs
            for key in list(inputs.keys()):
                if key == BOUNDING_BOXES:
                    inputs[BOUNDING_BOXES]["boxes"] = tf.expand_dims(
                        inputs[BOUNDING_BOXES]["boxes"], axis=0
                    )
                    inputs[BOUNDING_BOXES]["classes"] = tf.expand_dims(
                        inputs[BOUNDING_BOXES]["classes"], axis=0
                    )
                else:
                    inputs[key] = tf.expand_dims(inputs[key], axis=0)
                ...
            # _format_output
            for key in list(output.keys()):
                if key == BOUNDING_BOXES:
                    output[BOUNDING_BOXES]["boxes"] = tf.squeeze(
                        output[BOUNDING_BOXES]["boxes"], axis=0
                    )
                    output[BOUNDING_BOXES]["classes"] = tf.squeeze(
                        output[BOUNDING_BOXES]["classes"], axis=0
                    )
                else:
                    output[key] = tf.squeeze(output[key], axis=0)

It needs to be fixed if we want to implement augment_bounding_boxes for vectorized layers.

I can open the PR once approved.

@james77777778
Copy link
Contributor Author

This bug should influence #1439
@soma2000-lang

The unit test should fail at VectorizedBaseImageAugmentationLayer part

def test_augment_bounding_box_single(self):
image = tf.zeros([20, 20, 3])
boxes = {
"boxes": tf.convert_to_tensor([[0, 0, 1, 1]]),
"classes": tf.convert_to_tensor([0]),
}
input = {"images": image, "bounding_boxes": boxes}
layer = preprocessing.RandomCropAndResize(
target_size=(10, 10),
crop_area_factor=(0.5**2, 0.5**2),
aspect_ratio_factor=(1.0, 1.0),
bounding_box_format="rel_xyxy",
)
output = layer(input, training=True)

@soma2000-lang
Copy link
Contributor

soma2000-lang commented Mar 15, 2023

@james77777778 thanks for flagging ,yes some of the tests are failing due to this.

@LukeWood
Copy link
Contributor

Go ahead @james77777778 - thanks for filing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants