【发布时间】: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() 时,预测都会改变,总是向右移动一个单元格。
【问题讨论】:
-
没有你的代码怎么可能有人知道?
-
我想知道这是否经常发生。这就是为什么我问它是否可能是我的代码。刚刚更新添加模型代码。
-
你能链接到sklearn上的例子吗?
-
两个问题:1)当你说:“训练时准确率真的很高”时,是指验证集吗? 2) 你的验证集是什么形状的?
-
是的,训练集和验证集的准确率约为 90%。 (无,150,150,3)。
标签: tensorflow machine-learning keras neural-network confusion-matrix