Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • DSA
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps
    • Software and Tools
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Go Premium
  • Data Science
  • Data Science Projects
  • Data Analysis
  • Data Visualization
  • Machine Learning
  • ML Projects
  • Deep Learning
  • NLP
  • Computer Vision
  • Artificial Intelligence
Open In App

Lung Cancer Detection Using Transfer Learning

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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 the Transfer Learning technique 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.

Transfer Learning

In a convolutional neural network, the main task of the convolutional layers is to enhance the important features of an image. If a particular filter is used to identify the straight lines in an image then it will work for other images as well this is particularly what we do in transfer learning. There are models which are developed by researchers by regress hyperparameter tuning and training for weeks on millions of images belonging to 1000 different classes like imagenet dataset. A model that works well for one computer vision task proves to be good for others as well. Because of this reason, we leverage those trained convolutional layers parameters and tuned hyperparameter for our task to obtain higher accuracy.

Importing Libraries

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.
  • OpenCV - This 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.
Python
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 warnings.filterwarnings('ignore') 

Importing Dataset

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.

Python
from zipfile import ZipFile data_path = 'lung-and-colon-cancer-histopa\ thological-images.zip'  with ZipFile(data_path,'r') as zip:   zip.extractall()   print('The data set has been extracted.') 

Output:

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.

Python
path = '/lung_colon_image_set/lung_image_sets' classes = os.listdir(path) classes 

Output:

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

These are the three classes that we have here.

Python
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(Image.open(f'{path}/{cat}/{images[k]}'))         ax[i].imshow(img)         ax[i].axis('off')     plt.show() 

Output:

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.

Python
IMG_SIZE = 256 SPLIT = 0.2 EPOCHS = 10 BATCH_SIZE = 64 

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

Python
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)))     Y.append(i)  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.

Python
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) 

Output:

(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

We will use pre-trained weight for an Inception network which is trained on imagenet dataset. This dataset contains millions of images for around 1000 classes of images.

Model Architecture

We will implement a model using the  Functional API of Keras which will contain the following parts:

  • The base model is the Inception model in this case.
  • The Flatten layer flattens the output of the base model's output.
  • 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. 
Python
from tensorflow.keras.applications.inception_v3 import InceptionV3  pre_trained_model = InceptionV3(     input_shape = (IMG_SIZE, IMG_SIZE, 3),     weights = 'imagenet',     include_top = False ) 

Output:

87916544/87910968 [==============================] - 2s 0us/step

87924736/87910968 [==============================] - 2s 0us/step

Python
len(pre_trained_model.layers) 

Output:

311

This is how deep this model is this also justifies why this model is highly effective in extracting useful features from images which helps us to build classifiers.

The parameters of a model we import are already trained on millions of images and for weeks so, we do not need to train them again.

Python
for layer in pre_trained_model.layers:   layer.trainable = False 

'Mixed7' is one of the layers in the inception network whose outputs we will use to build the classifier.

Python
last_layer = pre_trained_model.get_layer('mixed7') output_shape = last_layer.input[0].shape   print('last layer output shape: ', output_shape)  last_output = last_layer.output  # This code is modified by Susobhan Akhuli 

Output:

last layer output shape:  (None, 14, 14, 768)

Python
x = layers.Flatten()(last_output)  x = layers.Dense(256,activation='relu')(x) x = layers.BatchNormalization()(x)  x = layers.Dense(128,activation='relu')(x) x = layers.Dropout(0.3)(x) x = layers.BatchNormalization()(x)  output = layers.Dense(3, activation='softmax')(x)  model = keras.Model(pre_trained_model.input, output) 

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.
Python
model.compile(     optimizer='adam',     loss='categorical_crossentropy',     metrics=['accuracy'] ) 

Callback

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 the 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.

Python
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,                    monitor = 'val_accuracy',                    restore_best_weights = True)  lr = ReduceLROnPlateau(monitor = 'val_loss',                        patience = 2,                        factor = 0.5,                        verbose = 1) 

Now we will train our model:

