Zahra Hosseini
Zahra Hosseini

Reputation: 596

How Can I apply Dense layer after keras ResNet?

How can apply a Dense layer after ResNet50? It is my code

    def build_model():
       x = tf.keras.applications.ResNet50(input_shape=(IMG_WIDTH, IMG_HEIGHT, 3), weights=None, 
       include_top=False, pooling='avg')
       model = tf.keras.layers.Dense(196)(x)
       model.summary()
       return model

but I got this error:

TypeError: Inputs to a layer should be tensors.

Upvotes: 0

Views: 723

Answers (1)

Lescurel
Lescurel

Reputation: 11651

You can access the output of the model with the property output of the model, if you are willing to recreate a model using the functional API. In that case, it could be easier to use the Sequential API though:

Sequential API:

new_model = tf.keras.Sequential(
    [
        tf.keras.applications.ResNet50(input_shape=(IMG_WIDTH, IMG_HEIGHT, 3), weights=None, include_top=False, pooling='avg'),
        tf.keras.layers.Dense(196)
    ]
)
new_model.summary()

Functional API :

resnet = tf.keras.applications.ResNet50(input_shape=(IMG_WIDTH, IMG_HEIGHT, 3), weights=None, include_top=False, pooling='avg')
out = tf.keras.layers.Dense(196)(resnet.output)
new_model = tf.keras.models.Model(inputs=resnet.input, outputs=out)
new_model.summary()

Upvotes: 2

Related Questions