• Home
  • Blog
  • Our Services
  • Contact Us
  • Register
Have any question?

(+91) 844 745 8168
[email protected]”>
RegisterLogin
AI Next Generation
  • Home
  • Blog
  • Our Services
  • Contact Us
  • Register

Deep Learning

  • Home
  • Blog
  • Deep Learning
  • Brain Tumor Prediction PyTorch[CNN]

Brain Tumor Prediction PyTorch[CNN]

  • Posted by Pranavi Kosaraju
  • Date December 8, 2021
  • Comments 0 comment

Introduction

This article is a Brain Tumor Prediction based on x-ray images by creating a Convolutional Neural Network. This article teaches you advanced topics of Deep Learning i.e, creation of convolutional layers, feature extraction, pooling, etc. If the terms sound new to you, then please go through the basics of CNN before trying to understand the code. I highly recommend watching the videos listed down below for a better understanding of the concepts.

Deep Learning: https://ainewgeneration.com/category/deep-learning/

Pytorch for Deep Learning: https://www.youtube.com/watch?v=Lkj_Jt9vwkc&t=16s

How to build a CNN Model: https://www.youtube.com/watch?v=_gHI3znSga8

Table of Contents

  1. Introduction
  2. About the Brain Tumor Prediction Dataset
  3. Importing the Libraries
  4. Loading the Dataset
  5. Visualizing the Dataset
  6. Creating the Model
  7. Training the Model
  8. Plotting the Accuracy and Loss
  9. Visualizing the Results
  10. Conclusion

About the Brain Tumor Prediction Dataset

The dataset comprises 253 brain x-rays with and without brain tumors. Our target is to build a model that can predict brain tumors based on the data.

You can download the dataset at: https://drive.google.com/drive/folders/10X-9JRWSbf5zQhqxi0Ft8JOR_hwtxsFh?usp=sharing

Here is a sample image of how our data look like:

Importing Libraries

Here is the list of libraries will be using for our model. We shall go through each of them while we use them. So for now, import these libraries:

import matplotlib.pyplot as plt

import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, random_split

import torchvision
import torchvision.transforms as transforms
from torchvision.datasets import ImageFolder
from torchvision.utils import make_grid

Loading the Dataset

The dataset we are using comprises two different folders. The names of these folders are the target variables of our data. To be brief, our data is segregated into two folders by the target values. To load such kind of data into our workspace, we use the library ImageFolder. Before we get to use that, we need to define the data path. Make sure you do not copy the code below. Please give the right data path to your computer.

data_path = "/Users/pranavi/Downloads/brain_tumor_dataset/"

First, we need to make sure that all our images are of the same size. Here is how to do it:

img_size = 120

Once we defined the image size we wanted, it’s time to transform the images

img_transform = transforms.Compose([
transforms.Resize((img_size, img_size)),       transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485,0.456,0.406], std=[0.229,0.224,0.225])])
  1. We resize the images into our desired shape i.e, 120*120
  2. RandomHorizontalFlip is a part of Data Augmentation. This creates new set of images by flipping which ultimately turns our model smarter.
  3. ToTensor(): transforms our images into tensor form
  4. We will have to normalize our images by giving the said mean and standard deviation values.
img_data = ImageFolder(root=data_path, 
              transform= img_transform )
input: img_data.class_to_idx
output: {'no': 0, 'yes': 1}
input: len(img_data)
output: 253

We have our entire image data as img_data we will need to split it into training, testing, and validation datasets but since we have small data, we will only be splitting it into training and testing.

train_data, test_data = random_split(img_data,[190,63])

The next step is to divide the data into batches by using DataLoader. This will simplify the computational process.

train_loader = DataLoader(train_data, batch_size=16, shuffle=True)
test_loader = DataLoader(test_data, batch_size=16, shuffle=False)

Let us now look at the shape of our images

for images, labels in train_loader:
    print(images.shape, labels.shape)
    break

Visualizing the Dataset

To visualize the images in our dataset, we use matplotlib. pyplot and make_grid to visualize our images in a row. Let us create a function so that we can use it for both training and testing data.

def show_img_batch(data):
    for images, labels in data:
        plt.figure(figsize=(20,15))
        plt.imshow(make_grid
        (images,nrow=16).permute(1,2,0))
        plt.show()
        break
