如何为图像分类器(CIFAR-10)打印混淆矩阵

时间:2019-04-30 15:53:40

标签: python tensorflow conv-neural-network confusion-matrix

我有一个图像分类器,在tensorflow上训练,在Cifar 10数据集上tflearn。它完全可以正常工作。现在,我需要创建其混淆矩阵。我不知道该怎么做。我已经在Google上搜索了它,但无法真正理解它。以下是我的图像分类器代码:

from __future__ import division, print_function, absolute_import
import tensorflow as tf
import tflearn
from tflearn.data_utils import shuffle, to_categorical
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.estimator import regression
from tflearn.data_preprocessing import ImagePreprocessing
from tflearn.data_augmentation import ImageAugmentation
import numpy as np
import cv2
from tqdm import tqdm
import os
import matplotlib.pyplot as plt

TRAIN_DIR = '../input/dataset/Train'
TEST_DIR = '../input/dataset/Test'
IMG_SIZE=32
def create_label(image_name):
    """ Create an one-hot encoded vector from image name """
    word_label = image_name.split('_')[1:2]
    word_label = word_label[0].split('.')[0:1]
    word_label = word_label[0]
    if word_label == 'cat':
        return np.array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    elif word_label == 'dog':
        return np.array([0, 1, 0, 0, 0, 0, 0, 0, 0, 0])
    elif word_label == 'automobile':
        return np.array([0, 0, 1, 0, 0, 0, 0, 0, 0, 0])
    elif word_label == 'airplane':
        return np.array([0, 0, 0, 1, 0, 0, 0, 0, 0, 0])
    elif word_label == 'ship':
        return np.array([0, 0, 0, 0, 1, 0, 0, 0, 0, 0])
    elif word_label == 'frog':
        return np.array([0, 0, 0, 0, 0, 1, 0, 0, 0, 0])
    elif word_label == 'truck':
        return np.array([0, 0, 0, 0, 0, 0, 1, 0, 0, 0])
    elif word_label == 'bird':
        return np.array([0, 0, 0, 0, 0, 0, 0, 1, 0, 0])
    elif word_label == 'horse':
        return np.array([0, 0, 0, 0, 0, 0, 0, 0, 1, 0])
    elif word_label == 'deer':
        return np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 1])

def create_train_data():
    training_data = []
    for img in tqdm(os.listdir(TRAIN_DIR)):
        path = os.path.join(TRAIN_DIR, img)
        img_data = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
        img_data = cv2.resize(img_data, (IMG_SIZE, IMG_SIZE))
        training_data.append([np.array(img_data), create_label(img)])
    shuffle(training_data)
    np.save('train_data1.npy', training_data)
    return training_data

def create_test_data():
    testing_data = []
    for img in tqdm(os.listdir(TEST_DIR)):
        path = os.path.join(TEST_DIR, img)
        img_num = img.split('.')[0]
        img_data = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
        img_data = cv2.resize(img_data, (IMG_SIZE, IMG_SIZE))
        testing_data.append([np.array(img_data), create_label(img)])

    shuffle(testing_data)
    np.save('test_data1.npy', testing_data)
    return testing_data

# If dataset is not created:
train_data = create_train_data()
test_data = create_test_data()

#train_data = np.load('train_data1.npy')
#test_data = np.load('test_data1.npy')
train = train_data[:40000]
val = train_data[-40000:]
test = test_data[:10000]
X_train = np.array([i[0] for i in train]).reshape(-1, IMG_SIZE,  IMG_SIZE, 1)
y_train = [i[1] for i in train]
X_val = np.array([i[0] for i in val]).reshape(-1, IMG_SIZE, IMG_SIZE, 1)
y_val = [i[1] for i in val]
X_test = np.array([i[0] for i in test]).reshape(-1, IMG_SIZE, IMG_SIZE, 1)
y_test = [i[1] for i in test]
y_train=np.array(y_train)
y_val=np.array(y_val)
y_test=np.array(y_test)
# Building The Model

X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_val = X_val.astype('float32')

# Real-time data preprocessing
img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()

# Real-time data augmentation
img_aug = ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_rotation(max_angle=25.)

# Convolutional network building
network = input_data(shape=[None, 32, 32, 1],
                 data_preprocessing=img_prep,
                 data_augmentation=img_aug)
network = conv_2d(network, 32, 3, activation='relu')
network = max_pool_2d(network, 2)
network = conv_2d(network, 64, 3, activation='relu')
network = conv_2d(network, 64, 3, activation='relu')
network = max_pool_2d(network, 2)
network = conv_2d(network, 64, 3, activation='relu')
network = conv_2d(network, 64, 3, activation='relu')
network = max_pool_2d(network, 2)
network = fully_connected(network, 512, activation='relu')
network = dropout(network, 0.5)
network = fully_connected(network, 10, activation='softmax')
network = regression(network, optimizer='adam',
                 loss='categorical_crossentropy',
                 learning_rate=0.001)

# Train using classifier
model = tflearn.DNN(network,tensorboard_dir='log', tensorboard_verbose=0)
model.fit(X_train, y_train, n_epoch=2, shuffle=True, validation_set=(X_val, y_val),
      show_metric=True, run_id='cifar10_cnn')
score = model.evaluate(X_test, y_test)
print('Test Accuracy: %0.4f%%' % (score[0] * 100))

我希望有人能帮助我绘制混乱矩阵,并提供我需要做的代码块。我必须在星期四提交报告,我需要认真的帮助。 谢谢。

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

我也被这个问题绊倒了。如果测试集中的每个样本都有一组预测标签,则可以使用 sklearn 绘制混淆矩阵。这很容易。 我已经使用更新的 tensorflow 库完成了它并将代码发布在 https://rschandrastechblog.blogspot.com/2021/05/plotting-confusion-matrix-for-cifar10.html

我不知道您是否能够将其放入您的代码中,但您可以看看它。