我已用全局平均池替换了VGG16模型的最后两个完全连接的层,并使用乳腺X线照片图像将此CNN用于二进制分类(0级:良性,1级:恶性)。学习率设置为0.0000001,纪元=500。训练损耗曲线在较大范围内波动(如大的锯齿形),而验证损耗曲线则像一条平坦的直线。我附上训练损失与验证损失曲线的关系图,以更好地理解。training loss vs validation loss curve
另一方面,训练精度曲线也在较大范围内波动(如大的锯齿形),验证精度曲线就像一条直线,我还附上训练精度与验证精度曲线的图一个更好理解的数字。training accuracy vs validation accuracy curve
为了训练280张(良性:136张,恶性:144张)图像,应用了数据增强。为了进行验证,使用了27张(良性:14张,恶性:13张)图像。为了进行测试,使用了21张(良性:5张,恶性:16张)图像。混淆矩阵看起来不错:Confusion matrix
我的问题是,当验证损失曲线总是平坦时,为什么训练损失曲线会波动?而且,当验证准确性曲线总是平坦时,为什么我的训练准确性曲线会波动?
# Show the image paths
train_path = 'Digital_Mamo/OPTIMAM' # Relative Path
valid_path = 'Digital_Mamo/InBreast'
test_path = 'Digital_Mamo/BCDR'
valid_batches = ImageDataGenerator().flow_from_directory(valid_path, target_size=(224, 224), classes=['Benign', 'Malignant'], batch_size=9) #, color_mode="grayscale"
test_batches = ImageDataGenerator().flow_from_directory(test_path, target_size=(224, 224), classes=['Benign', 'Malignant'], batch_size=7)
datagen = ImageDataGenerator(rotation_range=10, width_shift_range=0.1,
height_shift_range=0.1, shear_range=0.15, zoom_range=0.1,
channel_shift_range=10., horizontal_flip=True)
train_batches = datagen.flow_from_directory(
train_path,
target_size=(224, 224),
batch_size=10,
classes=['Benign','Malignant'])
vgg16_model= load_model('Fetched_VGG.h5')
# transform the model to Sequential
for layer in vgg16_model.layers[:-4]:
model.add(layer)
model.add(GlobalAveragePooling2D())
# Freezing the layers (Oppose weights to be updated)
for layer in model.layers:
layer.trainable = False
model.add(Dense(2, activation='softmax', name='predictions'))
### Compile the model
model.compile(Adam(lr=.0000001), loss='categorical_crossentropy', metrics=['accuracy']) # lr=.0001
# train the model
hist = model.fit_generator(train_batches, steps_per_epoch=28, validation_data=valid_batches, validation_steps=3, epochs=500, verbose=2) # epochs=5
scoreSeg = model.evaluate_generator(test_batches, steps=3)
print('Test Loss:', scoreSeg[0])
print('Test accuracy:', scoreSeg[1])
#test
predictions = model.predict_generator(test_batches, steps=3, verbose=0)
#print(predictions)