Python
history = model.fit(X_train, Y_train,                     validation_data = (X_val, Y_val),                     batch_size = BATCH_SIZE,                     epochs = EPOCHS,                     verbose = 1,                     callbacks = [es, lr, myCallback()]) 

Output:

Model training progress
Model training progress

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

Python
history_df = pd.DataFrame(history.history) history_df.loc[:,['loss','val_loss']].plot() history_df.loc[:,['accuracy','val_accuracy']].plot() plt.show() 

Output:

Graph of loss and accuracy epoch by epoch for training and validation data.loss
Graph of loss and accuracy epoch by epoch for training and validation data.loss

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.

Python
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.

Python
metrics.confusion_matrix(Y_val, Y_pred) 

Output:

Confusion matrix for the validation data
Confusion matrix for the validation data
Python
print(metrics.classification_report(Y_val, Y_pred,                                     target_names=classes)) 

Output:

Classification report for the validation data
Classification report for the validation data

Conclusion: Indeed the performance of our model using the Transfer Learning Technique has achieved higher accuracy without overfitting which is very good as the f1-score for each class is also above 0.90 which means our model's prediction is correct 90% of the time.

Get complete notebook link here :

Notebook: click here.

Dataset: click here.


A

abhishekm482g
Improve
Article Tags :
  • Deep Learning
  • AI-ML-DS
  • Deep Learning Projects
  • AI-ML-DS With Python

