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

Vectorized Random Shear. #1537

Merged
merged 5 commits into from
Mar 23, 2023

Conversation

sebastian-sz
Copy link
Contributor

What does this PR do?

Refactors RandomShear to subclass VectorizedBaseImageAugmentationLayer.

This gives performance benefits.

Fixes #1518

I had little error in my previous benchmark. The correct performance numbers are still better but the difference between implementations is smaller.
comparison
comparison_no_old_eager

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue? Please add a link
    to it if that's the case.
  • Did you write any new necessary tests?
  • If this adds a new model, can you run a few training steps on TPU in Colab to ensure that no XLA incompatible OP are used?

Who can review?

@LukeWood @ianstenbit

@sebastian-sz
Copy link
Contributor Author

The test of a different layer fails, will update tomorrow.

@LukeWood
Copy link
Contributor

LGTM @sebastian-sz but can you rebase on #1534 and include results from the demo?

@sebastian-sz sebastian-sz force-pushed the vectorize-random-shear branch from 54b5e21 to f431406 Compare March 22, 2023 07:24
@sebastian-sz
Copy link
Contributor Author

@LukeWood I'm getting errors after rebase. There seems to be an issue when calling keras_cv.bounding_box.convert_format after casting ragged boxes to dense:
Incompatible shapes: [9,5,1] vs. [9,1]

The error is thrown in: _xyxy_to_rel_xyxy:

left, right = left / image_width, right / image_width

Standalone code to reproduce:

import demo_utils
import keras_cv.bounding_box

dataset = demo_utils.load_voc_dataset(bounding_box_format="xyxy")

for d in dataset.take(1):
    d["bounding_boxes"] = keras_cv.bounding_box.to_dense(
        d["bounding_boxes"], default_value=0
    )
    keras_cv.bounding_box.convert_format(
        boxes=d["bounding_boxes"],
        source="xyxy",
        target="rel_xyxy",
        images=d["images"],
    )

The error goes away if I comment out the Ragged check:

if isinstance(boxes, tf.RaggedTensor):
height = tf.expand_dims(height, axis=-1)
width = tf.expand_dims(width, axis=-1)

#            if isinstance(boxes, tf.RaggedTensor):
            height = tf.expand_dims(height, axis=-1)
            width = tf.expand_dims(width, axis=-1)

but this might break something somewhere else. Not sure how to proceed with this.

Copy link
Contributor

@ianstenbit ianstenbit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!
This LGTM -- @LukeWood can you PTAL at the demo issue?

)
from keras_cv.utils import preprocessing


@keras.utils.register_keras_serializable(package="keras_cv")
class RandomShear(BaseImageAugmentationLayer):
@tf.keras.utils.register_keras_serializable(package="keras_cv")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: let's use keras directly and add from tensorflow import keras above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ianstenbit Done.

@LukeWood
Copy link
Contributor

** sebastian-sz ** commented

looks like an unrelated error. I will file a bug for this converter issue, in the meantime can you change the bounding box format used in the demo in a way to avoid this issue (if not possible no worries)

@sebastian-sz
Copy link
Contributor Author

@LukeWood commented out the Ragged check.

Old RandomShear:
base

Vectorized RandomShear:
current

Note that both layers sample different shear values.

@LukeWood
Copy link
Contributor

Thanks @sebastian-sz !

@LukeWood LukeWood merged commit e8c1564 into keras-team:master Mar 23, 2023
ghost pushed a commit to y-vectorfield/keras-cv that referenced this pull request Nov 16, 2023
* Vectorized Random Shear.

* Fixed failing tests.

* Refactored transformarion matrix creation.

* Remove unused import.

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

Successfully merging this pull request may close these issues.

Vectorize Random Shear.
3 participants