Python Forum
Help in adding confusion matrix
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help in adding confusion matrix
#1
Help....performed image classification on cifar-10 dataset but not able to add confusion matrix.
Kindly help me out in adding confusion matrix in this code.
from keras.models import Sequential
from keras.layers import Convolution2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense

from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True


classifier = Sequential()


classifier.add(Convolution2D(32, 3, 3, input_shape = (32, 32, 3), activation = 'relu'))


classifier.add(MaxPooling2D(pool_size = (2, 2)))


classifier.add(Convolution2D(32, 3, 3, activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))


classifier.add(Convolution2D(32, 3, 3, activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))



classifier.add(Flatten())

classifier.add(Dense(output_dim = 128, activation = 'relu'))
classifier.add(Dense(output_dim = 10, activation = 'sigmoid'))


classifier.compile(optimizer = 'Adam', loss = 'binary_crossentropy', metrics = ['accuracy'])


from keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale = 1./255,
                                   shear_range = 0.2,
                                   zoom_range = 0.4,
                                   horizontal_flip = True)

test_datagen = ImageDataGenerator(rescale = 1./255)

training_set = train_datagen.flow_from_directory('Dataset/train',
                                                 target_size = (32,32),
                                                 batch_size = 64,
                                                 class_mode = 'categorical')

test_set = test_datagen.flow_from_directory('Dataset/test',
                                            target_size = (32,32),
                                            batch_size = 64,
                                            class_mode = 'categorical')

classifier.fit_generator(
        training_set,
        steps_per_epoch=1000,
        epochs=25,
        validation_data=test_set,
        validation_steps=2000)
[icode]
Reply
#2
Something like this, but not tested:

import numpy as np # move to the beginning of the file
from sklearn.metrics import confusion_matrix 
# ....
_ = classifier.predict_generator(test_set, num_of_test_samples // batch_size+1)
y_pred = np.argmax(_, axis=1)
print('Confusion Matrix')
print(confusion_matrix(test_set.classes, y_pred))
Reply
#3
Its not working
Reply
#4
The main idea is to pass predicted and original class label arrays to the
confusion_matrix function. Arrays should have the same length. Currently, I haven't installed keras framework and can't reproduce the problem...
Reply
#5
Here is the final code ...its not working ..please help
import numpy as np 
from keras.models import Sequential
from keras.layers import Convolution2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
from sklearn.metrics import confusion_matrix

from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True

# Initialising the CNN
classifier = Sequential()

# Step 1 - Convolution
classifier.add(Convolution2D(32, 3, 3, input_shape = (32, 32, 3), activation = 'relu'))

# Step 2 - Pooling
classifier.add(MaxPooling2D(pool_size = (2, 2)))

# Adding a second convolutional layer
classifier.add(Convolution2D(32, 3, 3, activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))


classifier.add(Convolution2D(32, 3, 3, activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))


# Step 3 - Flattening
classifier.add(Flatten())

# Step 4 - Full connection
classifier.add(Dense(output_dim = 128, activation = 'relu'))
classifier.add(Dense(output_dim = 10, activation = 'sigmoid'))

# Compiling the CNN
classifier.compile(optimizer = 'Adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

# Part 2 - Fitting the CNN to the images

from keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale = 1./255,
                                   shear_range = 0.2,
                                   zoom_range = 0.4,
                                   horizontal_flip = True)

test_datagen = ImageDataGenerator(rescale = 1./255)

training_set = train_datagen.flow_from_directory('Dataset/train',
                                                 target_size = (32,32),
                                                 batch_size = 64,
                                                 class_mode = 'categorical')
print('Before Test Set')

test_set = test_datagen.flow_from_directory('Dataset/test',
                                            target_size = (32,32),
                                            batch_size = 64,
                                            class_mode = 'categorical')

classifier.fit_generator(
        training_set,
        steps_per_epoch=2,
        epochs=1,
        validation_data=test_set,
        validation_steps=20)

print('After Epoch')
#Confution Matrix and Classification Report
Y_pred = classifier.predict_generator(test_set, 60000)
y_pred = np.argmax(Y_pred, axis=1)
print('Confusion Matrix')
print(confusion_matrix(test_set.classes, y_pred))
print('Classification Report')
target_names = ['Airplan','Car','Birds','Cats','Deer', 'Dogs','Frog', 'Horse','Ship','Truck']
print(classification_report(test_set.classes, y_pred, target_names=target_names))
Reply
#6
(Apr-15-2019, 03:10 PM)Aashish Wrote: Here is the final code ...its not working
What means it isn't working. What kind of error occurred?

Do test_set.classes and y_pred variables have the same sizes?
Reply
#7
You have to put shuffle=False when you do test_datagen.flow_from_directory() so the samples don't get shuffled and have the same order as validation_generator.classes
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Alternative ways of plotting confusion matrix amjass12 0 2,371 Aug-14-2019, 07:21 PM
Last Post: amjass12
  PyCM 1.8 released: Machine learning library for confusion matrix statistical analysis sepandhaghighi 0 2,129 Jan-05-2019, 12:36 PM
Last Post: sepandhaghighi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020