为什么我的混乱矩阵向右“移动”?

时间:2019-07-03 11:58:22

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

我基于ResNet 50建立了4种花的分类器。在训练过程中,准确性非常高,而且一切似乎都很好。但是,一旦绘制出混淆矩阵,我就会看到这些值向右“移动”,而不是在主对角线中。

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

Confusion Matrix

这是我使用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

3 个答案:

答案 0 :(得分:1)

是的,我想这是代码,请检查您在创建混淆矩阵的索引,

答案 1 :(得分:1)

查看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,并且仅随机播放图像而不是标签。

答案 2 :(得分:0)

这是一个有趣的问题。可以通过在执行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))