Similar Reads

    Deep Learning Tutorial
    Deep Learning is a subset of Artificial Intelligence (AI) that helps machines to learn from large datasets using multi-layered neural networks. It automatically finds patterns and makes predictions and eliminates the need for manual feature extraction. Deep Learning tutorial covers the basics to adv
    5 min read

    Deep Learning Basics

    Introduction to Deep Learning
    Deep Learning is transforming the way machines understand, learn and interact with complex data. Deep learning mimics neural networks of the human brain, it enables computers to autonomously uncover patterns and make informed decisions from vast amounts of unstructured data. How Deep Learning Works?
    7 min read
    Artificial intelligence vs Machine Learning vs Deep Learning
    Nowadays many misconceptions are there related to the words machine learning, deep learning, and artificial intelligence (AI), most people think all these things are the same whenever they hear the word AI, they directly relate that word to machine learning or vice versa, well yes, these things are
    4 min read
    Deep Learning Examples: Practical Applications in Real Life
    Deep learning is a branch of artificial intelligence (AI) that uses algorithms inspired by how the human brain works. It helps computers learn from large amounts of data and make smart decisions. Deep learning is behind many technologies we use every day like voice assistants and medical tools.This
    3 min read
    Challenges in Deep Learning
    Deep learning, a branch of artificial intelligence, uses neural networks to analyze and learn from large datasets. It powers advancements in image recognition, natural language processing, and autonomous systems. Despite its impressive capabilities, deep learning is not without its challenges. It in
    7 min read
    Why Deep Learning is Important
    Deep learning has emerged as one of the most transformative technologies of our time, revolutionizing numerous fields from computer vision to natural language processing. Its significance extends far beyond just improving predictive accuracy; it has reshaped entire industries and opened up new possi
    5 min read

    Neural Networks Basics

    What is a Neural Network?
    Neural networks are machine learning models that mimic the complex functions of the human brain. These models consist of interconnected nodes or neurons that process data, learn patterns and enable tasks such as pattern recognition and decision-making.In this article, we will explore the fundamental
    11 min read
    Types of Neural Networks
    Neural networks are computational models that mimic the way biological neural networks in the human brain process information. They consist of layers of neurons that transform the input data into meaningful outputs through a series of mathematical operations. In this article, we are going to explore
    7 min read
    Layers in Artificial Neural Networks (ANN)
    In Artificial Neural Networks (ANNs), data flows from the input layer to the output layer through one or more hidden layers. Each layer consists of neurons that receive input, process it, and pass the output to the next layer. The layers work together to extract features, transform data, and make pr
    4 min read
    Activation functions in Neural Networks
    While building a neural network, one key decision is selecting the Activation Function for both the hidden layer and the output layer. It is a mathematical function applied to the output of a neuron. It introduces non-linearity into the model, allowing the network to learn and represent complex patt
    8 min read
    Feedforward Neural Network
    Feedforward Neural Network (FNN) is a type of artificial neural network in which information flows in a single direction i.e from the input layer through hidden layers to the output layer without loops or feedback. It is mainly used for pattern recognition tasks like image and speech classification.
    6 min read
    Backpropagation in Neural Network
    Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the model’s predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
    9 min read

    Deep Learning Models

    Convolutional Neural Network (CNN) in Machine Learning
    Convolutional Neural Networks (CNNs) are deep learning models designed to process data with a grid-like topology such as images. They are the foundation for most modern computer vision applications to detect features within visual data.Key Components of a Convolutional Neural NetworkConvolutional La
    6 min read
    Introduction to Recurrent Neural Networks
    Recurrent Neural Networks (RNNs) differ from regular neural networks in how they process information. While standard neural networks pass information in one direction i.e from input to output, RNNs feed information back into the network at each step.Lets understand RNN with a example:Imagine reading
    10 min read
    What is LSTM - Long Short Term Memory?
    Long Short-Term Memory (LSTM) is an enhanced version of the Recurrent Neural Network (RNN) designed by Hochreiter and Schmidhuber. LSTMs can capture long-term dependencies in sequential data making them ideal for tasks like language translation, speech recognition and time series forecasting. Unlike
    5 min read
    Gated Recurrent Unit Networks
    In machine learning Recurrent Neural Networks (RNNs) are essential for tasks involving sequential data such as text, speech and time-series analysis. While traditional RNNs struggle with capturing long-term dependencies due to the vanishing gradient problem architectures like Long Short-Term Memory
    6 min read
    Transformers in Machine Learning
    Transformer is a neural network architecture used for performing machine learning tasks particularly in natural language processing (NLP) and computer vision. In 2017 Vaswani et al. published a paper " Attention is All You Need" in which the transformers architecture was introduced. The article expl
    4 min read
    Autoencoders in Machine Learning
    Autoencoders are a special type of neural networks that learn to compress data into a compact form and then reconstruct it to closely match the original input. They consist of an:Encoder that captures important features by reducing dimensionality.Decoder that rebuilds the data from this compressed r
    8 min read
    Generative Adversarial Network (GAN)
    Generative Adversarial Networks (GAN) help machines to create new, realistic data by learning from existing examples. It is introduced by Ian Goodfellow and his team in 2014 and they have transformed how computers generate images, videos, music and more. Unlike traditional models that only recognize
    12 min read

    Deep Learning Frameworks

    TensorFlow Tutorial
    TensorFlow is an open-source machine-learning framework developed by Google. It is written in Python, making it accessible and easy to understand. It is designed to build and train machine learning (ML) and deep learning models. It is highly scalable for both research and production.It supports CPUs
    2 min read
    Keras Tutorial
    Keras high-level neural networks APIs that provide easy and efficient design and training of deep learning models. It is built on top of powerful frameworks like TensorFlow, making it both highly flexible and accessible. Keras has a simple and user-friendly interface, making it ideal for both beginn
    3 min read
    PyTorch Tutorial
    PyTorch is an open-source deep learning framework designed to simplify the process of building neural networks and machine learning models. With its dynamic computation graph, PyTorch allows developers to modify the network’s behavior in real-time, making it an excellent choice for both beginners an
    7 min read
    Caffe : Deep Learning Framework
    Caffe (Convolutional Architecture for Fast Feature Embedding) is an open-source deep learning framework developed by the Berkeley Vision and Learning Center (BVLC) to assist developers in creating, training, testing, and deploying deep neural networks. It provides a valuable medium for enhancing com
    8 min read
    Apache MXNet: The Scalable and Flexible Deep Learning Framework
    In the ever-evolving landscape of artificial intelligence and deep learning, selecting the right framework for building and deploying models is crucial for performance, scalability, and ease of development. Apache MXNet, an open-source deep learning framework, stands out by offering flexibility, sca
    6 min read
    Theano in Python
    Theano is a Python library that allows us to evaluate mathematical operations including multi-dimensional arrays efficiently. It is mostly used in building Deep Learning Projects. Theano works way faster on the Graphics Processing Unit (GPU) rather than on the CPU. This article will help you to unde
    4 min read

    Model Evaluation

    Gradient Descent Algorithm in Machine Learning
    Gradient descent is the backbone of the learning process for various algorithms, including linear regression, logistic regression, support vector machines, and neural networks which serves as a fundamental optimization technique to minimize the cost function of a model by iteratively adjusting the m
    15+ min read
    Momentum-based Gradient Optimizer - ML
    Momentum-based gradient optimizers are used to optimize the training of machine learning models. They are more advanced than the classic gradient descent method and helps to accelerate the training process especially for large-scale datasets and deep neural networks.By incorporating a "momentum" ter
    4 min read
    Adagrad Optimizer in Deep Learning
    Adagrad is an abbreviation for Adaptive Gradient Algorithm. It is an adaptive learning rate optimization algorithm used for training deep learning models. It is particularly effective for sparse data or scenarios where features exhibit a large variation in magnitude.Adagrad adjusts the learning rate
    6 min read
    RMSProp Optimizer in Deep Learning
    RMSProp (Root Mean Square Propagation) is an adaptive learning rate optimization algorithm designed to improve the performance and speed of training deep learning models.It is a variant of the gradient descent algorithm which adapts the learning rate for each parameter individually by considering th
    5 min read
    What is Adam Optimizer?
    Adam (Adaptive Moment Estimation) optimizer combines the advantages of Momentum and RMSprop techniques to adjust learning rates during training. It works well with large datasets and complex models because it uses memory efficiently and adapts the learning rate for each parameter automatically.How D
    4 min read

    Deep Learning Projects

    Lung Cancer Detection using Convolutional Neural Network (CNN)
    Computer Vision is one of the applications of deep neural networks and one such use case is in predicting the presence of cancerous cells. In this article, we will learn how to build a classifier using Convolution Neural Network which can classify normal lung tissues from cancerous tissues.The follo
    7 min read
    Cat & Dog Classification using Convolutional Neural Network in Python
    Convolutional Neural Networks (CNNs) are a type of deep learning model specifically designed for processing images. Unlike traditional neural networks CNNs uses convolutional layers to automatically and efficiently extract features such as edges, textures and patterns from images. This makes them hi
    5 min read
    Sentiment Analysis with an Recurrent Neural Networks (RNN)
    Recurrent Neural Networks (RNNs) are used in sequence tasks such as sentiment analysis due to their ability to capture context from sequential data. In this article we will be apply RNNs to analyze the sentiment of customer reviews from Swiggy food delivery platform. The goal is to classify reviews
    5 min read
    Text Generation using Recurrent Long Short Term Memory Network
    LSTMs are a type of neural network that are well-suited for tasks involving sequential data such as text generation. They are particularly useful because they can remember long-term dependencies in the data which is crucial when dealing with text that often has context that spans over multiple words
    4 min read
    Machine Translation with Transformer in Python
    Machine translation means converting text from one language into another. Tools like Google Translate use this technology. Many translation systems use transformer models which are good at understanding the meaning of sentences. In this article, we will see how to fine-tune a Transformer model from
    6 min read
    Deep Learning Interview Questions
    Deep learning is a part of machine learning that is based on the artificial neural network with multiple layers to learn from and make predictions on data. An artificial neural network is based on the structure and working of the Biological neuron which is found in the brain. Deep Learning Interview
    15+ min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Campus Training Program
  • Explore
  • POTD
  • Job-A-Thon
  • Community
  • Videos
  • Blogs
  • Nation Skill Up
  • Tutorials
  • Programming Languages
  • DSA
  • Web Technology
  • AI, ML & Data Science
  • DevOps
  • CS Core Subjects
  • Interview Preparation
  • GATE
  • Software and Tools
  • Courses
  • IBM Certification
  • DSA and Placements
  • Web Development
  • Programming Languages
  • DevOps & Cloud
  • GATE
  • Trending Technologies
  • Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
  • Preparation Corner
  • Aptitude
  • Puzzles
  • GfG 160
  • DSA 360
  • System Design
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences