Comments (12)
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.
Hi @ruddyscent ,
TFHub has dependency on tf_keras
package (i.e Keras2) as per the setup.py of TFHub.
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.
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.
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.kerasDepending on the Keras version, the module might either import
tf_keras
or directly usetf.keras
, the latter causes theisinstance(layer, Layer)
check inSequential.add
to returnFalse
forhub.KerasLayer
, even though it inherits fromkeras.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.
# 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.
# 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.kerasEven 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.
# 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.
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.
Hi @ruddyscent ,
TFHub has dependency on
tf_keras
package (i.e Keras2) as per the setup.py of TFHub.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.
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.
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.
Thank you, @sj-yoon, for your assistance.
The solution provided by @SuryanarayanaY works well for my situation.
from tensorflow.
Related Issues (20)
- Some `Check Failed` errors in `tf.raw_ops.Unbatch` HOT 2
- `Overflow` in `tf.raw_ops.DenseBincount` when the value of `size` is too large. HOT 1
- `Check failed` in `tf.raw_ops.UnbatchGrad` when the input of `original_input` is empty.
- Suggestion: Chatbot Integration for TensorFlow API Assistance HOT 1
- `segmentation fault` in `tf.raw_ops.AddManySparseToTensorsMap` when `sparse_indices` and `sparse_values` are empty and `sparse_shape` is too large.
- Getting [java.lang.IllegalArgumentException: Internal error: Error applying delegate] error while applying GPU Delegate HOT 2
- `Check failed` in `tf.transpose`, `tf.raw_ops.Transpose` and `tf.compat.v1.transpose` when the values of `perm` have negative numbers.
- `Check failed` in `tf.image.crop_and_resize` and `tf.compat.v1.image.crop_and_resize` when `boxes` and `box_indices` are empty. HOT 3
- `Check failed` and `segmentation fault` in `tf.raw_ops.AudioSpectrogram`.
- Error when trring a second training : WARNING:tensorflow:Value in checkpoint could not be found in the restored object: (root).optimizer.iter HOT 1
- `Check failed` in `tf.raw_ops.ScatterNd` and `tf.scatter_nd` when the rank of `indices` > 2.
- `Check failed` in `tf.raw_ops.TensorScatterUpdate` and `tf.tensor_scatter_nd_update` when the rank of `indices` > 2. HOT 1
- `Check failed` in `tf.raw_ops.TensorScatterMax` and `tf.tensor_scatter_nd_max` when the rank of `indices` > 2.
- `Check failed` in `tf.raw_ops.TensorScatterMin` and `tf.tensor_scatter_nd_min` when the rank of `indices` > 2. HOT 2
- `Check failed` in `tf.raw_ops.TensorScatterSub` and `tf.tensor_scatter_nd_sub` when the rank of `indices` > 2.
- `Check failed` in `tf.raw_ops.TensorScatterAdd` and `tf.tensor_scatter_nd_add` when the rank of `indices` > 2. HOT 1
- `Segmentation Fault` in `tf.raw_ops.TensorListScatter` and `tf.raw_ops.TensorListScatterV2` when the value of `indices` is too large.
- Some `Check Failed` errors in `tf.raw_ops.MatrixSetDiagV3` HOT 1
- Some `Check Failed` errors in `tf.raw_ops.MatrixSetDiagV2`
- Memory leak in tf.data when iterating over Dataset.from_generator HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tensorflow.