我使用预先训练的VGG16
建立了一个使用keras的模型 model = Sequential()
model.add(ZeroPadding2D((1, 1), input_shape=(3, img_width, img_height)))
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)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_2'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_2'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_3'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_2'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_3'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_2'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_3'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
# load the weights of the VGG16 networks
# (trained on ImageNet, won the ILSVRC competition in 2014)
# note: when there is a complete match between your model definition
# and your weight savefile, you can simply call model.load_weights(filename)
assert os.path.exists(vgg_model), 'Model weights not found (see "weights_path" variable in script).'
f = h5py.File(vgg_model)
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()
print('Model loaded.')
# build a classifier model to put on top of the convolutional model
top_model = Sequential()
top_model.add(Flatten(input_shape=model.output_shape[1:]))
top_model.add(Dense(256, activation='relu'))
top_model.add(Dropout(0.5))
top_model.add(Dense(2, activation='softmax'))
但是在使用fit函数运行模型时,它抛出异常
expected sequential_2 to have shape (None, 2) but got array with shape (32, 1)
这里有什么问题(注意:我使用目录功能来拟合我的模型)。
答案 0 :(得分:0)
问题在于你的训练标签。 很难给出确切的答案,因为您没有在这里向我们展示您拥有的标签类型和您所做的汇编。
我可以继续猜测您正在使用binary_crossentropy或categorical_crossentropy进行编译。
如果我正确猜测,请拨打标签'Y',使用以下代码为培训做好准备:
from keras.utils import np_utils
Y = np_utils.to_categorical(Y)
提示:当您进行二进制分类(两个类)时,您可以使最后一个密集层输出1而不是2.在标签中,为一个类选择0,为另一个选择1。这样您就可以避免现在遇到的问题。
答案 1 :(得分:0)
你的问题在于flow_from_directory
。您应该更改class_mode = "categorical"
。此外 - 您的二进制分类设置并不常见。您应该将最后一层更改为:
top_model.add(Dense(1, activation='sigmoid'))
然后将loss="binary_crossentropy"
和class_mode="binary"
留在您的生成器中或(在第二种情况下)离开:
top_model.add(Dense(2, activation='softmax'))
并在您的生成器中设置loss="categorical_crossentropy"
和class_mode="categorical"
。