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

Changes related to TF backend compatibility check for Vision Transformer on small datasets #1671

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

pksX01
Copy link
Contributor

@pksX01 pksX01 commented Dec 6, 2023

This PR caters to address changes required to convert vit_small_ds.py to Keras 3 with Tensorflow backend. This PR does not add backend_agnostic changes.

Copy link

google-cla bot commented Dec 6, 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.

@pksX01
Copy link
Contributor Author

pksX01 commented Dec 6, 2023

Hi @sachinprasadhs, I am not sure why CLA check is failing. I and the team which handles corporate CLA matters at my employer, we all tried all steps mentioned. I was thinking you or anyone from google might be able to help me regarding this.

Copy link
Member

@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.

Thanks for the PR! The code looks good -- please also generate the md and ipynb files via the python autogen.py add_example vision/vit_small_ds command (in scripts/ directory)

@pksX01
Copy link
Contributor Author

pksX01 commented Dec 7, 2023

Thanks for the PR! The code looks good -- please also generate the md and ipynb files via the python autogen.py add_example vision/vit_small_ds command (in scripts/ directory)

I am able to generate ipynb file but I am getting below error while generating md file.

ImportError                               Traceback (most recent call last)
Cell In[1], line 8
      6 import numpy as np
      7 import tensorflow as tf
----> 8 import keras
      9 import matplotlib.pyplot as plt
     10 from keras import layers

File ~/Library/Python/3.9/lib/python/site-packages/keras/__init__.py:3
      1 """AUTOGENERATED. DO NOT EDIT."""
----> 3 from keras import __internal__
      4 from keras import activations
      5 from keras import applications

File ~/Library/Python/3.9/lib/python/site-packages/keras/__internal__/__init__.py:3
      1 """AUTOGENERATED. DO NOT EDIT."""
----> 3 from keras.__internal__ import backend
      4 from keras.__internal__ import layers
      5 from keras.__internal__ import losses

File ~/Library/Python/3.9/lib/python/site-packages/keras/__internal__/backend/__init__.py:3
      1 """AUTOGENERATED. DO NOT EDIT."""
----> 3 from keras.src.backend import _initialize_variables as initialize_variables
      4 from keras.src.backend import track_variable

File ~/Library/Python/3.9/lib/python/site-packages/keras/src/__init__.py:21
     15 """Implementation of the Keras API, the high-level API of TensorFlow.
     16 
     17 Detailed documentation and user guides are available at
     18 [keras.io](https://keras.io).
     19 """
     20 from keras.src import distribute
---> 21 from keras.src import models
     22 from keras.src.engine.input_layer import Input
     23 from keras.src.engine.sequential import Sequential

File ~/Library/Python/3.9/lib/python/site-packages/keras/src/models/__init__.py:18
      1 # Copyright 2022 The TensorFlow Authors. All Rights Reserved.
      2 #
      3 # Licensed under the Apache License, Version 2.0 (the "License");
   (...)
     13 # limitations under the License.
     14 # ==============================================================================
     15 """Keras models API."""
---> 18 from keras.src.engine.functional import Functional
     19 from keras.src.engine.sequential import Sequential
     20 from keras.src.engine.training import Model

File ~/Library/Python/3.9/lib/python/site-packages/keras/src/engine/functional.py:25
     21 import warnings
     23 import tensorflow.compat.v2 as tf
---> 25 from keras.src import backend
     26 from keras.src.dtensor import layout_map as layout_map_lib
     27 from keras.src.engine import base_layer

File ~/Library/Python/3.9/lib/python/site-packages/keras/src/backend/__init__.py:9
      3 if backend() == "torch":
      4     # When using the torch backend,
      5     # torch needs to be imported first, otherwise it will segfault
      6     # upon import.
      7     import torch
----> 9 from keras.src.backend.common.dtypes import result_type
     10 from keras.src.backend.common.keras_tensor import KerasTensor
     11 from keras.src.backend.common.keras_tensor import any_symbolic_tensors

File ~/Library/Python/3.9/lib/python/site-packages/keras/src/backend/common/__init__.py:2
      1 from keras.src.backend.common import backend_utils
----> 2 from keras.src.backend.common.dtypes import result_type
      3 from keras.src.backend.common.variables import AutocastScope
      4 from keras.src.backend.common.variables import KerasVariable

File ~/Library/Python/3.9/lib/python/site-packages/keras/src/backend/common/dtypes.py:5
      3 from keras.src import backend
      4 from keras.src.api_export import keras_export
----> 5 from keras.src.backend.common.variables import ALLOWED_DTYPES
      6 from keras.src.backend.common.variables import standardize_dtype
      8 """
      9 We adapted the type promotion lattice from JAX. Ref:
     10 https://github.com/google/jax/blob/main/jax/_src/dtypes.py
     11 """

File ~/Library/Python/3.9/lib/python/site-packages/keras/src/backend/common/variables.py:9
      7 from keras.src.backend.common.stateless_scope import get_stateless_scope
      8 from keras.src.backend.common.stateless_scope import in_stateless_scope
----> 9 from keras.src.utils.naming import auto_name
     12 class KerasVariable:
     13     def __init__(
     14         self, initializer, shape=None, dtype=None, trainable=True, name=None
     15     ):

File ~/Library/Python/3.9/lib/python/site-packages/keras/src/utils/__init__.py:53
     50 from keras.src.utils.data_utils import get_file
     52 # Preprocessing utils
