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

Multiple issue with mobilevit vision example while conversion to Keras 3 with TF backend #18613

Closed
pksX01 opened this issue Oct 15, 2023 · 8 comments

Comments

@pksX01
Copy link
Contributor

pksX01 commented Oct 15, 2023

Working on converting the A mobile-friendly Transformer-based model for image classification keras.io example to keras 3 with TF backend and found multiple issues. Logging here as per #18468.

Issue 1: tensorflow_addons package is imported but not being used. This line itself is causing error when I am running in local on macbook air m1. But not causing error in running on google colab.
image

Issue 2: Issue in tf.nn.swish()

ValueError: A KerasTensor cannot be used as input to a TensorFlow function. A KerasTensor is a symbolic placeholder for a shape and dtype, used when constructing Keras Functional models or Keras Functions. You can only use it as input to a Keras layer or a Keras operation (from the namespaces `keras_core.layers` and `keras_core.operations`). You are likely doing something like:


x = Input(...)
...
tf_fn(x)  # Invalid.


What you should do instead is wrap `tf_fn` in a layer:

class MyLayer(Layer):
    def call(self, x):
        return tf_fn(x)

x = MyLayer()(x)

Issue 2 can be reproduced here.
Solution I tried: I replaced tf.nn.swish() with keras.activations.swish() and moved further to run notebook. This solution can be seen in notebook link of last issue.

Issue 3: AttributeError: module 'keras.applications.imagenet_utils' has no attribute 'correct_pad'
This can be reproduced here.
Solution I tried: Removed padding=imagenet_utils.correct_pad(m, 3) from layers.ZeroPadding2D(). This solution can be seen in notebook link of last issue.

Issue 4: Issue in checkpoint- ValueError: The filename must end in `.weights.h5`. Received: filepath=/tmp/checkpoint
This can be reproduced here
Solution I tried: Replaced checkpoint_filepath = "/tmp/checkpoint" with checkpoint_filepath = "/tmp/checkpoint.weights.h5". This solution can be seen in notebook link of last issue.

Issue 5: Issue while saving keras model for TFLite conversion. I got below error:
ValueError: Invalid filepath extension for saving. Please add either a `.keras` extension for the native Keras format (recommended) or a `.h5` extension. Use `tf.saved_model.save()` if you want to export a SavedModel for use with TFLite/TFServing/etc. Received: filepath=mobilevit_xxs.

When I tried to save using .keras extension but got below error:
OSError: SavedModel file does not exist at: mobilevit_xxs.keras/{saved_model.pbtxt|saved_model.pb}

When I tried to save using .h5 extension, I got below error:
ValueError: Unable to create dataset (name already exists)

Issue 5 can be reproduced here.

@sachinprasadhs sachinprasadhs added backend:tensorflow keras-team-review-pending Pending review by a Keras team member. labels Oct 25, 2023
@sachinprasadhs sachinprasadhs self-assigned this Oct 25, 2023
@sachinprasadhs sachinprasadhs removed the keras-team-review-pending Pending review by a Keras team member. label Oct 26, 2023
@innat
Copy link

innat commented Nov 3, 2023

Currently,

model.save('my_model.keras') # (recommended)
model.save('my_model.h5') # lagecy and will be deprecated

both are supported but it's warned that h5 will be deprecated. And for weight/state,

model.save_weight('my_model.weights.h5`)

but in tf.keras, it was

model.save_weight('my_model.h5`)

Confusing a bit, hopefully will sort out soon. I tried to load a .h5 file which was built with tf.keras to new keras-v3 but the filename should be end with .weights.h5, not sure why .weights term made compulsory instead of optional. Even though remaning the weight file from .h5 to .weights.h5 doesn't load the tf.keras weight keras-v3 model properly. Transforming is not gooing smooth! -_-

#18467 (comment)

@pksX01
Copy link
Contributor Author

pksX01 commented Nov 20, 2023

I am able to resolve Issue 5: Issue while saving keras model fort TFLite conversion as well. I am able to save and open model now. But I don't know why it is being crashed while converting to TFLite. I tried in Google Colab (Free version) and Kaggle notebooks. Is this because of memory?
@divyashreepathihalli @sampathweb @sachinprasadhs

@sachinprasadhs
Copy link
Collaborator

@pksX01 , Could you track the RAM usage in colab, crash might be due to OOM issue.

@pksX01
Copy link
Contributor Author

pksX01 commented Nov 20, 2023

I guess so, CPU consumption is getting reached to peak and then colab crashes.

@sachinprasadhs
Copy link
Collaborator

Well in that case, it is related to the memory availability in your system, since colab offers only limited RAM capacity for free version.

@pksX01
Copy link
Contributor Author

pksX01 commented Nov 23, 2023

But it's creating in Kaggle as well which provides almost 30 GB RAM for CPU.

@pksX01
Copy link
Contributor Author

pksX01 commented Nov 23, 2023

Now, it didn't crash. I will open PR with the solutions/ modifications for the 5 issues mentioned in 1st comment in upcoming days.

@pksX01
Copy link
Contributor Author

pksX01 commented Nov 29, 2023

Since PR is merged now, I think we can close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants