tensorflow / hub

A library for transfer learning by reusing parts of TensorFlow models.
https://tensorflow.org/hub
Apache License 2.0
3.48k stars 1.66k forks source link

Keras Hub Layer Does Not Work with Functional API. Throws Exception. #483

Closed nectario closed 4 years ago

nectario commented 4 years ago

TensorFlow Version: 2.1 Python Version: 3.76 OS: Windows 10 Issue: Keras Hub Layer Does Not Work with Functional API. Throws Exception. Unable to load IMDB dataset. See below code and exception:

import tensorflow as tf
from tensorflow.keras.layers import Dense, Concatenate, RepeatVector, Activation, Dot, Bidirectional, Flatten, Embedding, Input, SpatialDropout1D, LSTM, Dropout, Lambda, Conv2D, Conv1D, Attention, AdditiveAttention, GlobalAveragePooling1D, TimeDistributed, AveragePooling1D
from tensorflow.keras.models import Model
from tensorflow.keras.backend import backend
import tensorflow_hub as hub
from tensorflow.keras.datasets import imdb
from tensorflow.keras.preprocessing.sequence import pad_sequences
import numpy as np
import tensorflow_datasets as tfds

def cnn_classifier():
    # Encode each timestep
    # embedding = Embedding(10000, 300, trainable=True)(input)

    embedding = "https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim/1"
    input = Input(shape=(None,), name="Input")
    hub_layer = hub.KerasLayer(embedding, trainable=True)(input)
    cnn = Conv1D(64,3, padding="same", activation="relu")(hub_layer)
    cnn = Conv1D(32, 3, padding="same", activation="relu")(cnn)
    cnn = Conv1D(16, 3, padding="same", activation="relu")(cnn)
    cnn = Flatten()(cnn)
    output = Dense(1, activation="sigmoid")(cnn)

    model = Model(input, output)
    model.compile(loss="binary_crossentropy",
                  optimizer="adam",
                  metrics=["accuracy"])
    model.summary()
    return model

model = cnn_classifier()

train_validation_split = tfds.Split.TRAIN.subsplit([8, 2])

(train_data, validation_data), test_data = tfds.load(
    name="imdb_reviews",
    split=(train_validation_split, tfds.Split.TEST),
    as_supervised=True)

#train_data = pad_sequences(train_data, maxlen=1100, dtype='int32', padding='post', truncating='post', value=0.0)
#test_data = pad_sequences(test_data, maxlen=1100, dtype='int32', padding='post', truncating='post', value=0.0)

model.fit(train_data.shuffle(25000).batch(512),
                    epochs=20,
                    validation_data=validation_data.batch(512),
                    verbose=10)

Exception:

2020-01-17 15:46:42.273900: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
WARNING:tensorflow:AutoGraph could not transform <tensorflow.python.saved_model.function_deserialization.RestoredFunction object at 0x0000029880A70D88> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
Cause: Python inputs incompatible with input_signature:
  inputs: (
    Tensor("Input:0", shape=(None, None), dtype=float32))
  input_signature: (
    TensorSpec(shape=(None,), dtype=tf.string, name=None))
WARNING:tensorflow:AutoGraph could not transform <tensorflow.python.saved_model.function_deserialization.RestoredFunction object at 0x0000029880A70D88> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
Cause: Python inputs incompatible with input_signature:
  inputs: (
    Tensor("Input:0", shape=(None, None), dtype=float32))
  input_signature: (
    TensorSpec(shape=(None,), dtype=tf.string, name=None))
Traceback (most recent call last):
  File "C:/Development/Projects/TensorFlow_2.0_Hierarchical_Attention/Classifiers.py", line 47, in <module>
    model = cnn_classifier()
  File "C:/Development/Projects/TensorFlow_2.0_Hierarchical_Attention/Classifiers.py", line 17, in cnn_classifier
    hub_layer = hub.KerasLayer(embedding, trainable=True)(input)
  File "C:\Development\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 773, in __call__
    outputs = call_fn(cast_inputs, *args, **kwargs)
  File "C:\Development\Python\Python37\lib\site-packages\tensorflow_core\python\autograph\impl\api.py", line 237, in wrapper
    raise e.ag_error_metadata.to_exception(e)
