1
$\begingroup$

I am writing a code to classify images from two classes, dogs and cats. I wrote the below code, but always all the dogs images are classified as cats as shown in the confusion matrix.

Am I missing something in the code?

Note: I tried epochs up to 70, but got same results.

from keras.models import Sequential
from keras.layers import Conv2D,Activation,MaxPooling2D,Dense,Flatten,Dropout
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
from IPython.display import display
import matplotlib.pyplot as plt
from PIL import Image
from sklearn.metrics import classification_report, confusion_matrix


classifier = Sequential()
classifier.add(Conv2D(32,(3,3),input_shape=(64,64,3)))
classifier.add(Activation('relu'))
classifier.add(MaxPooling2D(pool_size =(2,2)))
classifier.add(Conv2D(32,(3,3)))
classifier.add(Activation('relu'))
classifier.add(MaxPooling2D(pool_size =(2,2)))
classifier.add(Conv2D(64,(3,3)))
classifier.add(Activation('relu'))
classifier.add(MaxPooling2D(pool_size =(2,2)))
classifier.add(Flatten())
classifier.add(Dense(64))
classifier.add(Activation('relu'))
classifier.add(Dropout(0.5))
classifier.add(Dense(1))
classifier.add(Activation('sigmoid'))
classifier.summary()
classifier.compile(optimizer ='rmsprop',
                   loss ='binary_crossentropy',
                   metrics =['accuracy'])
train_datagen = ImageDataGenerator(rescale =1./255,
                                   shear_range =0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip =True)
test_datagen = ImageDataGenerator(rescale = 1./255)

batchsize=32
training_set = train_datagen.flow_from_directory('/home/osboxes/Downloads/Downloads/dogs-vs-cats/train/',
                                                target_size=(64,64),
                                                batch_size= batchsize,
                                                class_mode='binary')

test_set = test_datagen.flow_from_directory('/home/osboxes/Downloads/Downloads/dogs-vs-cats/test/',
                                           target_size = (64,64),
                                           batch_size = batchsize,
                       shuffle=False,
                                           class_mode ='binary')
history=classifier.fit_generator(training_set,
                        steps_per_epoch =9000 // batchsize,
                        epochs = 30,
                        validation_data =test_set,
                        validation_steps = 4500 // batchsize)

Y_pred = classifier.predict_generator(test_set, steps=4500 // batchsize+1)
y_pred = np.argmax(Y_pred, axis=1)
print('Confusion Matrix')
print(confusion_matrix(test_set.classes, y_pred))
print('Classification Report')
target_names = test_set.classes
class_labels = list(test_set.class_indices.keys())   
report = classification_report(target_names, y_pred, target_names=class_labels)
print(report) 

# summarize history for accuracy
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()


$\endgroup$

2 Answers 2

1
$\begingroup$

Replace your line

y_pred = np.argmax(Y_pred, axis=1)

by:

y_pred = Y_pred > 0.5

You might need to flatten your Y_pred array beforehand.

Your classifier is returning a score between 0 and 1 defining how probable the image belongs to class 1. When you call argmax on your prediction, it will always return 0, as there always is a single entry in your array.

$\endgroup$
0
$\begingroup$

I think you should replace this:

classifier.add(Dense(2))

because you have 2 class: cat & dog.

$\endgroup$
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.