【发布时间】:2018-12-19 07:05:08
【问题描述】:
我用 keras 构建了一个图像分类 CNN。虽然模型本身工作正常(它可以正确预测新数据),但我在绘制模型的混淆矩阵和分类报告时遇到了问题。
我使用 ImageDataGenerator 训练了模型
train_path = '../DATASET/TRAIN'
test_path = '../DATASET/TEST'
IMG_BREDTH = 30
IMG_HEIGHT = 60
num_classes = 2
train_batch = ImageDataGenerator(featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=45,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True,
vertical_flip=False).flow_from_directory(train_path,
target_size=(IMG_HEIGHT, IMG_BREDTH),
classes=['O', 'R'],
batch_size=100)
test_batch = ImageDataGenerator().flow_from_directory(test_path,
target_size=(IMG_HEIGHT, IMG_BREDTH),
classes=['O', 'R'],
batch_size=100)
这是混淆矩阵和分类报告的代码
batch_size = 100
target_names = ['O', 'R']
Y_pred = model.predict_generator(test_batch, 2513 // batch_size+1)
y_pred = np.argmax(Y_pred, axis=1)
print('Confusion Matrix')
cm = metrics.confusion_matrix(test_batch.classes, y_pred)
print(cm)
print('Classification Report')
print(metrics.classification_report(test_batch.classes, y_pred))
对于混淆矩阵,我得到滚动结果(这似乎是错误的)
Confusion Matrix
[[1401 0]
[1112 0]]
假阳性和真阳性均为 0。 对于分类报告,我得到以下输出和警告
Classification Report
precision recall f1-score support
0 0.56 1.00 0.72 1401
1 0.00 0.00 0.00 1112
avg / total 0.31 0.56 0.40 2513
/Users/sashaanksekar/anaconda3/lib/python3.6/site-packages/sklearn/metrics/classification.py:1135: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples.
'precision', 'predicted', average, warn_for)
我试图预测一个物体是有机的还是可回收的。我有大约 22000 张训练图像和 2513 张测试图像。
我是机器学习的新手。我究竟做错了什么?
提前致谢
【问题讨论】:
-
问题/错误是什么?
-
混淆矩阵的真阳性和假阳性为0。我觉得这是因为我初始化y_pred的方式。我也不明白分类报告的警告信息
-
您需要确保在
metrics.confusion_matrix()中以正确的顺序插入y_pred和y_true。其次,这种糟糕的性能可能是由于过度拟合或模型不佳造成的。你使用交叉验证吗? -
不,我没有。由于我是机器学习的新手,我不知道如何处理图像数据。
-
可以添加数据吗?
标签: python scikit-learn keras