Karan Arya
Karan Arya

Reputation: 104

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

Upvotes: 0

Views: 374

Answers (1)

today
today

Reputation: 33410

The problem is due to the fact that unlike VGG, Resnet does not have a sequential architecture (e.g. some layers are connected to more than one layers, there are skip connections, etc.). Therefore you cannot iterate over the layers in the model one after another and connect each layer to the previous one (i.e. sequentially). You can plot the architecture of the model using plot_model() to have a better understanding of this point.

Upvotes: 2

Related Questions