我正在使用 mobilenet v2 在我的图像上训练模型。除了几层外,我已经冻结了所有层,然后添加了额外的层进行训练。我希望能够从中间层而不是从一开始进行训练。我的问题:
- 是否可以提供最后一个冻结层的输出作为训练的输入(它将是 (?, 7,7,1280) 的张量)?
- 如何指定训练从第一个可训练(非冻结)层开始?在这种情况下,mbnetv2_conv.layer[153]。
- 在这种情况下 y_train 是什么?我不太明白 y_train 在训练过程中是如何使用的——一般来说,CNN 什么时候引用 y_train?
加载mobilenet v2
image_size = 224
mbnetv2_conv = MobileNetV2(weights='imagenet', include_top=False, input_shape=(image_size, image_size, 3))
# Freeze all layers except the last 3 layers
for layer in mbnetv2_conv.layers[:-3]:
layer.trainable = False
# Create the model
model = models.Sequential()
model.add(mbnetv2_conv)
model.add(layers.Flatten())
model.add(layers.Dense(16, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(3, activation='softmax'))
model.summary()
# Build an array (?,224,224,3) from images
x_train = np.array(all_images)
# Get layer output
from keras import backend as K
get_last_frozen_layer_output = K.function([mbnetv2_conv.layers[0].input],
[mbnetv2_conv.layers[152].output])
last_frozen_layer_output = get_last_frozen_layer_output([x_train])[0]
# Compile the model
from keras.optimizers import SGD
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['acc'])
# how to train from a specific layer and what should y_train be?
model.fit(last_frozen_layer_output, y_train, batch_size=2, epochs=10)