0

i have an image classifier, trained in tensorflow, tflearn on cifar 10 dataset. its completely working. now i need to create its confusion matrix. i have no idea how to do it. i have searched it on google but cant really understand it. Following is my code for image classifier:

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))

I was hoping if someone could help me with plotting the confusion matrix and provide me the chunk of code i need to do it, please. I have to present it on thursday and i need serious help. Thanks.

Atif Ali
  • 71
  • 1
  • 8

1 Answers1

0

I was also stumbled at this question. If you have an array of predicted labels for each of the sample in test set, then you can plot the confusion matrix using sklearn. It is pretty easy. I have done it using the updated tensorflow library and posted the code at https://rschandrastechblog.blogspot.com/2021/05/plotting-confusion-matrix-for-cifar10.html

I don't know whether you will be able to fit it into your code, but you can just have a look at it.