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

feat: Changes related to Stage 1 - TF backend compatibility check for Vision Transformer on small datasets #18884

Closed
wants to merge 1 commit into from

Conversation

pksX01
Copy link
Contributor

@pksX01 pksX01 commented Dec 5, 2023

This PR caters to address Stage 1: tf.keras backwards compatibility check of the Issue Keras.io examples conversion gameplan #18468 for vitsmall_ds.py example with some essential modification to make script executable error free for tensorflow backend.

Here is the git diff with original file -

diff --git a/examples/vision/vit_small_ds.py b/examples/vision/vit_small_ds.py
index 7fd77de..31be894 100644
--- a/examples/vision/vit_small_ds.py
+++ b/examples/vision/vit_small_ds.py
@@ -5,6 +5,7 @@ Date created: 2022/01/07
 Last modified: 2022/01/10
 Description: Training a ViT from scratch on smaller datasets with shifted patch tokenization and locality self-attention.
 Accelerator: GPU
+Keras 3 Conversion initiated by: [Pavan Kumar Singh](https://github.com/pksX01)
 """
 """
 ## Introduction
@@ -33,26 +34,20 @@ The main ideas are:
 This example implements the ideas of the paper. A large part of this
 example is inspired from
 [Image classification with Vision Transformer](https://keras.io/examples/vision/image_classification_with_vision_transformer/).
-
-_Note_: This example requires TensorFlow 2.6 or higher, as well as
-[TensorFlow Addons](https://www.tensorflow.org/addons), which can be
-installed using the following command:
-
-```python
-pip install -qq -U tensorflow-addons
-```
 """
 """
 ## Setup
 """

+import os
+os.environ["KERAS_BACKEND"] = "tensorflow"
+
 import math
 import numpy as np
 import tensorflow as tf
-from tensorflow import keras
-import tensorflow_addons as tfa
+import keras
 import matplotlib.pyplot as plt
-from tensorflow.keras import layers
+from keras import layers

 # Setting seed for reproducibiltiy
 SEED = 42
@@ -354,7 +349,7 @@ at a later stage.
 """


-class MultiHeadAttentionLSA(tf.keras.layers.MultiHeadAttention):
+class MultiHeadAttentionLSA(keras.layers.MultiHeadAttention):
     def __init__(self, **kwargs):
         super().__init__(**kwargs)
         # The trainable temperature term. The initial value is
@@ -498,7 +493,7 @@ def run_experiment(model):
         warmup_steps=warmup_steps,
     )

-    optimizer = tfa.optimizers.AdamW(
+    optimizer = keras.optimizers.AdamW(
         learning_rate=LEARNING_RATE, weight_decay=WEIGHT_DECAY
     )

@@ -547,7 +542,4 @@ supplementary of the paper.

 I would like to thank [Jarvislabs.ai](https://jarvislabs.ai/) for
 generously helping with GPU credits.
-
-You can use the trained model hosted on [Hugging Face Hub](https://huggingface.co/keras-io/vit_small_ds_v2)
-and try the demo on [Hugging Face Spaces](https://huggingface.co/spaces/keras-io/vit-small-ds).
 """

Copy link

google-cla bot commented Dec 5, 2023

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@codecov-commenter
Copy link

codecov-commenter commented Dec 5, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (10252a9) 63.86% compared to head (56bde42) 79.44%.
Report is 5 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff             @@
##           master   #18884       +/-   ##
===========================================
+ Coverage   63.86%   79.44%   +15.57%     
===========================================
  Files         336      336               
  Lines       34848    34854        +6     
  Branches     6855     6851        -4     
===========================================
+ Hits        22255    27689     +5434     
+ Misses      11121     5584     -5537     
- Partials     1472     1581      +109     
Flag Coverage Δ
keras 79.30% <ø> (+15.44%) ⬆️
keras-jax 61.04% <ø> (?)
keras-numpy 55.96% <ø> (?)
keras-tensorflow 63.21% <ø> (?)
keras-torch 63.84% <ø> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@sachinprasadhs sachinprasadhs added the keras-team-review-pending Pending review by a Keras team member. label Dec 5, 2023
@fchollet
Copy link
Collaborator

fchollet commented Dec 5, 2023

Thanks for the PR! The code changes look good. Please reopen the PR on https://github.com/keras-team/keras-io -- we have moved example handling there. Keras 3 examples were only hosted on keras-team/keras temporarily while keras.io was still focused on Keras 2.

I have updated the instructions in the "call for contributions" pinned issue.

@fchollet fchollet closed this Dec 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
keras-team-review-pending Pending review by a Keras team member. size:L
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants