使用Keras微调VGG时输入形状的问题

时间:2017-04-02 09:34:48

标签: tensorflow deep-learning conv-neural-network keras-layer vgg-net

我试图微调VGG-16的最后一层。以下是我制作新模型的代码部分:

def train2false(model):
    for layer in model.layers:
        layer.trainable = False
    return model

def define_training_layers(model):
     model.layers = model.layers[0:21]
     model = train2false(model)
     last_layer = model.get_layer('fc7')
     out = Dense(n_classes, activation='softmax', name='fc8')(last_layer)
     model = Model(input=model.input, output=out)
     return model

def compile_model(epochs, lrate, model):
    decay = lrate / epochs
    sgd = SGD(lr=lrate, momentum=0, decay=0.0002, nesterov=True)
    model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
    print (model.summary())

    return model

def train_evaluate(model, X_train, y_train, X_test, y_test, epochs):
    model.fit(X_train, y_train, validation_data=(X_test, y_test), nb_epoch=epochs, batch_size=32)
    # Final evaluation of the model
    scores = model.evaluate(X_test, y_test, verbose=0)
    print("Accuracy: %.2f%%" % (scores[1]*100))

    return model

X_train, X_test, labels_test, labels_train, n_classes = load_dataset()
image_input = Input(shape=(3, 224, 224))
vgg_model = VGGFace(input_tensor= image_input, include_top=True)
custom_vgg_model = define_training_layers(vgg_model)
custom_vgg_model = compile_model(epochs=50, lrate=0.001, model=custom_vgg_model)
custom_vgg_model = train_evaluate(custom_vgg_model, X_train=X_train, y_train=labels_train, X_test=X_test, y_test=labels_test, epochs=50)

我收到以下错误:

  

tensorflow.python.framework.errors_impl.InvalidArgumentError:   两个形状中的尺寸1必须相等,但是为1000和2622   ' Assign_30' (op:' Assign')输入形状:[4096,1000],   [4096,2622]。

如果我尝试使用include_top=False微调所有完全连接的部分而不仅仅是softmax激活,它对我有用。

有什么东西我不见了吗?

1 个答案:

答案 0 :(得分:1)

解决!!!我从https://github.com/rcmalli/keras-vggface/releases/download/v1.0/rcmalli_vggface_th_weights_th_ordering.h5获取预训练的权重,其中有2622个输出,我有1000个输出。因此,只需更改VGG.py

中最后一层的输出数量