使用 Keras 微调 VGG 时输入形状的问题
Problems with input shapes at finetuning VGG with Keras
我正在尝试微调 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:
Dimension 1 in both shapes must be equal, but are 1000 and 2622 for
'Assign_30' (op: 'Assign') with input shapes: [4096,1000],
[4096,2622].
如果我尝试使用 include_top=False
而不仅仅是 softmax 激活来微调所有完全连接的部分,它对我有用。
有什么我遗漏的吗?
解决了!!!我从 https://github.com/rcmalli/keras-vggface/releases/download/v1.0/rcmalli_vggface_th_weights_th_ordering.h5 中获取了预训练的权重,它有 2622 个输出,我有 1000 个输出。所以只需更改 VGG.py
中最后一层的输出数量
我正在尝试微调 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: Dimension 1 in both shapes must be equal, but are 1000 and 2622 for 'Assign_30' (op: 'Assign') with input shapes: [4096,1000], [4096,2622].
如果我尝试使用 include_top=False
而不仅仅是 softmax 激活来微调所有完全连接的部分,它对我有用。
有什么我遗漏的吗?
解决了!!!我从 https://github.com/rcmalli/keras-vggface/releases/download/v1.0/rcmalli_vggface_th_weights_th_ordering.h5 中获取了预训练的权重,它有 2622 个输出,我有 1000 个输出。所以只需更改 VGG.py
中最后一层的输出数量