input: show_img_batch(train_loader)
output:
input: show_img_batch(test_loader)
output:

Creating the Model

It is time to create our CNN model. For our data, I have used 3 convolutional layers and 3 fully connected layers. To determine the image size in the fc1, I have used n-f+2p/s +1 formula where n = image size, f = number of kernels, p = padding, s = stride for every convolutional layer.

class CNN_seq(nn.Module):
    def __init__(self, kernel_size=3,out_channels1=16, out_channels2=32, out_channels3=64 ):
        super(CNN_seq, self).__init__()
        
        self.features = nn.Sequential(
                        nn.Conv2d(in_channels=3,
                        out_channels=out_channels1, 
                        kernel_size=kernel_size),
                        nn.ReLU(),
                        nn.MaxPool2d(2),
                        nn.Conv2d(in_channels=
                        out_channels1, 
                        out_channels=out_channels2, 
                        kernel_size=kernel_size),
                        nn.ReLU(),
                        nn.MaxPool2d(2),
                        nn.Conv2d(in_channels=
                        out_channels2, 
                        out_channels=out_channels3,
                        kernel_size=kernel_size),
                        nn.ReLU(),
                        nn.MaxPool2d(2))
        
        self.classifier = nn.Sequential(
                          nn.Linear(13*13*64, 256),
                          nn.ReLU(),
                          nn.Dropout(0.1),
                          nn.Linear(256,128),
                          nn.ReLU(),
                          nn.Dropout(0.1),
                          nn.Linear(128, 2))
        
    def forward(self,x):
        x = self.features(x)
        x = x.view(-1, 13*13*64)
        x = self.classifier(x)
        
        return x
        
