Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError: Layer weight shape (3, 3, 3, 64) not compatible with provided weight shape (64, 3, 3, 3)

Tags:

python

keras

I am trying to Classify products based on images and text, but running into errors

 img_width, img_height = 224, 224
# build the VGG16 network
model = Sequential()
model.add(ZeroPadding2D((1, 1), input_shape=(img_width, img_height,3), name='image_input'))

model.add(Convolution2D(64, (3, 3), activation='relu', name='conv1_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(64, (3, 3), activation='relu', name='conv1_2'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))


# set trainable to false in all layers
for layer in model.layers:
    if hasattr(layer, 'trainable'):
        layer.trainable = False

return model

WEIGHTS_PATH='E:/'
weight_file = ''.join((WEIGHTS_PATH, '/vgg16_weights.h5'))
f = h5py.File(weight_file,mode='r')
for k in range(f.attrs['nb_layers']):
    if k >= len(model.layers):
        # we don't look at the last (fully-connected) layers in the savefile
        break
    g = f['layer_{}'.format(k)]
    weights = [g['param_{}'.format(p)] for p in range(g.attrs['nb_params'])]
    model.layers[k].set_weights(weights)
f.close()
return model

load_weights_in_base_model(get_base_model())

error: File "C:\Python\lib\site-packages\keras\engine\topology.py", line 1217, in set_weights 'provided weight shape ' + str(w.shape)) ValueError: Layer weight shape (3, 3, 3, 64) not compatible with provided weight shape (64, 3, 3, 3)

can any one please help me to resolve the error. Thanks in Advance..

like image 897
Pradeep Avatar asked Jan 16 '18 14:01

Pradeep


1 Answers

The problem seems to be with the line

model.layers[k].set_weights(weights)

Keras initializes weights differently with different backends. If you are using theano as a backend, then weights will be initialized acc. to kernels_first and if you are using tensorflow as a backend, then weights will be initialized acc. to kernels_last.

So, the problem in you case seems to be that you are using tensorflow but are loading weights from a file which was created using theano as backend. The solution is to reshape your kernels using the keras conv_utils

from keras.utils.conv_utils import convert_kernel
reshaped_weights = convert_kernel(weights)
model.layers[k].set_weights(reshaped_weights)

Check this out for more information

like image 74
layog Avatar answered Oct 20 '22 13:10

layog