Building an algorithm to detect different types of Intracranial Brain Hemorrhage using deep…

Source: Deep Learning on Medium

Let’s us understand What is Intracranial hemorrhage

Intracranial hemorrhage is a types of bleeding that occurs in skull.It can be caused due to many factor like ranging from trauma, stroke, aneurysm, vascular malformations, high blood pressure, illicit drugs and blood clotting disorders.

Intracranial hemorrhage is a relatively common condition that has many causes ranging from trauma, stroke, aneurysm, vascular malformations, high blood pressure, illicit drugs and blood clotting disorders.

There are different sub-types of Intracranial hemorrhage are:

The data-set that we are going to use is taken from Kaggle competition . In this data-set we are going to build an algorithm to detect different sub-types of Intracranial hemorrhage.


  • Basic programming knowledge

Although Python is highly involved in this project, it is not required to have a deep knowledge in the language, as long as you have basic programming knowledge you can understand very easily.

For this program, we will need Python to be installed on the computer. We will be using the libraries like numpy, pandas, seaborn, matplotlib, scipy, pydicom( it is used to read a radiotherapy plan), keras, open-cv, and sklearn.

Process Description

Section A: Loading the data-set

Section B: Image Processing

Section C: Applying deep learning model

Section D: Make predictions on testing set

Section A: Loading the data-set

As our task of detecting sub-types of hemorrhage is one that focuses heavily on image data, one would expect there to be a lot of image processing. This is definitely correct.

I chose to start with loading the data-set with the help of pandas library. Here is a bit of an overview of what we are about to do:

import pandas as pd

data_set=pd.read_csv(‘ data_set.csv’)

Step A.1: Understand the data

Now we look at the top 5 column of the data-set by using head function

Now we split the data-set into 3 column.The new columns are label,class and filename.

Section B: Image Processing

Note that so far, we haven’t written a lot of code. This is attributed to the beauty of Python’s succinctness in syntax as well as the use of external program-ready libraries, such as open-CV.

The next step is to perform image processing in which where we rescale the image and resize the image and save it into the png form. Lets us do one by one all of these things

Now we define window_img function that is used for localizing the image exactly where in an image object resides, window_img function is taking an image and rescaling into the normal form by using the formula of min-max scaler.

In window_img function we are passing parameters like img, window_center,window_width, intercept to do that task.

The second function i.e get_first_of_dicom_field_as_int is used to check the type of x is equal to pydicom.multival.MultiValue then return the first index of x otherwise return integer part of x.

Get windowing function is used for getting the window center, window width values of the data and also getting the the intercept and slope values.

Now we are converting the images from dcm format into png format.And now we are using dcmread function to read the path and store it in dcm variable. After that we call the get windowing function and passing the dcm variable. After that we call the window_img function and store it in img and resizing the image and save it in directory.

Section C: Applying deep learning model

Now we defining our own data generator function having validation split of 0.15 . Now we define three more generator for training, testing and validation. And then call the functions

The next step is to train the model but before that we have to define a function called focal loss that is used only when the data is unbalanced.

In this above function we are computing the focal loss value for multi-label columns. The formula for focal loss is:

fl= -alpha * (z-p)^gamma * log(p) - (1-alpha) *p^gamma *log(1-p)
where alpha=0.25, gamma=2, p=sigmoid(x), z=target_tensor

Now we initialize the densenet class

The upcoming step is to define our neural network architecture using Keras. Here we will be using a network with densenet layer.

Now we compile our model using the Adam optimizer with “categorical_crossentropy” as the loss function.

Now create the checkpoint and save it in a model.h5 format.

Now that our Keras model is compiled, we can “fit” it on our training data.

Section D: Make Predictions on testing set

Now we save the model weight in a h5 format and we are now ready to make the prediction on testing set