Python 绘制所有样本的混淆矩阵

Python 绘制所有样本的混淆矩阵,python,tensorflow,deep-learning,confusion-matrix,Python,Tensorflow,Deep Learning,Confusion Matrix,嗨,我正试图为我用于测试的所有样本绘制一个混淆矩阵。但是,由于我已经指定了batc_大小,因此混淆矩阵仅为指定数量的批次_大小输出所有正确的类。也就是说,如果我总共有3000个样本,而不是预测所有3000个样本,那么混淆矩阵只预测150个样本,如果批量大小指定为150。你能帮我找出我能做些什么来为所有3000个样本绘制混淆矩阵吗 num_classes = 2 image_resize = 256 train_dir ='./..../..'#3000 samples

嗨,我正试图为我用于测试的所有样本绘制一个混淆矩阵。但是,由于我已经指定了batc_大小,因此混淆矩阵仅为指定数量的批次_大小输出所有正确的类。也就是说,如果我总共有3000个样本,而不是预测所有3000个样本,那么混淆矩阵只预测150个样本,如果批量大小指定为150。你能帮我找出我能做些什么来为所有3000个样本绘制混淆矩阵吗

    num_classes = 2
    image_resize = 256
    train_dir ='./..../..'#3000 samples
    test_dir = './.../...'#3000 samples
    batch_size_training = 150
    batch_size_validation = 150
    num_epochs = 10
    
    data_generator = ImageDataGenerator(
        preprocessing_function=preprocess_input,validation_split=0.2)
    
    
    train_generator = data_generator.flow_from_directory(
        train_dir,
        target_size=(image_resize, image_resize),
        batch_size=batch_size_training,
        class_mode='categorical')
    validation_generator = data_generator.flow_from_directory(
        train_dir,
        target_size=(image_resize, image_resize),
        batch_size=batch_size_validation,
        class_mode='categorical')
    test_generator = data_generator.flow_from_directory(
        test_dir,
        target_size=(image_resize, image_resize),
        batch_size=batch_size_validation,
        class_mode='categorical')
    
    x_train, y_train = next(train_generator)
    x_val,y_val = next(validation_generator)
    x_test, y_test = next(test_generator)
model.compile(loss='categorical_crossentropy',metrics=['accuracy'])


steps_per_epoch_training = int(np.floor(train_generator.n // batch_size_training ))

steps_per_epoch_validation = int(np.floor(validation_generator.n // batch_size_validation ))


fit_history = model.fit(train_generator,
    steps_per_epoch=steps_per_epoch_training,
    epochs=num_epochs,
    validation_data=validation_generator,
    validation_steps=steps_per_epoch_validation,
    verbose=1,
)
    
probs = model.predict(x_test)
preds = probs.argmax(axis = -1)
accuracy = 100*(np.mean(preds == y_test.argmax(axis=-1)))
y_test = np.argmax(y_test,axis=-1)
        
print("Classification accuracy: %f " % (accuracy))
cm =confusion_matrix(y_test,preds)
print(cm)
df_cm = pd.DataFrame(cm, range(2), range(2))
fig = plt.figure(figsize=(10,7))
sn.set(font_scale=1.4) # for label size
sn.heatmap(df_cm, annot=True, annot_kws={"size": 16}) # font size
fig.savefig('CM.jpg')
        

您的问题是您实际上只从数据生成器中获取了一批数据:

。。。
x\U列,y\U列=下一列(列发电机)
x_val,y_val=next(验证生成器)
x_测试,y_测试=下一步(测试生成器)
...
然后您使用单个测试批对其运行预测:

。。。
probs=模型预测(x_检验)
...
因此,代码在单个批处理上完全按照预期运行

要对生成器中的所有测试数据运行预测,您只需执行以下操作:

# to get predictions for all test data points
probs = model.predict(test_generator)
# to get labels for all the test data points
y_test = test_generator.labels

x_测试的形状是什么?