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

Added vectorized Auto Contrast implementation. #1394

Merged
merged 6 commits into from
Feb 17, 2023

Conversation

sebastian-sz
Copy link
Contributor

What does this PR do?

Modifies AutoContrast to use Vectorized base image layer.

Fixes #1382

Following #1392 I also added the benchmark and numerical check (compare old and new).
Benchmark results:
autocontrast_benchmark_2

autocontrast_benchmark_1

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

@LukeWood
Copy link
Contributor

What does this PR do?

Modifies AutoContrast to use Vectorized base image layer.

Fixes #1382

Following #1392 I also added the benchmark and numerical check (compare old and new). Benchmark results: autocontrast_benchmark_2

autocontrast_benchmark_1

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 any idea why the original is faster in some cases? This is surprising to me, perhaps vectorized-map was performing quite well here?

I assume this is still better when they pass bounding boxes in too though - as it would fallback to map_fn.

@bhack
Copy link
Contributor

bhack commented Feb 14, 2023

@sebastian-sz any idea why the original is faster in some cases? This is surprising to me, perhaps vectorized-map was performing quite well here?

Cause it was never in the fallback list for missing converters and it was not using tf.image OPS:
#291 (comment)

You could already run AutoContrast on master with disabling fallback and enabling warning:

        if self.auto_vectorize:
            return tf.vectorized_map(func, inputs, fallback_to_while_loop=False, warn=True)

@LukeWood
Copy link
Contributor

@sebastian-sz any idea why the original is faster in some cases? This is surprising to me, perhaps vectorized-map was performing quite well here?

Cause it was never in the fallback list for missing converters and it was not using tf.image OPS: #291 (comment)

You could already run AutoContrast on master with disabling fallback and enabling warning:

        if self.auto_vectorize:
            return tf.vectorized_map(func, inputs, fallback_to_while_loop=False, warn=True)

that explains why its the same speed, but I'm still surprised that its faster. Maybe its vectorizing something we didn't realize we could vectorize.

@sebastian-sz
Copy link
Contributor Author

@bhack @LukeWood
I did inspect both layers, but unfortunately, I cannot pinpoint the place where the difference in execution time comes from.

These differences are not huge (~5ms), yet still, they exist.

@LukeWood
Copy link
Contributor

@bhack @LukeWood I did inspect both layers, but unfortunately, I cannot pinpoint the place where the difference in execution time comes from.

These differences are not huge (~5ms), yet still, they exist.

yeah they're pretty tiny. Also - the one that relies on vectorized_map will use map_fn when bounding boxes are provided, so yours will outperform in this case by a good margin. I'm totally happy w/ this performance personally.

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.

LGTM. Thank you @sebastian-sz!

@LukeWood PTAL and let me know what you think about the a/b testing comment on #1392 as that applies here as well

@ianstenbit ianstenbit requested a review from LukeWood February 15, 2023 18:18
@@ -90,3 +156,11 @@ def test_auto_contrast_properly_converts_value_range(self):

self.assertTrue(tf.math.reduce_any(ys[0] == 0.0))
self.assertTrue(tf.math.reduce_any(ys[0] == 1.0))

def test_is_consistent_with_non_vectorized_implementation(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

@sebastian-sz can you please move this test case to the benchmark, instead of this test case?

Then we can merge -- thanks!

Copy link
Contributor Author

@sebastian-sz sebastian-sz Feb 16, 2023

Choose a reason for hiding this comment

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

@ianstenbit done. Moved consistency test to benchmarks.

@sebastian-sz sebastian-sz requested review from ianstenbit and removed request for LukeWood February 16, 2023 19:49
@ianstenbit
Copy link
Contributor

/gcbrun

@ianstenbit ianstenbit merged commit 37d34bf into keras-team:master Feb 17, 2023
@sebastian-sz sebastian-sz deleted the vectorize-auto-contrast branch February 18, 2023 06:25
ghost pushed a commit to y-vectorfield/keras-cv that referenced this pull request Nov 16, 2023
* Added vectorized Auto Contrast implementation.

* Fixed typos in fn signatures.

* Moved consistency test to benchmarks.

* Override random transformation.

* Lint code.

* Added back the default get_random_transformation_batch.
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 Auto Contrast.
4 participants