Difficulty importing data from Tensorflow Hub

Hello.

I’m having difficulty importing data from Tensorflow Hub.

The data comes from a tf.dataset that contains a pd.Dataframe and images aggregated in the tf.dataset. When I use it with cropnet it says:

Only instances of keras.Layer can be added to a Sequential model. Received: <tensorflow_hub.keras_layer.KerasLayer object at 0x7fadc741d550> (of type <class ‘tensorflow_hub.keras_layer.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

And the data input is the following layer:

img_input = keras.Input(shape=(*CFG.image_size, 3), name=“images”)

I already tried to add cardinality, but it didn’t work. Neither does the shape of the tensors.

Do you know how to solve this?

I rely heavily on this cropnet to develop a neural network model ensemble.

I’m using Keras 3.0.4, Tensorflow 2.15.0 and Keras-CV 0.8.2

Thanks in advance.

1 Like

Hi @Andre_Galhardo, Thank you for reporting this bug. while reproducing the issue we have also observed the same. As an alternative i recommend you to use tf.keras which does not provide any error.

classifier = hub.KerasLayer('https://tfhub.dev/google/cropnet/classifier/cassava_disease_V1/2')
model=tf.keras.Sequential([
    classifier,
    tf.keras.layers.Dense(10,activation='softmax')  
])

Thank You.

Try to use Keras 2. version :+1:t2:

import tensorflow as tf
from tensorflow import keras
print(tf.version)
2.16.1

import keras
print(keras.version)
3.2.0

import tf_keras
print(tf_keras.version)
2.16.0

Help needed! I have checked the version (as mentioned above) and I am using keras 2. version.

When I run the above below, I am getting the same error too (see below the code).

encoder_layer = hub.KerasLayer(“Google | universal-sentence-encoder | Kaggle”,
input_shape=,
dtype = tf.string,
trainable=False,
name=“pretrained”)

model = tf.keras.Sequential([encoder_layer, tf.keras.layers.Dense(1,activation=“sigmoid”)],
name=“model_pretrained”)

ValueError: Only instances of keras.Layer can be added to a Sequential model. Received: <tensorflow_hub.keras_layer.KerasLayer object at 0x14f2a7650> (of type <class ‘tensorflow_hub.keras_layer.KerasLayer’>)

Hi @rtax, I have tried to load the universal sentence encoder from tf_hub using tensorflow 2.15.1 and keras 2.15.0 in colab and did not face any error. could you please try with the above mentioned versions and let us know the execution status. Please refer to this gist for working code example. Thank You.

it gets no error on colab but everytime stuck on kaggle