Error while trying to load weights for VGG19 model

I was trying to rewrite from scratch but instead building vgg19 instead. I actually wanted to build a different model from scratch to see if it really affects accuracy. I went through the code for vgg19 and vgg16 from the keras repo. I saw that the difference lies in the extra convolution blocks in vgg19.

And also, the pre-trained weights for both of them differ. I’m pasting the part where my code for vgg16 and vgg19 differ here.

 self.ConvBlock(2, 64)
 self.ConvBlock(2, 128)
 self.ConvBlock(4, 256)
 self.ConvBlock(4, 512)
 self.ConvBlock(4, 512)

And, I’m loading the pre-trained weights this way:

        TH_WEIGHTS_PATH = ''
       def create(self,size,include_top):
        if not include_top:
        fname = 'vgg19_weights_th_dim_ordering_th_kernels_notop.h5'
        model.load_weights(get_file(fname, TH_WEIGHTS_PATH_NO_TOP, cache_subdir='models'))
        model.add(Dense(1000, activation='softmax'))

    fname = 'vgg19_weights_th_dim_ordering_th_kernels.h5'
    model.load_weights(get_file(fname, TH_WEIGHTS_PATH, cache_subdir='models'))

I’m getting the following error when i try to instantiate vgg19 in my redux.ipynb file.

ValueError: You are trying to load a weight file containing 18 layers into a model with 21 layers.

The exact line of error pointed out is.

 89         self.FCBlock()
 90         model.add(Dense(1000, activation='softmax'))
 ---> 91 
 92         fname = 'vgg19_weights_th_dim_ordering_th_kernels.h5'
 93         model.load_weights(get_file(fname, TH_WEIGHTS_PATH, cache_subdir='models'))

I double-checked the path for my weights and they seem correct. Please do let me know what I’m doing wrong here.


Right. I figured out what the problem was. The vgg16 that I based my vgg19 code on had a BatchNorm layer that I discounted. I’m still at Lesson 2/3 so I didnt know what BatchNorm exactly does and that it counts as a layer in the network(now after learning about it, it seems very obvious.). So yes, this question can be considered closed.