input: model = CNN_seq()
print(model.parameters)
output: 
<bound method Module.parameters of CNN_seq(
  (features): Sequential(
    (0): Conv2d(3, 16, kernel_size=(3, 3), stride=(1, 1))
    (1): ReLU()
    (2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (3): Conv2d(16, 32, kernel_size=(3, 3), stride=(1, 1))
    (4): ReLU()
    (5): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (6): Conv2d(32, 64, kernel_size=(3, 3), stride=(1, 1))
    (7): ReLU()
    (8): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  )
  (classifier): Sequential(
    (0): Linear(in_features=10816, out_features=256, bias=True)
    (1): ReLU()
    (2): Dropout(p=0.1, inplace=False)
    (3): Linear(in_features=256, out_features=128, bias=True)
    (4): ReLU()
    (5): Dropout(p=0.1, inplace=False)
    (6): Linear(in_features=128, out_features=2, bias=True)
  )
)>
loss_fn = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

Training the Model

Now that we have our model created, we now write a function that will train, test, and plot the data. I have iterated the data 10 times and we are using Adam as our optimizer.

input:
def CNN_train(loss_fn, optimizer):

    epochs = 10

    training_acc = []
    training_loss = []
    testing_acc = []
    testing_loss = []

    for epoch in range(epochs):

        train_acc = 0.0
        train_loss = 0.0

        model.train()

        for images, labels in train_loader:
            optimizer.zero_grad()
            output = model(images)
            loss = loss_fn(output, labels)

            loss.backward()
            optimizer.step()

            predictions = torch.argmax(output, 1)

            train_acc += (predictions == labels).sum().item()
            train_loss += loss.item()

        training_acc.append(train_acc/len(train_data))
        training_loss.append(train_loss/len(train_loader))

        model.eval()

        test_acc = 0.0
        test_loss = 0.0

        with torch.no_grad():
            for images, labels in test_loader:
                output = model(images)
                loss = loss_fn(output, labels)

                predictions = torch.argmax(output ,1)

                test_acc += (predictions == labels).sum().item()
                test_loss += loss.item()

            testing_acc.append(test_acc/len(test_data))
            testing_loss.append(test_loss/len(test_loader))

        print("Epochs:{},Training Accuracy:{:.2f},Training Loss:{:.2f},Testing Accuracy:{:.2f},Testing Loss:{:.2f}".
              format(epoch+1,train_acc/len(train_data), train_loss/len(train_loader), test_acc/len(test_data),
        test_loss/len(test_loader)))
output:
Epochs:1,Training Accuracy:0.66,Training Loss:0.65,Testing Accuracy:0.83,Testing Loss:0.56
Epochs:2,Training Accuracy:0.79,Training Loss:0.50,Testing Accuracy:0.81,Testing Loss:0.43
Epochs:3,Training Accuracy:0.77,Training Loss:0.49,Testing Accuracy:0.79,Testing Loss:0.46
Epochs:4,Training Accuracy:0.81,Training Loss:0.44,Testing Accuracy:0.83,Testing Loss:0.42
Epochs:5,Training Accuracy:0.83,Training Loss:0.40,Testing Accuracy:0.83,Testing Loss:0.46
Epochs:6,Training Accuracy:0.92,Training Loss:0.26,Testing Accuracy:0.81,Testing Loss:0.56
Epochs:7,Training Accuracy:0.94,Training Loss:0.17,Testing Accuracy:0.83,Testing Loss:0.61
Epochs:8,Training Accuracy:0.91,Training Loss:0.24,Testing Accuracy:0.84,Testing Loss:0.48
Epochs:9,Training Accuracy:0.91,Training Loss:0.18,Testing Accuracy:0.86,Testing Loss:0.53
Epochs:10,Training Accuracy:0.96,Training Loss:0.14,Testing Accuracy:0.84,Testing Loss:0.64

Plotting the Accuracy and Loss

 plt.title("Accuracy Vs Epochs")
 plt.plot(range(epochs), training_acc, 
    label="Training Accuracy")
 plt.plot(range(epochs), testing_loss, 
    label="Testing Accuracy")
 plt.legend()
 plt.xlabel("Accuracy")
 plt.ylabel("Epochs")
 plt.show()
plt.title("Loss Vs Epochs")
plt.plot(range(epochs), training_loss, label="Training Loss")
plt.plot(range(epochs), testing_loss, label ="Testing Loss")
plt.legend()
plt.xlabel("Loss")
plt.ylabel("Epochs")
plt.show()

Visualizing the Results

We can visualize the labels of our model’s predictions with the actual predictions by creating a function.

def predict_image(img, labels):
    x = img.unsqueeze(0)
    y = model(x)
    
    pred = torch.argmax(y, dim=1)
    
    return img_data.classes[pred]
input: img, label = test_data[10]
plt.imshow(img.permute(1,2,0))
print("Actual Label:" ,img_data.classes[label], "Predicted Label:", predict_image(img, model))
output:
Actual Label: yes Predicted Label: yes
input: img, label = test_data[44]
plt.imshow(img.permute(1,2,0))
print("Actual Label:" ,img_data.classes[label], "Predicted Label:", predict_image(img, model))
output:

Conclusion

As you can see, our model has given 84% in the 10th iteration. Try tuning the parameters, changing the structure of our model to achieve more accuracy. If you wish to see a video explanation of this dataset, you can watch it at https://www.youtube.com/watch?v=SaMlE9wzcbo

Tag:deep learning, machine learning

  • Share:
author avatar
Pranavi Kosaraju

Previous post

Covid-19 X-ray prediction using Deep Learning
December 8, 2021

Next post

Garbage Classification using CNN Model
December 12, 2021

You may also like

solid-waste-management-3-1200×900
Garbage Classification using CNN Model
12 December, 2021
lung-cancer-image-605×340
Covid-19 X-ray prediction using Deep Learning
6 December, 2021
V6uxcpY(1)
Computer Vision in Deep Learning
26 August, 2021

Leave A Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Garbage Classification using CNN Model
  • Brain Tumor Prediction PyTorch[CNN]
  • Covid-19 X-ray prediction using Deep Learning
  • Data Analysis for Olympic 2021
  • Naive Bayes in Machine Learning

Categories

  • Data Science
  • Deep Learning
  • Machine Learning
  • Python

Archives

  • December 2021
  • November 2021
  • September 2021
  • August 2021
  • July 2021

(+91) 844 745 8168

[email protected]

COMPANY

  • Blog
  • Our Services
  • Contact Us

LINKS

  • Home
  • Blog
  • Activity
  • Checkout

RECOMMEND

  • Cart
  • Members
  • Sample Page
  • Shop

SUPPORT

  • Members
  • My account
  • Register
  • Shop

Copyright © 2021 AI New Generation

Become an instructor?

Join thousand of instructors and earn money hassle free!

Get started now

Login with your site account

Lost your password?

Not a member yet? Register now

Register a new account

Are you a member? Login now