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

Bug: TF Hub is not compatible with Keras 3 and TensorFlow 2.16+ #903

Open
rkazants opened this issue Mar 20, 2024 · 5 comments
Open

Bug: TF Hub is not compatible with Keras 3 and TensorFlow 2.16+ #903

rkazants opened this issue Mar 20, 2024 · 5 comments
Labels

Comments

@rkazants
Copy link

What happened?

When I try to create Keras model using TF 2.16+ I've got an error:
ValueError: Exception encountered when calling layer 'keras_layer' (type KerasLayer).
A KerasTensor is symbolic: it's a placeholder for a shape an a dtype. It doesn't have any actual numerical value. You cannot convert it to a NumPy array.

Relevant code

import tensorflow as tf
import tensorflow_hub as hub

image = tf.keras.layers.Input(shape=(224, 224, 3), dtype=tf.float32, name="image")
feature_vector = hub.KerasLayer("https://www.kaggle.com/models/google/mobilenet-v2/frameworks/tensorFlow2/variations/100-224-feature-vector/versions/2", trainable=False)(image)
softmax = tf.keras.layers.Dense(20, activation='softmax')(feature_vector)
classification_model = tf.keras.Model(inputs={'image': image}, outputs={'softmax': softmax})

Relevant log output

ValueError: Exception encountered when calling layer 'keras_layer' (type KerasLayer).
           A KerasTensor is symbolic: it's a placeholder for a shape an a dtype. It doesn't have any actual numerical value. You cannot convert it to a NumPy array.

tensorflow_hub Version

0.12.0 (latest stable release)

TensorFlow Version

other (please specify)

Other libraries

TF 2.16

Python Version

3.x

OS

Linux

@KeijiBranshi
Copy link
Collaborator

Hi @rkazants. Thanks for posting.

Can you try upgrading to the latest tensorflow_hub version 0.16.1 and installing tf-keras as a peer dependency?

Some extra context:

TensorFlow v2.16 points tf.keras to Keras 3, which unfortunately breaks a number of workflows with tensorflow_hub. We're working to make tensorflow_hub compatible with Keras 3 but in the meantime the recommendation is to use Keras 2 via tf-keras.

@YamatoDX
Copy link

YamatoDX commented Aug 1, 2024

@KeijiBranshi Thanks. It worked

@MohammadH8086
Copy link

MohammadH8086 commented Sep 9, 2024

  • Install tf_keras via pip install tf-keras
  • Run the below code at the first of your code ( before importing tensorflow ):
 import os
 os.environ['TF_USE_LEGACY_KERAS']='1'

Now your code should work properly with using keras 2

@Aarushi16V
Copy link

Done with the above installation still getting the error can anyone help with it

@rabyunghwa
Copy link

@Aarushi16V The following is what worked for me.

!pip3 install tf_keras==2.16
!pip3 install --upgrade tensorflow-hub

import os
os.environ['TF_USE_LEGACY_KERAS']='1'

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

No branches or pull requests

6 participants