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

2,856 views
Skip to first unread message

Rohit Gandikota

unread,
Jun 5, 2018, 3:57:34 AM6/5/18
to Keras-users
I'am currently using keras 2.1.5 in anaconda(windows). This is the error when I'am trying to load the weights file available online into VGG16 model. 
And the code goes like: 



def obtain_compiled_vgg_16(vgg_weights_path):
    model = vgg_16(vgg_weights_path)
    sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
    model.compile(optimizer=sgd, loss='categorical_crossentropy')
    return model


def vgg_16(weights_path=None):
    model = Sequential()
    model.add(ZeroPadding2D((1, 1), input_shape=(3, 224, 224)))
    model.add(Convolution2D(64, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(64, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(128, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(128, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(256, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(256, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(256, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(Flatten())
    model.add(Dense(4096, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(4096, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(1000, activation='softmax'))

    if weights_path:
        model.load_weights(weights_path)

    return model

path_vgg = "./vgg16_weights.h5"
 model_vgg = obtain_compiled_vgg_16(path_vgg)



After running this I'am getting this as error: 

  File "C:\Users\rohit\Anaconda2\envs\tensorflow\lib\site-packages\keras\engine\topology.py", line 3354, in load_weights_from_hdf5_group
    str(len(filtered_layers)) + ' layers.')

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



Can someone help me with this, I'am stuck on this for a while now and can't downgrade my keras 

wang...@gmail.com

unread,
Jun 5, 2018, 5:14:18 AM6/5/18
to Keras-users
You may want to double check your weight file since it is saying the weight file contains 0 layers. There should be other ways of downloading VGG19 if you search around.

Rohit Gandikota

unread,
Jun 5, 2018, 6:09:06 AM6/5/18
to Keras-users
Yeah, I've checked the file again. I tried downloading other files too. It shows the same error

aakash...@gmail.com

unread,
Feb 20, 2019, 7:20:06 PM2/20/19
to Keras-users
Did you solve this error?

Ted Yu

unread,
Feb 21, 2019, 12:51:48 PM2/21/19
to Keras-users
The weight file didn't have layer.

Can you let us know where you downloaded it ?

Cheers
--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/057c45e2-3ae5-40bd-bd55-f1de407b40f3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Lance Norskog

unread,
Feb 21, 2019, 1:39:08 PM2/21/19
to aakash...@gmail.com, Keras-users
I think that under Windows it needs to be "vgg16_weights.h5" or ".\\vgg16_weights.h5".
"./vgg16_weights.h5" is Unix-style.

--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/057c45e2-3ae5-40bd-bd55-f1de407b40f3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Lance Norskog
lance....@gmail.com
Redwood City, CA
Reply all
Reply to author
Forward
0 new messages