【问题标题】:Why is my confusion matrix "shifted" to the right?为什么我的混淆矩阵“向右移动”?
【发布时间】:2019-11-14 02:47:39
【问题描述】:

我基于 ResNet 50 构建了一个 4 种花型的分类器。训练的时候准确率真的很高,看起来一切都很好。然而,一旦我绘制了我的混淆矩阵,我发现这些值被“移动”到了右边而不是主对角线上。

这是什么意思?是我的数据集有问题,还是我的代码有问题?

这是我使用 ResNet 50 所做的:

def create_model(input_shape, top='flatten'):
    if top not in ('flatten', 'avg', 'max'):
        raise ValueError('unexpected top layer type: %s' % top)

    # connects base model with new "head"
    BottleneckLayer = {
        'flatten': Flatten(),
        'avg': GlobalAvgPooling2D(),
        'max': GlobalMaxPooling2D()
    }[top]

    base = InceptionResNetV2(input_shape=input_shape,
                             include_top=False, 
                             weights='imagenet')

    x = BottleneckLayer(base.output)
    x = Dense(NUM_OF_FLOWERS, activation='linear')(x)
    model = Model(inputs=base.inputs, outputs=x)
    return model

    base = ResNet50(input_shape=input_shape, include_top=False)
    x = Flatten()(base.output)
    x = Dense(NUM_OF_FLOWERS, activation='softmax')(x)
    model = Model(inputs=base.inputs, outputs=x)

混淆矩阵生成:

# Predict the values from the validation dataset
Y_pred = model.predict_generator(validation_generator, nb_validation_samples // batch_size+1)
# Convert predictions classes to one hot vectors 
Y_pred_classes = numpy.argmax(Y_pred, axis = 1) 
# Convert validation observations to one hot vectors
Y_true = validation_generator.classes 
# compute the confusion matrix
confusion_mtx = confusion_matrix(Y_true, Y_pred_classes) 
# plot the confusion matrix
plot_confusion_matrix(confusion_mtx, classes = range(4))

根据要求,这就是我创建生成器的方式:

train_generator = train_datagen.flow_from_directory(
    train_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    color_mode='rgb',
    class_mode='categorical',
    shuffle=True)

validation_generator = test_datagen.flow_from_directory(
    validation_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    color_mode='rgb',
    class_mode='categorical',
    shuffle=False)

这是我的混淆矩阵的相册。每次我执行 model.predict() 时,预测都会改变,总是向右移动一个单元格。

Confusion Matrix Album

【问题讨论】:

  • 没有你的代码怎么可能有人知道?
  • 我想知道这是否经常发生。这就是为什么我问它是否可能是我的代码。刚刚更新添加模型代码。
  • 你能链接到sklearn上的例子吗?
  • 两个问题:1)当你说:“训练时准确率真的很高”时,是指验证集吗? 2) 你的验证集是什么形状的?
  • 是的,训练集和验证集的准确率约为 90%。 (无,150,150,3)。

标签: tensorflow machine-learning keras neural-network confusion-matrix


【解决方案1】:

是的,我想它是代码,检查创建混淆矩阵的索引,它会偏离一个

【讨论】:

  • 我添加了用于生成混淆矩阵的代码,我相信这不是问题,因为我是从 SKLearn 网站获取的。
  • 你有没有想过这个问题?对于测试集,数据集标签肯定要减一...
【解决方案2】:

查看validation_generator 类。当您使用 data_generator.flow_from_directory 时,您需要查看参数 shuffle 是否等于 False,如上例所示:

 val_generator = val_data_generator.flow_from_directory(
        test_data_dir,
        target_size=(IMAGE_WIDTH, IMAGE_HEIGHT),
        batch_size=100,
        class_mode="binary",
        classes=['dog','cat'],
        shuffle=False)  

因为默认参数是 True 并且唯一随机播放图像而不是标签。

【讨论】:

  • 这是错误的:/我之前遇到过这个问题,但是使用 shuffle=False 后我得到了更好的结果。谢谢!
【解决方案3】:

这是一个有趣的问题。它可以通过在执行 model.predict 之前重新加载 imagedatagenerator 来修复。

所以:

validation_generator = test_datagen.flow_from_directory(
    validation_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    color_mode='rgb',
    class_mode='categorical',
    shuffle=False)

Y_pred = model.predict_generator(validation_generator, nb_validation_samples // batch_size+1)
# Convert predictions classes to one hot vectors 
Y_pred_classes = numpy.argmax(Y_pred, axis = 1) 
# Convert validation observations to one hot vectors
Y_true = validation_generator.classes 
# compute the confusion matrix
confusion_mtx = confusion_matrix(Y_true, Y_pred_classes) 
# plot the confusion matrix
plot_confusion_matrix(confusion_mtx, classes = range(4))

【讨论】:

    猜你喜欢
    • 2020-05-21
    • 1970-01-01
    • 2019-09-28
    • 2021-11-30
    • 1970-01-01
    • 2014-07-09
    • 2015-12-17
    • 2020-10-01
    • 2012-01-20
    相关资源
    最近更新 更多