繁体   English   中英

如何在 keras ResNet 之后应用密集层?

[英]How Can I apply Dense layer after keras ResNet?

在 ResNet50 之后如何应用 Dense 层? 这是我的代码

    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

但我收到了这个错误:

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. 在这种情况下,使用 Sequential API 可能会更容易:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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