Classifying blood cell images with Tensorflow

Berkay Kullukçu
CodeX
Published in
2 min readSep 1, 2022

Hello everyone! In this blog, we will learn how to write a script in Python which can classify blood sample images we take from a Kaggle dataset.

Photo by National Cancer Institute on Unsplash

Identifying and categorizing patient blood samples is a standard part of diagnosing blood-based disorders. Methods for detecting and classifying automated blood cell subtypes have substantial medicinal implications.

We will use the dataset taken from:

12,500 augmented pictures of blood cells (JPEG) with cell type labels are included in this dataset (CSV). There are around 3,000 photos for each of the four cell types, which are organized into four folders (according to cell type). Eosinophils, lymphocytes, monocytes, and neutrophils are the four cell types.

Kaggle, Blood Cell Images dataset

First, we will start with importing the required libraries.

We will print our figures in a 20x20 frame to see the pictures beforehand. From the dataset, we input the folder of the training data to the script and store the value in a variable called test_folder. The for loop will show us five different pictures from the training set.

Here we declare our image width and height and use the function to create two arrays of data, consisting of information about the filenames in the input directories. The function is also used for changing the image types into arrays of float32 and normalizing.

We map different types of blood cells using a dictionary.

{'EOSINOPHIL': 0, 'LYMPHOCYTE': 1, 'MONOCYTE': 2, 'NEUTROPHIL': 3}

We train our Tensorflow Keras model to find the desired figures from the dataset.

We will fit the model using a predefined number of epochs. I used five epochs for this example. You can increase the number of your epochs for increased accuracy.

Here is the entire code.

Conclusion

Finally! Using the approach above, we can write a script in Python for image classification. Thank you for reading, and I’ll keep adding new instructions and examples. For other articles and updates, connect with me on LinkedIn. You can also follow me on GitHub.

References

--

--