简体   繁体   English

在 Keras 中从 VGG19 到 ResNet 的转换

[英]Conversion from VGG19 to ResNet in Keras

I have the following code which works on pre-trained VGG model but fails on ResNet and Inception model.我有以下代码适用于预训练的 VGG 模型,但在 ResNet 和 Inception 模型上失败。

vgg_model = keras.applications.vgg16.VGG16(weights='imagenet')
type(vgg_model)
vgg_model.summary()
model = Sequential()
for layer in vgg_model.layers:
    model.add(layer)

Now, changing the model to ResNet as follows:现在,将模型更改为 ResNet,如下所示:

resnet_model=keras.applications.resnet50.ResNet50(weights='imagenet')
type(resnet_model)
resnet_model.summary()
model = Sequential()
for layer in resnet_model.layers:
    model.add(layer)

gives the following error:给出以下错误:

ValueError: Input 0 is incompatible with layer res2a_branch1: expected axis -1 of input shape to have value 64 but got shape (None, 56, 56, 256)

The problem is due to the fact that unlike VGG, Resnet does not have a sequential architecture (eg some layers are connected to more than one layers, there are skip connections, etc.).问题是由于与 VGG 不同,Resnet 没有顺序架构(例如,某些层连接到多个层,存在跳过连接等)。 Therefore you cannot iterate over the layers in the model one after another and connect each layer to the previous one (ie sequentially).因此,您不能一个接一个地迭代模型中的层并将每一层连接到前一层(即按顺序)。 You can plot the architecture of the model using plot_model() to have a better understanding of this point.您可以使用plot_model()绘制模型的架构以更好地理解这一点。

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

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