我正在Keras训练VGG16的最后一层。我的模型如下:
map_characters1 = {0: 'No Pneumonia', 1: 'Yes Pneumonia'}
class_weight1 = class_weight.compute_class_weight('balanced', np.unique(y_train), y_train)
weight_path1 = './imagenet_models/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5'
pretrained_model_1 = VGG16(weights = 'imagenet', include_top=False, input_shape=(200, 200, 3))
optimizer1 = keras.optimizers.Adam(lr=0.0001)
def pretrainedNetwork(xtrain,ytrain,xtest,ytest,pretrainedmodel,pretrainedweights,classweight,numclasses,numepochs,optimizer,labels):
base_model = pretrained_model_1 # Topless
# Add top layer
x = base_model.output
x = Flatten()(x)
predictions = Dense(numclasses, activation='relu')(x)
model = Model(inputs=base_model.input, outputs=predictions)
# Train top layer
for layer in base_model.layers:
layer.trainable = False
model.compile(loss='categorical_crossentropy',
optimizer=optimizer,
metrics=['accuracy'])
callbacks_list = [keras.callbacks.EarlyStopping(monitor='val_acc', patience=3, verbose=1)]
model.summary()
# Fit model
history = model.fit(xtrain,ytrain, epochs=numepochs, class_weight=classweight, validation_data=(xtest,ytest), verbose=1,callbacks = [MetricsCheckpoint('logs')])
# Evaluate model
score = model.evaluate(xtest,ytest, verbose=0)
print('\nKeras CNN - accuracy:', score[1], '\n')
return model
一开始的培训看起来不错:损失减少,准确性提高。但是随后损失变为微弱,准确度变为0.5-作为随机猜测。
模型:
input_1(InputLayer)(无,200、200、3)0
block1_conv1(Conv2D)(无,200、200、64)1792
block1_conv2(Conv2D)(无,200、200、64)36928
block1_pool(MaxPooling2D)(无,100、100、64)0
block2_conv1(Conv2D)(无,100、100、128)73856
block2_conv2(Conv2D)(无,100、100、128)147584
block2_pool(MaxPooling2D)(无,50、50、128)0
block3_conv1(Conv2D)(无,50、50、256)295168
block3_conv2(Conv2D)(无,50、50、256)590080
block3_conv3(Conv2D)(无,50、50、256)590080
block3_pool(MaxPooling2D)(无,25,25,256)0
block4_conv1(Conv2D)(无,25、25、512)1180160
block4_conv2(Conv2D)(无,25、25、512)2359808
block4_conv3(Conv2D)(无,25、25、512)2359808
block4_pool(MaxPooling2D)(无,12,12,512)0
block5_conv1(Conv2D)(无,12,12,512)2359808
block5_conv2(Conv2D)(无,12、12、512)2359808
block5_conv3(Conv2D)(无,12、12、512)2359808
block5_pool(MaxPooling2D)(无,6,6,512)0
flatten_2(Flatten)(无,18432)0
总参数:14,751,554 可训练的参数:36,866 不可训练参数:14,714,688
训练输出:
训练2682个样本,验证468个样本
史诗1/6 2682/2682 [==============================]-621s 232ms / step-损失:1.5150-acc:0.7662-val_loss :0.4117-val_acc:0.8526
史诗2/6 2682/2682 [==============================]-615s 229ms / step-损耗:0.2535-acc:0.9459-val_loss :1.7812-val_acc:0.7009
史诗3/6 2682/2682 [==============================]-621s 232ms / step-loss:nan-acc:0.7468-val_loss :nan-val_acc:0.5000
史诗4/6 2682/2682 [==============================]-644s 240ms / step-损失:nan-acc:0.5000-val_loss :nan-val_acc:0.5000
史诗5/6 2682/2682 [==============================]-616s 230ms / step-损失:nan-acc:0.5000-val_loss :nan-val_acc:0.5000
我在哪里可以找到问题?发生什么了?