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

587 views
Skip to first unread message

khawla....@gmail.com

unread,
Nov 20, 2019, 1:46:16 PM11/20/19
to Keras-users
When I tried to load VGG16 weights with this function :


load_model_weights(model, weights_path):

   f = h5py.File(weights_path)

   nb_layers = len(f.attrs["layer_names"])

   for k in range(nb_layers):

   if k >= len(model.layers):

   # without 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)

   model.layers[k].trainable = False

f.close()


I am having this error :

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



Some users suggested to use K.set_image_dim_ordering('tf') but I am having this error:

Traceback (most recent call last):

File "<input>", line 1, in <module>

AttributeError: module 'keras.backend' has no attribute 'set_image_dim_ordering'


Other users suggested :

from keras.utils.conv_utils import convert_kernel

reshaped_weights = convert_kernel(weights)

model.layers[k].set_weights(reshaped_weights)


But I got this error :

Traceback (most recent call last):

File "<input>", line 1, in <module>

File "<input>", line 11, in load_model_weights

File ".../lib/python3.6/site-packages/keras/utils/conv_utils.py", line 78, in convert_kernel

raise ValueError('Invalid kernel shape:', kernel.shape)

ValueError: ('Invalid kernel shape:', (0,))



keras version: '2.3.1' Tensorflow version: 1.15.0


Any one can help me?


Reply all
Reply to author
Forward
0 new messages