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

Port the ImageClassifier guide to keras_core #608

Merged
merged 4 commits into from
Jul 31, 2023

Conversation

tirthasheshpatel
Copy link
Contributor

@tirthasheshpatel tirthasheshpatel commented Jul 25, 2023

Continuing #273.

This PR ports the ImageClassifier guide on keras.io to keras_core. It uses the keras_cv.backend module to import Keras which provides support for both tf.keras and keras_core in a backend-agnostic manner.

To run the guide on Colab, use the core branch from Ian's fork of Keras-CV where we are porting backbones to keras_core.

cc @ianstenbit

## Multi-Backend Support

Keras-CV's `ImageClassifier` model supports several backends like Jax, PyTorch, and TensorFlow with the help of `keras_core`. To enable multi-backend support in Keras-CV, set the `KERAS_CV_MULTI_BACKEND` environment variable. We can then switch between different backends by setting the `KERAS_BACKEND` environment variable. Currently, `tensorflow`, `jax`, and `torch` are supported.
KerasCV's `ImageClassifier` model supports several backends like Jax, PyTorch,
Copy link
Contributor

Choose a reason for hiding this comment

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

JAX

and TensorFlow with the help of `keras_core`. To enable multi-backend support
in KerasCV, set the `KERAS_CV_MULTI_BACKEND` environment variable. We can
then switch between different backends by setting the `KERAS_BACKEND`
environment variable. Currently, `tensorflow`, `jax`, and `torch` are
Copy link
Contributor

Choose a reason for hiding this comment

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

Use backticks + double quotes for strings

@tirthasheshpatel tirthasheshpatel changed the base branch from image-classifier-demo to main July 26, 2023 13:17
@tirthasheshpatel tirthasheshpatel changed the title Address reviews for "Port the ImageClassifier guide to keras_core" Port the ImageClassifier guide to keras_core Jul 26, 2023

"""Now let's apply our final augmenter to the training data:"""

augmenter = tf.keras.Sequential(augmenters)
Copy link
Contributor

Choose a reason for hiding this comment

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

There should be no tf.keras at all

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's use the Augmenter which we're adding back to KerasCV in keras-team/keras-cv#1978



def preprocess_inputs(image, label):
image = tf.cast(image, tf.float32)
Copy link
Contributor

Choose a reason for hiding this comment

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

Since the example has quite a bit of TF-only APIs...

  1. We could reduce or remove the TF dependency and keep it in the backend-agnostic folder.
  2. We could move it to the TF-only folder.

Copy link
Contributor

Choose a reason for hiding this comment

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

The TF components are just for preprocessing which for now has to live in tf.data. I'm fine with moving it to TF-only, but I would expect this to work fine with all backends since this is constrained to the tf.data parts of the example

Copy link
Contributor Author

Choose a reason for hiding this comment

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

+1 for what @ianstenbit said. I am OK with moving this under TF-only though, if you prefer @fchollet.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think at minima we should:

  • drop the dependency on tf.keras.Sequential and just use a list or Augmenter
  • drop the dependency on tf.one_hot. You can use a CategoryEncoding layer for that
  • remove the tf.cast to float (unnecessary)
  • remove import tensorflow and do from tensorflow import data as tf_data

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was trying your suggestions out @fchollet and seems like we would still need the tf import for one hot encoding since CategoryEncoding doesn't accept float32 labels (so, even if we use it, we'd still need to cast labels to integers).

Other than that, everything else works. Let me know if this is OK from your side!

Copy link
Contributor

@fchollet fchollet left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@fchollet fchollet merged commit 267c550 into keras-team:main Jul 31, 2023
@fchollet
Copy link
Contributor

Can you also open a PR for this on keras-team/keras-io?

@tirthasheshpatel
Copy link
Contributor Author

Sure, I still had one change in flight but I'll submit a follow-up! I will also submit a PR on keras.io.

Thanks for the reviews @fchollet @ianstenbit!

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.

3 participants