我正在关注转移学习的blog:
##First I compute the saved the bottleneck features and build a new model and train it with the bottleneck features:
input_layer = Input(shape=base_model.output_shape[1:])
x = GlobalAveragePooling2D()(input_layer)
x = Dense(512, activation='relu',name='fc_new_1')(x)
x = Dropout(0.2)(x)
x = Dense(512, activation='relu',name='fc_new_2')(x)
x = Dense(num_classes, activation='softmax',name='logit_new')(x)
Add_layers = Model(inputs=input_layer, outputs=x,name='Add_layers')
##Then I put this new model at the end of pretrained models:
base_model = ResNet50(include_top=False, weights='imagenet', input_shape=
(img_shape[0],img_shape[1],3))
x = base_model.output
predictions = Add_layers(x)
model = Model(inputs=base_model.input, outputs=predictions)
model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=
['accuracy'])
##Then, I evaluate the model :
score = model.evaluate_generator(train_generator, nb_train_samples //
batch_size_finetuning)
print('The evaluation of the entire model before fine tuning : ')
print(score)
score = model.evaluate_generator(validation_generator, nb_validation_samples
// batch_size_evaluation)
print('The evaluation of the entire model before fine tuning : ')
print(score)
获得培训损失和准确性:[0.015362062912073827, 1.0]
验证损失和准确性:[0.89740632474422455, 0.75]
##Just one line below it, I trained the new model:
model.fit_generator(train_generator,
steps_per_epoch= nb_train_samples // batch_size_finetuning,
epochs=finetuning_epoch,
validation_data=validation_generator,
validation_steps=nb_validation_samples //batch_size_evaluation,
callbacks=[checkpointer_finetuning,
history_finetuning,TB_finetuning,
lrate_finetuning,Eartly_Stopping_finetuning]);
然后输出是:
31/31 [==============================] - 35s - loss: 3.4004 - acc: 0.3297 - val_loss: 0.9591 - val_acc: 0.7083
奇怪的是:只有当我使用Resnet50
和InceptionV3
而不是vgg16
时,才会出现此问题。我很确定改变预训练模型是唯一的区别。我知道辍学可能会有所不同,但不应该这么大,vgg16
根本没有明显的问题。
另一个奇怪的事情是:如果我将每个图层更改为.trainable = False
并进行编译,则验证准确性仍会显着降低。我甚至检查了每个图层的权重,如果.trainable = False
权重不会改变,.trainable = True
权重会发生变化。
任何帮助表示赞赏!致谢!!!