良好的训练和验证准确性,但混淆矩阵较差

时间:2021-01-13 11:29:38

标签: matrix deep-learning neural-network conv-neural-network training-data

我已训练我的模型来检测正常与肺炎胸部 X 光检查类别。这是我的数据集,如下所示:

train_batch= ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input)\
.flow_from_directory(directory=train_path, target_size=(224,224), classes=['NORMAL', 'PNEUMONIA'], 
batch_size=32,class_mode='categorical')
val_batch= ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input) \
.flow_from_directory(directory=val_path, target_size=(224,224), classes=['NORMAL', 'PNEUMONIA'], batch_size=32, class_mode='categorical')
test_batch= ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input) \
.flow_from_directory(directory=test_path, target_size=(224,224), classes=['NORMAL', 'PNEUMONIA'], batch_size=16,class_mode='categorical', shuffle=False)

Found 3616 images belonging to 2 classes.  #training

Found 1616 images belonging to 2 classes.  #validation

Found 624 images belonging to 2 classes.   #test

我的模型由 5 个 CNN 层组成,其中图像 w,h = (224* 224,3) 具有 16 个特征图作为第一层,然后是 32、64、128,256。批量归一化、最大池化和 dropout 被添加到每个 cnn 层,但最后一个密集层如下

model.add(Dense(units=2 , activation='softmax'))

optim = Adam( lr=0.001 )
model.compile(optimizer=optim , loss= 'categorical_crossentropy' , metrics= ['accuracy'])

history=model.fit_generator(train_batch,
                  steps_per_epoch= 113,   #3616/32=113

                  epochs = 25,
                  validation_data = val_batch,
                  validation_steps = 51   #1616/32=51
                  #verbose=2
                  #callbacks=callbacks #remove to chk

)

从图中可以看出,我的训练和验证准确性和损失都很好,但是当我绘制混淆矩阵时,它似乎不太好,为什么??

prediction = model.predict_generator(test_batch,steps= stepss) #, verbose=0)

prediction1 = np.argmax(prediction, axis=1)
cm = confusion_matrix (test_batch.classes, prediction1)
print(cm)

这是我的混淆矩阵如下 enter image description here

你可以看到我的图表如下

enter image description here

之后,我使用 VGG!6 对我的模型进行了微调,用我自己的具有两个输出的密集层替换了最后一个密集层,这是图形和混淆矩阵:

enter image description here enter image description here

我不明白为什么即使使用 vgg16 模型我的测试也不顺利,因为您可以看到结果,所以请给我您宝贵的建议谢谢

0 个答案:

没有答案