Giter VIP home page Giter VIP logo

Comments (12)

sj-yoon avatar sj-yoon commented on April 27, 2024 4

win10, no gpu, no colab

tensorflow_hub를 사용하시려면 다음과 같이 해보세요
import tensorflow as tf
import tensorflow_hub as hub
import tf_keras as keras """tensorflow_hub 설치시 자동으로 설치됩니다."

...
...
model = keras.Sequential([ """"tf.keras --> keras"""
hub,KerasLayer(url, input_shape=(size, size, 3)
...
...
imgfile = keras.utils.get_file('image.jpg', 'https://..........') """"tf.keras --> keras"""
작동이 잘됩니다.

from tensorflow.

SuryanarayanaY avatar SuryanarayanaY commented on April 27, 2024 3

Hi @ruddyscent ,

TFHub has dependency on tf_keras package (i.e Keras2) as per the setup.py of TFHub.

https://github.com/tensorflow/hub/blob/ff72e25fef44bd67d5c14fb5328aa44e303e3404/tensorflow_hub/pip_package/setup.py#L31

Since TF2.16 comes with Keras3 the problem arises. As a workaround, you can install tf_keras package and set environment variable TF_USE_LEGACY_KERAS=1 to ensure Keras2 will be used with tf.keras.

from tensorflow.

Aloqeely avatar Aloqeely commented on April 27, 2024

After investigating the code, I found a potential cause of the issue in tensorflow_hub/keras_layer.py lines 26-31:

# Use Keras 2.
version_fn = getattr(tf.keras, "version", None)
if version_fn and version_fn().startswith("3."):
  import tf_keras as keras
else:
  keras = tf.keras

Depending on the Keras version, the module might either import tf_keras or directly use tf.keras, the former causes the isinstance(layer, Layer) check in Sequential.add to return False for hub.KerasLayer, even though it inherits from keras.layers.Layer

from tensorflow.

SuryanarayanaY avatar SuryanarayanaY commented on April 27, 2024

After investigating the code, I found a potential cause of the issue in tensorflow_hub/keras_layer.py lines 26-31:

# Use Keras 2.
version_fn = getattr(tf.keras, "version", None)
if version_fn and version_fn().startswith("3."):
  import tf_keras as keras
else:
  keras = tf.keras

Depending on the Keras version, the module might either import tf_keras or directly use tf.keras, the latter causes the isinstance(layer, Layer) check in Sequential.add to return False for hub.KerasLayer, even though it inherits from keras.layers.Layer

Hi, When I tried with TF2.16v on Colab environment the error stack seems generated from the _ensure_keras_2_importable() function from tensorflow_hub/__init__.py as per attached gist.

from tensorflow.

SuryanarayanaY avatar SuryanarayanaY commented on April 27, 2024
# Use Keras 2.
version_fn = getattr(tf.keras, "version", None)
if version_fn and version_fn().startswith("3."):
  import tf_keras as keras
else:
  keras = tf.keras

Even this code seems confusing to me. If version_fn results in Keras3 then it tries to import tf_keras to use Keras2 else it assumes Keras2 is alreday there(i.e TF<=2.15v) hence it takes keras=tf.keras.

But version_fn = getattr(tf.keras, "version", None) returns None making this to use tf.keras (i.e Keras3) which ic not compatible with TF_Hub.

from tensorflow.

Aloqeely avatar Aloqeely commented on April 27, 2024
# Use Keras 2.
version_fn = getattr(tf.keras, "version", None)
if version_fn and version_fn().startswith("3."):
  import tf_keras as keras
else:
  keras = tf.keras

Even this code seems confusing to me. If version_fn results in Keras3 then it tries to import tf_keras to use Keras2 else it assumes Keras2 is alreday there(i.e TF<=2.15v) hence it takes keras=tf.keras.

But version_fn = getattr(tf.keras, "version", None) returns None making this to use tf.keras (i.e Keras3) which ic not compatible with TF_Hub.

Actually, version_fn is not None, that code works as intended, which is to use Keras 2

tf.keras.Sequential is on Keras 3. and hub.KerasLayer is on Keras 2, so I believe due to the version difference, it causes isinstance(layer, Layer) to fail and return False

from tensorflow.

SuryanarayanaY avatar SuryanarayanaY commented on April 27, 2024
# Use Keras 2.
version_fn = getattr(tf.keras, "version", None)
if version_fn and version_fn().startswith("3."):
  import tf_keras as keras

From the code above if version_fn is not None and if it starts with "3" (which means Keras 3 found) in that case it is importing tf_keras package as keras. Please note that tf_keras package is for Keras2. This indicates TFHub supports only Keras2 package.

The else part it assumes that version_fn doesn't starts with "3" which case it assumes Keras2 installed with TF package and it marks keras=tf.keras. But for any case if version_fn becomes None and if Keras3 installed with TF then tf.keras will become Keras3 which might a problem.

@Aloqeely , Could you please import TF2.16 and confirm what will be the version_fn output ?

from tensorflow.

Aloqeely avatar Aloqeely commented on April 27, 2024

Here you go

import tensorflow as tf

version_fn = getattr(tf.keras, "version", None)
print("TF Version: " + tf.__version__)
print("TF Keras Version: " + version_fn())

Output:

TF Version: 2.16.1
TF Keras Version: 3.0.5

from tensorflow.

Aloqeely avatar Aloqeely commented on April 27, 2024

Hi @ruddyscent ,

TFHub has dependency on tf_keras package (i.e Keras2) as per the setup.py of TFHub.

https://github.com/tensorflow/hub/blob/ff72e25fef44bd67d5c14fb5328aa44e303e3404/tensorflow_hub/pip_package/setup.py#L31

Since TF2.16 comes with Keras3 the problem arises. As a workaround, you can install tf_keras package and set environment variable TF_USE_LEGACY_KERAS=1 to ensure Keras2 will be used with tf.keras.

If this workaround fixes ruddyscent's problem, then I think we should mark this issue as resolved, because the problem is tensorflow hub not supporting Keras 3, so it is not relevant to this repository.

from tensorflow.

SuryanarayanaY avatar SuryanarayanaY commented on April 27, 2024

3.0.5

This means when tf.keras version is 3.x then Hub suggesting to import tf_keras as keras which is a Keras2 package. For that we should install with tf_keras package using pip install tf_keras. But from the error log the package is having keras/src which seems to be from Keras3 package for me.

from tensorflow.

Aloqeely avatar Aloqeely commented on April 27, 2024

This means when tf.keras version is 3.x then Hub suggesting to import tf_keras as keras which is a Keras2 package. For that we should install with tf_keras package using pip install tf_keras. But from the error log the package is having keras/src which seems to be from Keras3 package for me.

Yep, hub does that.
The error log is caused by tf.keras.Sequential (Keras 3, hence the error is produced from keras/src) after it received a hub.KerasLayer (Keras 2)

from tensorflow.

ruddyscent avatar ruddyscent commented on April 27, 2024

Thank you, @sj-yoon, for your assistance.

The solution provided by @SuryanarayanaY works well for my situation.

from tensorflow.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.