---> 53 from keras.src.utils.feature_space import FeatureSpace
     55 # Internal
     56 from keras.src.utils.layer_utils import get_source_inputs

File ~/Library/Python/3.9/lib/python/site-packages/keras/src/utils/feature_space.py:20
     17 import tensorflow.compat.v2 as tf
     19 from keras.src import backend
---> 20 from keras.src.engine import base_layer
     21 from keras.src.saving import saving_lib
     22 from keras.src.saving import serialization_lib

File ~/Library/Python/3.9/lib/python/site-packages/keras/src/engine/base_layer.py:32
     29 import tensorflow.compat.v2 as tf
     31 from keras.src import backend
---> 32 from keras.src import constraints
     33 from keras.src import initializers
     34 from keras.src import regularizers

File ~/Library/Python/3.9/lib/python/site-packages/keras/src/constraints/__init__.py:4
      1 import inspect
      3 from keras.src.api_export import keras_export
----> 4 from keras.src.constraints.constraints import Constraint
      5 from keras.src.constraints.constraints import MaxNorm
      6 from keras.src.constraints.constraints import MinMaxNorm

File ~/Library/Python/3.9/lib/python/site-packages/keras/src/constraints/constraints.py:2
      1 from keras.src import backend
----> 2 from keras.src import ops
      3 from keras.src.api_export import keras_export
      6 @keras_export("keras.constraints.Constraint")
      7 class Constraint:

File ~/Library/Python/3.9/lib/python/site-packages/keras/src/ops/__init__.py:5
      1 # from keras.src.ops.numpy import Matmul, matmul
      2 # from keras.src.ops.numpy import Add, add
      3 # from keras.src.ops.numpy import Multiply, multiply
----> 5 from keras.src.backend import cast
      6 from keras.src.backend import cond
      7 from keras.src.backend import is_tensor

ImportError: cannot import name 'cast' from partially initialized module 'keras.src.backend' (most likely due to a circular import) (/Users/rudra/Library/Python/3.9/lib/python/site-packages/keras/src/backend/__init__.py)

Traceback (most recent call last):
  File "/Users/rudra/Tech/open_source/keras-io/scripts/autogen.py", line 1152, in <module>
    keras_io.add_example(
  File "/Users/rudra/Tech/open_source/keras-io/scripts/autogen.py", line 250, in add_example
    tutobooks.py_to_md(py_path, nb_path, md_path, img_dir, working_dir=working_dir)
  File "/Users/rudra/Tech/open_source/keras-io/scripts/tutobooks.py", line 283, in py_to_md
    nb_to_md(nb_path, md_path, img_dir, working_dir=working_dir)
  File "/Users/rudra/Tech/open_source/keras-io/scripts/tutobooks.py", line 275, in nb_to_md
    raise RuntimeError(
RuntimeError: An error was encountered when attempting to run the notebook. See logs for details.

When I updated to Tensorflow to 2.15.0 and Keras to 3.0.0 then it created md file successfully.

We might need to update requirements.txt file.

@fchollet
Copy link
Member

fchollet commented Dec 7, 2023

Can you add the md and ipynb files to the PR?

@pksX01
Copy link
Contributor Author

pksX01 commented Dec 10, 2023

Sure, I will be adding soon. I was able to generate but I noticed I had reduced dataset size and number of epochs for faster experimentation. I tried with full dataset and full epochs but kernal stopped after 24 hours in my macbook automatically at 46th epoch out of 50 epochs. So, I need to restart the generation process again.

@fchollet
Copy link
Member

kernal stopped after 24 hours in my macbook automatically at 46th epoch out of 50 epochs

You're going to need a GPU VM (like a V100), running on CPU will take too much time.

Copy link

This PR is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you.

@github-actions github-actions bot added the stale label Dec 27, 2023
@pksX01
Copy link
Contributor Author

pksX01 commented Jan 2, 2024

I tried GPU version of tensorflow for mac M1 metal but still taking lot of time and getting crashed after 12 hours. I also rented GPU from jarvislabs.ai but they are not providing latest version of tensorflow in their image. It seems pretty hard for me to generate .md file due to not having access to Nvidia GPU.

@pksX01
Copy link
Contributor Author

pksX01 commented Feb 16, 2024

@fchollet Please review.

@pksX01
Copy link
Contributor Author

pksX01 commented Aug 29, 2024

@sachinprasadhs can you please review and merge these changes as this PR is opened from long time.

@sachinprasadhs
Copy link
Collaborator

@pksX01 , Can you generate .ipynb and .md files instead of making manual changes.

Refer: https://github.com/keras-team/keras-io?tab=readme-ov-file#creating-a-new-example-starting-from-a-python-script

@pksX01
Copy link
Contributor Author

pksX01 commented Sep 25, 2024

@pksX01 , Can you generate .ipynb and .md files instead of making manual changes.

Refer: https://github.com/keras-team/keras-io?tab=readme-ov-file#creating-a-new-example-starting-from-a-python-script

Added generated .md and .ipynb files

@sachinprasadhs can you please review?

@sachinprasadhs
Copy link
Collaborator

Can you exclude the workflow changes in this PR.

@pksX01
Copy link
Contributor Author

pksX01 commented Sep 27, 2024

Can you exclude the workflow changes in this PR.

Done. @sachinprasadhs

Copy link

This PR is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you.

@github-actions github-actions bot added the stale label Oct 12, 2024
@pksX01
Copy link
Contributor Author

pksX01 commented Oct 13, 2024

@sachinprasadhs can you please review it, it is marked stale.

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