3

I want to build a confusion matrix for my CNN model there is the code:

classifier = Sequential()


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


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

classifier.add(Flatten())
classifier.add(Dense(units=1, activation='sigmoid'))



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


batch_size = 32

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

test_datagen = ImageDataGenerator(rescale=1. / 255)

training_set = train_datagen.flow_from_directory('x1' ,
                                             target_size=(64,64),
                                             batch_size=64,
                                             class_mode='binary')

test_set = test_datagen.flow_from_directory('x2' ,
                                        target_size=(64,64),
                                        batch_size=64,
                                        class_mode='binary')



ep=50

H=classifier.fit_generator(training_set,
                     steps_per_epoch=1204/batch_size,
                     epochs=ep,
                     validation_data=test_set,
                     validation_steps=408/batch_size,
                                     )

validation_steps=408

confusion matrix:

from sklearn.metrics import confusion_matrix
Y_pred = classifier.predict_generator(test_set,validation_steps//batch_size+1)


y_pred = np.argmax(Y_pred, axis=1)
print('Confusion Matrix')
print(confusion_matrix(test_set.classes, y_pred))

I got this error :

ValueError: Found input variables with inconsistent numbers of samples: [408, 792]

What should I do?

1
  • Try model.predict_generator(test_generator,steps = len(test_set)) and check the shapes Commented Mar 13, 2019 at 11:28

2 Answers 2

1

test_set and y_pred don't have the same number of observations. Likely the number of steps you are passing to predict_generator is incorrect.

Not sure which version of Keras you are using, but try Y_pred =classifier.predict_generator(test_set). In newer versions leaving out number of steps will return predictions on all images.

Sign up to request clarification or add additional context in comments.

4 Comments

I use this it gives me confusion matrix but the confusion matrix it's not correct because it has two classes it returns [[ 42 0] [366 0]] each of them are the number of items that are in two classes, this means it predicts all of them correct? if it is true why accrucy is not 100 %
No, that doesn't mean it's 100% correct. It means it is classifying every single observation into the same class. Top-left and bottom-right are correct classifications, so 366 are being classified incorrectly.
why all of them classified into one class? is my code incorrect? could you please take a look at my code @karl
It means your model has learnt it is easier to simply classify everything as one class rather than trying to split them. There could be a hundred reasons for this. Your model might be too simple, your data might not be good enough, your learning rate might be too high, etc...
1

You have

batch_size = 32

but inside test_set,

batch_size=64

so when you run predict_generator with validation_steps//batch_size+1 steps you are using 32 to create your steps but 64 for your generator.

You must set both to 32 or 64

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.