ValueError: in converted code:

    C:\Development\Python\Python37\lib\site-packages\tensorflow_hub\keras_layer.py:209 call  *
        result = f()
    C:\Development\Python\Python37\lib\site-packages\tensorflow_core\python\saved_model\load.py:438 _call_attribute
        return instance.__call__(*args, **kwargs)
    C:\Development\Python\Python37\lib\site-packages\tensorflow_core\python\eager\def_function.py:568 __call__
        result = self._call(*args, **kwds)
    C:\Development\Python\Python37\lib\site-packages\tensorflow_core\python\eager\def_function.py:606 _call
        results = self._stateful_fn(*args, **kwds)
    C:\Development\Python\Python37\lib\site-packages\tensorflow_core\python\eager\function.py:2362 __call__
        graph_function, args, kwargs = self._maybe_define_function(args, kwargs)
    C:\Development\Python\Python37\lib\site-packages\tensorflow_core\python\eager\function.py:2661 _maybe_define_function
        *args, **kwargs)
    C:\Development\Python\Python37\lib\site-packages\tensorflow_core\python\eager\function.py:2185 canonicalize_function_inputs
        self._flat_input_signature)
    C:\Development\Python\Python37\lib\site-packages\tensorflow_core\python\eager\function.py:2252 _convert_inputs_to_signature
        format_error_message(inputs, input_signature))

    ValueError: Python inputs incompatible with input_signature:
      inputs: (
        Tensor("Input:0", shape=(None, None), dtype=float32))
      input_signature: (
        TensorSpec(shape=(None,), dtype=tf.string, name=None))

Process finished with exit code 1
rmanak commented 4 years ago

same issue here

maximneumann commented 4 years ago

Hi Nektarios,

as the error message indicates, the expected inputs are strings, and not floats.

First, you'd need to change your input to something like this: input = Input(shape=(), name="Input", dtype=tf.string)

To start, you can follow the example in the colab of this hub module: https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim/1 https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/tf2_text_classification.ipynb

and adapt it to Functional API as needed.

Example: hub_handle = "https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim/1" hub_layer = hub.KerasLayer(hub_handle, output_shape=[20], input_shape=[], dtype=tf.string, trainable=True) input = Input(shape=(), name="Input", dtype=tf.string) net = hub_layer(input) net = tf.keras.layers.Dense(16, activation='relu')(net) output = tf.keras.layers.Dense(1, activation='sigmoid')(net) m = Model(input, output) m.summary()

Once you get a very basic example working, you can experiment further with your model. In case other issues come up, I'd recommend to ask at StackOverflow for general tensorflow related questions.

StoyanVenDimitrov commented 4 years ago

Hi,

I tried out what you say, but I got: WARNING:tensorflow:AutoGraph could not transform <tensorflow.python.saved_model.function_deserialization.RestoredFunction object at 0x1313d4f60> and will run it as-is. Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux,export AUTOGRAPH_VERBOSITY=10) and attach the full output. Cause: Python inputs incompatible with input_signature: inputs: ( Tensor("Input_1:0", shape=(None, None), dtype=string)) input_signature: ( TensorSpec(shape=(None,), dtype=tf.string, name=None)) Any Idea whats going on? The code is here: https://github.com/StoyanVenDimitrov/deeplearning_NLU/blob/save_mode/application.py#L154

murilo-cunha commented 4 years ago

Try looking at the input shapes. The error states that it expected shape=(None,) but you passed shape=(None, None). Moreover, you could look into the types as well. Not sure if string instead of tf.string will make a difference.

arshad171 commented 4 years ago

as pointed out by @maximneumann , the input shape has to be shape=().

This resolved the issue for me.