Last Updated on 08/10/2025 by Eran Feit
This tutorial provides a step-by-step easy guide on how to implement and train a CNN model for Malaria cell classification using TensorFlow and Keras.
🔍 What You’ll Learn 🔍:
Data Preparation — In this part, you’ll download the dataset and prepare the data for training. This involves tasks like preparing the data , splitting into training and testing sets, and data augmentation if necessary.
CNN Model Building and Training — In part two, you’ll focus on building a Convolutional Neural Network (CNN) model for the binary classification of malaria cells. This includes model customization, defining layers, and training the model using the prepared data.
Model Testing and Prediction — The final part involves testing the trained model using a fresh image that it has never seen before. You’ll load the saved model and use it to make predictions on this new image to determine whether it’s infected or not.
Check out our tutorial here : https://youtu.be/WlPuW3GGpQo?si=xMGGR-Sj91u3w2dn
Link for the full code : https://ko-fi.com/s/98d022c834
You can find more tutorials, and join my newsletter here : https://eranfeit.net/
Here is the code for Convolutional neural network – Malaria Cells Classification :
Dataset name – Malaria cell images dataset : https://www.kaggle.com/datasets/iarunava/cell-images-for-detecting-malaria
Part 1 : Malaria Cell Image Preprocessing and Dataset Preparation :
This Python script processes images from a malaria cell classification dataset, resizing them for machine learning tasks.
It reads parasitized and uninfected cell images, assigns labels (0 for parasitized, 1 for uninfected), and saves the processed data as NumPy arrays for further analysis or model training.
# Import necessary libraries
import numpy as np # For handling numerical data and arrays
import pandas as pd # (Not used in the script but generally used for data handling)
import cv2 # OpenCV for image processing
import glob # For handling file paths and reading multiple images
# Lists to store processed images and their corresponding labels
allImages = []
allLables = []
# Define the target shape for resizing images
input_shape = (124, 124)
# Define the paths to the dataset (Parasitized and Uninfected cell images)
ParasitedPath = "E:/Data-sets/Malaria Cell Classification/cell_images/Parasitized"
UninfectedPath = "E:/Data-sets/Malaria Cell Classification/cell_images/Uninfected"
# List containing both paths
paths = [ParasitedPath, UninfectedPath]
# Loop through both directories
for path in paths:
path2 = path + "/*.png" # Define the file search pattern (all PNG images)
# Loop through all image files in the current directory
for file in glob.glob(path2):
print(file) # Print the file path (for debugging or tracking progress)
# Load the image using OpenCV
img = cv2.imread(file)
# If the image is successfully loaded, process it
if img is not None:
# Resize the image to the specified input shape
resized = cv2.resize(img, input_shape, interpolation=cv2.INTER_AREA)
# Add the resized image to the list
allImages.append(resized)
# Assign labels: 0 for parasitized, 1 for uninfected
if path == ParasitedPath:
allLables.append(0)
else: # If from the uninfected directory
allLables.append(1)
# Convert the lists to NumPy arrays for efficient storage and processing
allImagesNP = np.array(allImages)
print(allImagesNP.shape) # Print the shape of the processed image dataset
allLablesNP = np.array(allLables)
print(allLablesNP.shape) # Print the shape of the labels array
# Save the processed images and labels as .npy files for future use
print("Save the data")
np.save("e:/temp/Malaria-images.npy", allImagesNP)
np.save("e:/temp/Malaria-lables.npy", allLablesNP)
print("Finish save the data ....")Link for the full code : https://ko-fi.com/s/98d022c834
Part 2 : Training a CNN for Malaria Cell Classification :
This script loads a preprocessed malaria dataset, normalizes image pixel values, splits the data into training and testing sets, and trains a Convolutional Neural Network (CNN) to classify parasitized and uninfected cells. The best-performing model is saved for future use.
# Import necessary libraries
import numpy as np # For numerical operations
import cv2 # For image processing
# Load the saved dataset (preprocessed images and labels)
allImages = np.load("e:/temp/Malaria-images.npy")
allLables = np.load("e:/temp/Malaria-lables.npy")
# Print the shapes of the loaded datasets to verify dimensions
print(allImages.shape) # Shape of the images dataset
print(allLables.shape) # Shape of the labels dataset
# Define the expected input shape for the model
input_shape = (124, 124, 3) # Image dimensions with 3 color channels (RGB)
shape = (124, 124) # 2D shape for image display
# Display the first image in the dataset
img = allImages[0] # Extract the first image
label = allLables[0] # Get its corresponding label
print(label) # Print the label (0 = parasitized, 1 = uninfected)
# Show the image using OpenCV
cv2.imshow("img", img)
cv2.waitKey(0) # Wait for a key press
cv2.destroyAllWindows() # Close the image window
# Normalize image pixel values to be between 0 and 1 for better training performance
allImagesForModel = allImages / 255.0
# Split the dataset into training and testing sets (70% training, 30% testing)
from sklearn.model_selection import train_test_split
print("Splitting data into training and testing sets...")
X_train, X_test, y_train, y_test = train_test_split(allImagesForModel, allLables, test_size=0.3, random_state=42)
# Print shapes of the train and test datasets
print("X_train, X_test, y_train, y_test ----->>> shapes:")
print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)
# Import necessary Keras libraries to define and train the CNN model
from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPooling2D, Flatten
from keras.callbacks import ModelCheckpoint
# Define a Convolutional Neural Network (CNN) model
model = Sequential()
# First convolutional layer with 16 filters, kernel size (3x3), and ReLU activation
model.add(Conv2D(input_shape=input_shape, filters=16, kernel_size=(3,3), padding="same", activation="relu"))
model.add(Conv2D(filters=16, kernel_size=(3,3), padding="same", activation="relu"))
model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2))) # Max pooling to reduce spatial dimensions
# Second convolutional block with 32 filters
model.add(Conv2D(filters=32, kernel_size=(3,3), padding="same", activation="relu"))
model.add(Conv2D(filters=32, kernel_size=(3,3), padding="same", activation="relu"))
model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2))) # Max pooling again
# Flatten the feature maps into a 1D vector for the dense layers
model.add(Flatten())
# Fully connected dense layers
model.add(Dense(1024, activation="relu")) # Hidden layer with 1024 neurons
model.add(Dense(1, activation="sigmoid")) # Output layer (sigmoid activation for binary classification)
# Compile the model with Adam optimizer and binary cross-entropy loss (for binary classification)
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Print the model summary to see the architecture
print(model.summary())
# Define batch size and number of epochs for training
batch = 32
epochs = 10
# Calculate steps per epoch for training and validation
stepsPerEpoch = np.ceil(len(X_train) / batch)
validationSteps = np.ceil(len(X_test) / batch)
# Define the path to save the best model based on validation accuracy
best_model_file = "e:/temp/Malaria_binary.h5"
# Create a ModelCheckpoint callback to save the best model during training
best_model = ModelCheckpoint(best_model_file, monitor="val_accuracy", verbose=1, save_best_only=True)
# Train the model
history = model.fit(
X_train, y_train,
batch_size=batch,
epochs=epochs,
verbose=1,
validation_data=(X_test, y_test),
validation_steps=validationSteps,
steps_per_epoch=stepsPerEpoch,
shuffle=True, # Shuffle training data to improve generalization
callbacks=[best_model] # Save the best model during training
)Link for the full code : https://ko-fi.com/s/98d022c834
Part3 : Loading a Trained Model and run inference on a test image
This script loads a pre-trained Convolutional Neural Network (CNN) model for malaria cell classification. It processes a test image, makes a prediction, and displays the image with the classification result (infected or uninfected).

