简体   繁体   English

"ValueError:层权重形状(3、3、3、64)与提供的权重形状(64、3、3、3)不兼容"

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

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)错误:文件“C:\\Python\\lib\\site-packages\\keras\\engine\\topology.py”,第 1217 行,在 set_weights 'provided weight shape' + str(w.shape)) ValueError: Layer weight shape (3, 3, 3, 64) 与提供的重量形状 (64, 3, 3, 3) 不兼容

can any one please help me to resolve the error.任何人都可以帮我解决错误。 Thanks in Advance..提前致谢..

The problem seems to be with the line 问题似乎出在线路上

model.layers[k].set_weights(weights)

Keras initializes weights differently with different backends. Keras使用不同的后端以不同的方式初始化权重。 If you are using theano as a backend, then weights will be initialized acc. 如果您使用theano作为后端,则权重将根据acc初始化。 to kernels_first and if you are using tensorflow as a backend, then weights will be initialized acc. kernels_first ,如果您使用tensorflow作为后端,则权重将根据acc初始化。 to kernels_last . 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. 因此,您遇到的问题似乎是您正在使用tensorflow但正在从使用theano作为后端创建的文件中加载权重。 The solution is to reshape your kernels using the keras conv_utils 解决方案是使用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 查看以获取更多信息

我通过将图像转换为 RGB 解决了类似的错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:层权重形状 (43, 100) 与提供的权重形状 (412457, 400) 不兼容 - ValueError: Layer weight shape (43, 100) not compatible with provided weight shape (412457, 400) 层权重形状 (1, 1) 与为 keras model 提供的权重形状 (1,) 不兼容 - Layer weight shape (1, 1) not compatible with provided weight shape (1,) for keras model Tensorflow Keras 嵌入层错误:层权重形状不兼容 - Tensorflow Keras Embedding Layer Error: Layer weight shape not compatible keras 图层权重的形状与保存的权重形状不匹配 - keras shape of layer weights does not match saved weight shape ValueError:输入 0 与图层不兼容:预期形状 =(None, 48, 187, 621, 64),找到的形状 =(48, 187, 621, 64) - ValueError: Input 0 is incompatible with layer: expected shape=(None, 48, 187, 621, 64), found shape=(48, 187, 621, 64) 重量变量Tensorflow的形状 - Shape of weight variable Tensorflow ValueError:层顺序的输入 0 与层不兼容:预期 ndim=4,发现 ndim=3。 收到的完整形状:[32, 64, 3] - ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [32, 64, 3] ValueError:无法将输入数组从形状(64,64,4)广播到形状(64,64) - ValueError: could not broadcast input array from shape (64,64,4) into shape (64,64) TensorFlow ValueError:无法为形状为“(?, 64, 64, 3)”的张量 u'Placeholder:0' 提供形状 (64, 64, 3) 的值 - TensorFlow ValueError: Cannot feed value of shape (64, 64, 3) for Tensor u'Placeholder:0', which has shape '(?, 64, 64, 3)' ValueError:无法将大小为 128 的数组重塑为形状 (64,64) - ValueError: cannot reshape array of size 128 into shape (64,64)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM