Select Page

Lung Cancer Detection using Convolutional Neural Network (CNN)

satyam kumar
Published: September 4, 2022

Computer Vision is one of the applications of deep neural networks that enables us to automate tasks that earlier required years of expertise and one such use in predicting the presence of cancerous cells.

In this article, we will learn how to build a classifier using a simple Convolution Neural Network which can classify normal lung tissues from cancerous. This project has been developed using collab and the dataset has been taken from Kaggle whose link has been provided as well.

The process which will be followed to build this classifier:

Flow Chart for the Project

Flow Chart for the Project

Modules Used

Python libraries make it very easy for us to handle the data and perform typical and complex tasks with a single line of code.

  • Pandas This library helps to load the data frame in a 2D array format and has multiple functions to perform analysis tasks in one go.
  • Numpy Numpy arrays are very fast and can perform large computations in a very short time.
  • Matplotlib This library is used to draw visualizations.
  • Sklearn – This module contains multiple libraries having pre-implemented functions to perform tasks from data preprocessing to model development and evaluation.
  • OpenCVThis is an open-source library mainly focused on image processing and handling.
  • Tensorflow – This is an open-source library that is used for Machine Learning and Artificial intelligence and provides a range of functions to achieve complex functionalities with single lines of code.


import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

from PIL import Image

from glob import glob


from sklearn.model_selection import train_test_split

from sklearn import metrics


import cv2

import gc

import os


import tensorflow as tf

from tensorflow import keras

from keras import layers


import warnings


Importing Dataset

The dataset which we will use here has been taken from -  This dataset includes 5000 images for three classes of lung conditions:

  • Normal Class
  • Lung Adenocarcinomas
  • Lung Squamous Cell Carcinomas

These images for each class have been developed from 250 images by performing Data Augmentation on them. That is why we won’t be using Data Augmentation further on these images.


from zipfile import ZipFile


data_path = ''


with ZipFile(data_path,'r') as zip:


  print('The data set has been extracted.')


The data set has been extracted.

Data Visualization

In this section, we will try to understand visualize some images which have been provided to us to build the classifier for each class.


path = 'lung_colon_image_set/lung_image_sets'

classes = os.listdir(path)



['lung_n', 'lung_aca', 'lung_scc']

These are the three classes that we have here.


path = '/lung_colon_image_set/lung_image_sets'


for cat in classes:

    image_dir = f'{path}/{cat}'

    images = os.listdir(image_dir)


    fig, ax = plt.subplots(1, 3, figsize=(15, 5))

    fig.suptitle(f'Images for {cat} category . . . .', fontsize=20)


    for i in range(3):

        k = np.random.randint(0, len(images))

        img = np.array('{path}/{cat}/{images[k]}'))




Images for lung_n category

Images for lung_n category

Images for lung_aca category

Images for lung_aca category

Images for lung_scc category

Images for lung_scc category

The above output may vary if you will run this in your notebook because the code has been implemented in such a way that it will show different images every time you rerun the code.

Data Preparation for Training

In this section, we will convert the given images into NumPy arrays of their pixels after resizing them because training a Deep Neural Network on large-size images is highly inefficient in terms of computational cost and time.

For this purpose, we will use the OpenCV library and Numpy library of python to serve the purpose. Also, after all the images are converted into the desired format we will split them into training and validation data so, that we can evaluate the performance of our model.


IMG_SIZE = 256

SPLIT = 0.2



Some of the hyperparameters which we can tweak from here for the whole notebook.


X = []

Y = []


for i, cat in enumerate(classes):

  images = glob(f'{path}/{cat}/*.jpeg')


  for image in images:

    img = cv2.imread(image)


    X.append(cv2.resize(img, (IMG_SIZE, IMG_SIZE)))



X = np.asarray(X)

one_hot_encoded_Y = pd.get_dummies(Y).values

One hot encoding will help us to train a model which can predict soft probabilities of an image being from each class with the highest probability for the class to which it really belongs.


X_train, X_val, Y_train, Y_val = train_test_split(X, one_hot_encoded_Y,

                                                  test_size = SPLIT,

                                                  random_state = 2022)

print(X_train.shape, X_val.shape)


(12000, 256, 256, 3) (3000, 256, 256, 3)

In this step, we will achieve the shuffling of the data automatically because the train_test_split function split the data randomly in the given ratio.

Model Development

From this step onward we will use the TensorFlow library to build our CNN model. Keras framework of the tensor flow library contains all the functionalities that one may need to define the architecture of a Convolutional Neural Network and train it on the data.

Model Architecture

We will implement a Sequential model which will contain the following parts:

  • Three Convolutional Layers followed by MaxPooling Layers.
  • The Flatten layer to flatten the output of the convolutional layer.
  • Then we will have two fully connected layers followed by the output of the flattened layer.
  • We have included some BatchNormalization layers to enable stable and fast training and a Dropout layer before the final layer to avoid any possibility of overfitting.
  • The final layer is the output layer which outputs soft probabilities for the three classes. 


model = keras.models.Sequential([


                  kernel_size=(5, 5),






    layers.MaxPooling2D(2, 2),



                  kernel_size=(3, 3),



    layers.MaxPooling2D(2, 2),



                  kernel_size=(3, 3),



    layers.MaxPooling2D(2, 2),



    layers.Dense(256, activation='relu'),


    layers.Dense(128, activation='relu'),



    layers.Dense(3, activation='softmax')


Let’s print the summary of the model’s architecture:


Model: “sequential”


 Layer (type)                Output Shape              Param #   


 conv2d (Conv2D)             (None, 256, 256, 32)      2432      


 max_pooling2d (MaxPooling2D  (None, 128, 128, 32)     0         



 conv2d_1 (Conv2D)           (None, 128, 128, 64)      18496     


 max_pooling2d_1 (MaxPooling  (None, 64, 64, 64)       0         



 conv2d_2 (Conv2D)           (None, 64, 64, 128)       73856     


 max_pooling2d_2 (MaxPooling  (None, 32, 32, 128)      0         



 flatten (Flatten)           (None, 131072)            0         


 dense (Dense)               (None, 256)               33554688  


 batch_normalization (BatchN  (None, 256)              1024      



 dense_1 (Dense)             (None, 128)               32896     


 dropout (Dropout)           (None, 128)               0         


 batch_normalization_1 (Batc  (None, 128)              512       



 dense_2 (Dense)             (None, 3)                 387       



Total params: 33,684,291

Trainable params: 33,683,523

Non-trainable params: 768


From above we can see the change in the shape of the input image after passing through different layers. The CNN model we have developed contains about 33.5 Million parameters. This huge number of parameters and complexity of the model is what helps to achieve a high-performance model which is being used in real-life applications.




    show_shapes = True,

    show_dtype = True,

    show_layer_activations = True



Changes in the shape of the input image.

Changes in the shape of the input image.



    optimizer = 'adam',

    loss = 'categorical_crossentropy',

    metrics = ['accuracy']


While compiling a model we provide these three essential parameters:

  • optimizer – This is the method that helps to optimize the cost function by using gradient descent.
  • loss – The loss function by which we monitor whether the model is improving with training or not.
  • metrics – This helps to evaluate the model by predicting the training and the validation data.


Callbacks are used to check whether the model is improving with each epoch or not. If not then what are the necessary steps to be taken like ReduceLROnPlateau decreases learning rate further. Even then if model performance is not improving then training will be stopped by EarlyStopping. We can also define some custom callbacks to stop training in between if the desired results have been obtained early.


from keras.callbacks import EarlyStopping, ReduceLROnPlateau



class myCallback(tf.keras.callbacks.Callback):

    def on_epoch_end(self, epoch, logs={}):

        if logs.get('val_accuracy') > 0.90:

            print('\n Validation accuracy has reached upto \

                      90% so, stopping further training.')

            self.model.stop_training = True



es = EarlyStopping(patience=3,




lr = ReduceLROnPlateau(monitor='val_loss',




Now we will train our model:


history =, Y_train,

                    validation_data = (X_val, Y_val),

                    batch_size = BATCH_SIZE,

                    epochs = EPOCHS,

                    verbose = 1,

                    callbacks = [es, lr, myCallback()])



Let’s visualize the training and validation accuracy with each epoch.


history_df = pd.DataFrame(history.history)





From the above graphs, we can certainly say that the model has not overfitted the training data as the difference between the training and validation accuracy is very low.

Model Evaluation

Now as we have our model ready let’s evaluate its performance on the validation data using different metrics. For this purpose, we will first predict the class for the validation data using this model and then compare the output with the true labels.


Y_pred = model.predict(X_val)

Y_val = np.argmax(Y_val, axis=1)

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

Let’s draw the confusion metrics and classification report using the predicted labels and the true labels.


metrics.confusion_matrix(Y_val, Y_pred)


Confusion Matrix for the validation data.

Confusion Matrix for the validation data.


print(metrics.classification_report(Y_val, Y_pred,



Classification Report for the Validation Data

Classification Report for the Validation Data


Indeed the performance of our simple CNN model is very good as the f1-score for each class is above 0.90 which means our model’s prediction is correct 90% of the time. This is what we have achieved with a simple CNN model what if we use the Transfer Learning Technique to leverage the pre-trained parameters which have been trained on millions of datasets and for weeks using multiple GPUs? It is highly likely to achieve even better performance on this dataset.