gpt4 book ai didi

tensorflow - 为什么我的混淆矩阵 "shifted"在右边?

转载 作者:行者123 更新时间:2023-11-30 09:31:19 24 4
gpt4 key购买 nike

我基于 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

最佳答案

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

关于tensorflow - 为什么我的混淆矩阵 "shifted"在右边?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56869657/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com