[英]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)
我通过将图像转换为 RGB 解决了类似的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.