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