python-3.xtensorflowkerastensorflow-hub

module 'tensorflow_hub' has no attribute 'KerasLayer'


When I'm trying to retrain the model with tensorflow it shows an error:

**error module 'tensorflow_hub' has no attribute 'KerasLayer'**

The code is:

print("Building model with", MODULE_HANDLE)
model = tf.keras.Sequential([
    hub.KerasLayer(MODULE_HANDLE, output_shape=[FV_SIZE],
    trainable=do_fine_tuning),
    tf.keras.layers.Dropout(rate=0.2),
    tf.keras.layers.Dense(train_generator.num_classes,
    activation='softmax',
    kernel_regularizer=tf.keras.regularizers.l2(0.0001))
])
model.build((None,)+IMAGE_SIZE+(3,))
model.summary()

The error is like:

      1 print("Building model with", MODULE_HANDLE)
      2 model = tf.keras.Sequential([
----> 3     hub.KerasLayer(MODULE_HANDLE, output_shape=[FV_SIZE],
      4                    trainable=do_fine_tuning),
      5     tf.keras.layers.Dropout(rate=0.2),

AttributeError: module 'tensorflow_hub' has no attribute 'KerasLayer'

by using the tensorflow hub retrain the previous hub model by adding new dence fully connected layers.when run the code it show the above error.is any have idea about that.please help


Solution

  • Please check the tensorflow version. It should be a recent nightly version.

    When I use a version like 1.13.1, I see the following warning before the error, no attribute 'KerasLayer':

    W0423 20:04:16.453974 139707130586880 __init__.py:56] Some hub symbols are not available because TensorFlow version is less than 1.14
    

    After, doing pip install "tf-nightly", everything works fine.

    https://www.tensorflow.org/hub

    For the BatchNormalizationv1 issue, you can use tf2.0 nightly which should also take care of the original issue.

    pip install -U tf-nightly-2.0-preview
    

    https://github.com/tensorflow/tfjs/issues/1255