0

我正在尝试微调 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 必须相等,但对于输入形状为 [4096,1000]、[4096,2622] 的“Assign_30”(操作:“Assign”)分别为 1000 和 2622 .

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

有什么我想念的吗?

4

1 回答 1

1

解决了!!!我从https://github.com/rcmalli/keras-vggface/releases/download/v1.0/rcmalli_vggface_th_weights_th_ordering.h5获取了预训练的权重,它有 2622 个输出,我有 1000 个输出。所以只需更改 VGG.py 中最后一层的输出数量

于 2017-04-02T11:15:04.517 回答