# Import necessary libraries
import tensorflow as tf # Deep learning framework
import os # File system operations
import cv2 # OpenCV for image processing
import numpy as np # Numerical computations
# Load the trained model from the saved file
best_model_file = "e:/temp/Malaria_binary.h5"
model = tf.keras.models.load_model(best_model_file)
# Print model summary to check the architecture
print(model.summary())
# Define input image size
input_shape = (124, 124)
# Define class labels
categories = ["infected", "uninfected"]
# Function to preprocess an image before feeding it into the model
def prepareImage(img):
# Resize the image to match the input size expected by the model
resized = cv2.resize(img, input_shape, interpolation=cv2.INTER_AREA)
# Expand dimensions to match the input shape expected by TensorFlow (batch size, height, width, channels)
imgResult = np.expand_dims(resized, axis=0)
# Normalize pixel values to the range [0,1]
imgResult = imgResult / 255.0
return imgResult
# Load a test image from the specified path
testImagePath = "TensorFlowProjects/Malaria Cell Classification/testInfected.jpg"
img = cv2.imread(testImagePath) # Read the image using OpenCV
# Prepare the image for prediction
imgForModel = prepareImage(img)
# Run prediction using the trained model
result = model.predict(imgForModel, verbose=1)
print(result) # Print raw prediction output
# Convert the prediction into a binary classification (0 = infected, 1 = uninfected)
if result > 0.5:
result = 1
else:
result = 0
# Print the final classification result
print(result)
# Assign the corresponding category label
text = categories[result]
# Reload the image to display the classification result
img = cv2.imread(testImagePath)
# Define font for text overlay
font = cv2.FONT_HERSHEY_COMPLEX
# Overlay the predicted label on the image
cv2.putText(img, text, (0, 20), font, 1, (0, 255, 255), 2) # Yellow text
# Display the image with classification result
cv2.imshow("img", img)
cv2.waitKey(0) # Wait for a key press before closing the image windowLink for the full code : https://ko-fi.com/s/98d022c834
Connect :
☕ Buy me a coffee — https://ko-fi.com/eranfeit
🖥️ Email : feitgemel@gmail.com
🤝 Fiverr : https://www.fiverr.com/s/mB3Pbb
Planning a trip and want ideas you can copy fast?
Here are three detailed guides from our travels:
• 5-Day Ireland Itinerary: Cliffs, Castles, Pubs & Wild Atlantic Views
https://eranfeit.net/unforgettable-trip-to-ireland-full-itinerary/
• My Kraków Travel Guide: Best Places to Eat, Stay & Explore
https://eranfeit.net/my-krakow-travel-guide-best-places-to-eat-stay-explore/
• Northern Greece: Athens, Meteora, Tzoumerka, Ioannina & Nafpaktos (7 Days)
https://eranfeit.net/my-amazing-trip-to-greece/
Each guide includes maps, practical tips, and family-friendly stops—so you can plan in minutes, not hours.
Enjoy,
Eran
