1
$\begingroup$

For a homework I have to analyse a set of images. For this I plan to use convolutional neural network. The images are split onto specific folders :

  • A test set with 624 photos
    • dataset/test/normal (234 items)
    • dataset/test/pneumonia (390 items)
  • A train set with 5216 photos
    • dataset/train/normal (1341 items)
    • dataset/train/pneumonia (3875 items)

The objective is to learn a machine to detect if someone has pneumonia or not.

For this I try to build a convolutionel neural network and obtain rather nice results :

  • loss: 0.0328
  • accuracy: 0.9877
  • val_loss: 0.2308
  • val_accuracy: 0.9231

enter image description here

# Artificial Neural Network - Convolutional Neural Network

# Building the CNN
# Importing keras libraries and packages
import sys
from matplotlib import pyplot as plt
import numpy as np

from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers import Dense

from sklearn.metrics import classification_report as cr
from sklearn.metrics import confusion_matrix as cm

# defining variables
# Start
train_data_path = 'dataset/train'
test_data_path = 'dataset/test'
val_data_path = 'dataset/val'

img_rows = 64
img_cols = 64
epochs = 5
batch_size = 32
num_of_train_samples = 5216
num_of_test_samples = 624

# plot diagnostic learning curves
def summarize_diagnostics(history):
    # plot loss
    plt.subplot(211)
    plt.title('Cross Entropy Loss')
    plt.plot(history.history['loss'], color='blue', label='train')
    plt.plot(history.history['val_loss'], color='orange', label='test')
    # plot accuracy
    plt.subplot(212)
    plt.title('Classification Accuracy')
    plt.plot(history.history['accuracy'], color='blue', label='train')
    plt.plot(history.history['val_accuracy'], color='orange', label='test')
    # save plot to file
    filename = sys.argv[0].split('/')[-1]
    plt.savefig(filename + '_plot.png')
    plt.close()

# Initialising the CNN - Building the model
classifier = Sequential()
# Step 1 : Convolution (test with 64) & ReLU
classifier.add(Conv2D(32, (3, 3), 
                      input_shape=(img_rows, img_cols, 3), 
                      activation='relu')
    )
# Step 2 : Max Pooling
classifier.add(MaxPooling2D(pool_size=(2,2)))
classifier.add(Dropout(0.1))

# Step 2b - enhancing accuracy : adding convolution layers
classifier.add(Conv2D(32, (3, 3), activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2,2)))
classifier.add(Dropout(0.1))

classifier.add(Conv2D(64, (3, 3), activation='softmax'))
classifier.add(MaxPooling2D(pool_size=(2,2)))
classifier.add(Dropout(0.3))

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

# Step 4 : Full connection (hidden layer)
classifier.add(Dense(units=128, activation='relu'))
# enhancing accuracy : adding a layer 
classifier.add(Dense(units=128, activation='relu'))

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

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

# Part 2 : Fitting the CNN to the images
# https://keras.io/preprocessing/image/#imagedatagenerator-class
from keras.preprocessing.image import ImageDataGenerator

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

test_datagen = ImageDataGenerator(rescale=1./255)

# increase image size to enhance results
training_set = train_datagen.flow_from_directory(
        'dataset/train',
        target_size=(64, 64),
        batch_size=64,
        class_mode='binary')

test_set = test_datagen.flow_from_directory(
        'dataset/test',
        target_size=(64, 64),
        batch_size=64,
        class_mode='binary')

history = classifier.fit_generator(
        training_set,
        steps_per_epoch=5216,
        epochs=5,
        validation_data=test_set,
        validation_steps=624)

# evaluate model
_, acc = classifier.evaluate_generator(test_set, steps=len(test_set), verbose=0)
print('> %.3f' % (acc * 100.0))

summarize_diagnostics(history)

I would like to build the confusion matrix and know how many images from the training set. It would allow me to have the false positive and false negative results.

For this I wrote this piece of code :

#Confution Matrix and Classification Report
Y_pred = classifier.predict_generator(training_set, 624 // batch_size+1)
y_pred = np.argmax(Y_pred, axis=1)
print('Confusion Matrix')
matrix = cm(training_set.classes, y_pred)
print(matrix)
print('Classification Report')

print(cr(training_set.classes, y_pred))

while running those lines :

I don't get matrix variable and obtain an error message :

ValueError: Found input variables with inconsistent numbers of samples: [5216, 1280]

I'm not sure I set the confusion matrix correctly. Thanks.

$\endgroup$
0
$\begingroup$

This has to do with the different shapes you are feeding into the cm function. You are passing training_set.classes (which will have length n_classes) and y_pred (which will have length n_samples). Instead of passing training_set.classes you should therefore pass the real labels for each sample, so that this vector also has a length of n_samples.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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