简体   繁体   中英

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.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM