The below code in PyCharm is about transfer learning to add a feature extractor layer into keras sequential model. :
feature_extract_layer=hub.KerasLayer(model_url,trainable=False,input_shape=(224,224,3),name="feature_extraction") model=tf.keras.Sequential([ feature_extract_layer, tf.keras.layers.Dense(train_data.num_classes,activation='softmax',name="outputLayer")
]) is giving the below error:
Only instances of keras.Layer
can be added to a Sequential model. Received:
<tensorflow_hub.keras_layer.KerasLayer object at 0x000001C830B446B0> (of type <class 'tensorflow_hub.keras_layer.KerasLayer'>) code: import tensorflow as tf import tensorflow_hub as hub model_url = "https://www.kaggle.com/models/google/mobilenet-v2/frameworks/tensorFlow2/variations/100-224-feature-vector/versions/1?tfhub-redirect=true"
I tried the code in google colab and it worked normal, but I need it in PyCharm.
the solution is to make sure that keras is version 2.something : Since TF2.16 comes with Keras3 the problem arises. As a workaround, we can install tf_keras package and set environment variable TF_USE_LEGACY_KERAS=1 to ensure Keras2 will be used with tf.keras.