I'm trying to customize the model taken from tf hub but can't access the layers with following error 'KerasLayer' object has no attribute 'layers'
Here is my code as an example:
import tensorflow_hub as hub
from tensorflow.keras import layers
feature_extractor_url = "https://tfhub.dev/tensorflow/efficientnet/lite0/feature-vector/1"
base_model = hub.KerasLayer(feature_extractor_url,
input_shape=(224,224,3))
base_model.trainable = True
import tensorflow
from tensorflow.keras.models import Model
x = base_model.layers[-10].output
x = tensorflow.keras.layers.Conv2D(4, (3, 3), padding="same", activation="relu")(x)
x = tensorflow.keras.layers.GlobalMaxPooling2D()(x)
x = tensorflow.keras.layers.Flatten()(x)
outputs = tensorflow.keras.layers.Activation('sigmoid', name="example_output")(x)
model = Model(base_model.input, outputs=outputs)
model.summary()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-43-0501ec56d6c4> in <module>()
14 from tensorflow.keras.models import Model
15
---> 16 x = base_model.layers[-10].output
17 x = tensorflow.keras.layers.Conv2D(4, (3, 3), padding="same", activation="relu")(x)
18 x = tensorflow.keras.layers.GlobalMaxPooling2D()(x)
AttributeError: 'KerasLayer' object has no attribute 'layers'
What I've tried: I built the model using sequential api :
model = tf.keras.Sequential([
base_model,
layers.Dense(image_data.num_classes)
])
model.summary()
But still a I can't access the layers inside base_model.
How can I access the layers from KerasLayer?
Thank you!