Blood Cell Classification Using Deep Learning
Welcome to our Blood Cell Classification project! Do you wish to learn how the machines get to distinguish between the various types of blood cells? In this project, we immerse ourselves in an AI-based approach that does just that: using some of the most potent machine-learning tools to interpret sophisticated cell images. It has brought the prospect of quicker, more accurate diagnosis a little bit closer.
Overview
In this project, we build deep learning models to be able to categorize blood cells from image data. There are several blood cells which include red blood cells, white blood cells, and platelets and each has its function in our body. Identifying these cells may not be easy and that’s where deep learning for image classification comes into the picture. Through training the model on different types of cell images we train it to correctly recognize each cell type.
In this project, we will discuss Data Pre-Processing, Selection of a Model, Training the Model, and assessing the performance. It will also demonstrate how it is possible to generalize it to real medical analysis where precision is necessary. So at the end of the project, one will understand how to employ machine learning to make blood cell analysis smarter, faster, and efficient.
Prerequisites
Before we jump into the code, here’s what you’ll need:
- An understanding of Python programming and usage of Google Colab
- Basic knowledge about deep learning and medical images.
- Comfortable using frameworks like Tensorflow, Keras, Numpy, OpenCV, and Seaborn to handle data and build models and visualize data and performance of models
- Blood cell dataset.
Once you organize these tools, you will notice how almost all of them can be used in the following step. Also, do not stress if you are not a Python master—through the tutorial, you will understand every line of the code!
Approach
In this Blood Cell Classification project, first, we collected the dataset from Kaggle. Then we load a labeled dataset of blood cell images, each tagged with its respective cell type. After exploring the dataset, we preprocess the images by using resizing, normalization, and augmentation techniques to improve model performance. Then we build three different deep-learning models to classify the blood cells from images.
After training the model, we evaluate the model performance using different techniques like precision, recall, and confusion matrix to ensure that models work perfectly on unseen data
Finally, we test the model on new images to confirm its ability to classify unseen samples accurately, showcasing the model’s real-world potential in medical diagnostics.
Workflow and Methodology
This project can be divided into the following basic steps:
- Data Collection: We collected the blood cell dataset labeled with different cell types from Kaggle.
- Data preprocess: To improve the model performance and achieve higher accuracy, we applied different preprocessing techniques. First, we augmented the dataset to create a balanced dataset. Then we resized and normalized the images in 0 to 1 pixel values.
- Model Selection: In this project, there are three models used (Custom CNN, EfficientNetB4, and VGG16).
- Training and Testing: Each of the Models has been trained on the preprocessed dataset and later, tested on the dataset that was not used during training.
- Model Evaluation: The evaluation of the model's performance is done by evaluating accuracy, precision, recall, confusion matrix, etc.
- Prediction and Testing: Test the models on new images to confirm their effectiveness in classifying unseen samples accurately.
The methodology includes
Data Preprocessing: The images are resized, normalized, and augmented to improve the performance of models based on them.
Model Training: Each model is trained with 100 epochs to enhance the level of performance.
Evaluation: Standard metrics (accuracy, working of confusion matrix) are applied to assess the efficiency of the models.
Dataset Collection
The project sourced a dataset from Kaggle. This is a popular repository of various datasets for machine learning projects. This dataset consists of images of blood cells. In this dataset, each image is labeled with a specific blood cell type. Each image is tagged with the respective cell type, which is quite critical in supervised learning. This assists our model in learning the individual characteristics of each cell type, which is essential for accurate classification as a result.
Data Preparation
The dataset was pre-processed by resizing the images to a size of 128 * 128 pixels and scaling the pixels to the range 0 to 255. To increase the variability of the dataset, primarily data augmentation techniques were applied.
Data Preparation Workflow
- Load Dataset from Google Drive
- Rotation, flipping, and changes in contrast, among others, are employed to increase the diversity of the datasets.
- Process and Resize as per Standards used in the model. This helps to standardize the input of the models.
- Further, the collected dataset has to be split into training and validation sets.
Code Explanation
STEP 1:
Mounting Google Drive
This command mounts your Google Drive to the indicated folder path (/content/drive). After this step has been performed, you will need to allow access to your Google Drive account. After the access has been granted, reading and writing files will become straightforward as you can do this straight from your Drive, which is very helpful in loading datasets and saving the results of the models during the project.
from google.colab import drive
drive.mount('/content/drive')
Import the necessary libraries.
This code block imports all the required libraries for this project for creating, training, and evaluating models. It also imports image processing libraries like PIL and OpenCV for handling images, and matplotlib and seaborn for data visualization. Scikit-learn utilities facilitate model evaluation using metrics such as confusion matrices.
import os
import keras
import numpy as np
from tqdm import tqdm
from keras.models import Sequential
from keras.callbacks import ModelCheckpoint
from keras.layers import Convolution2D, MaxPooling2D, ZeroPadding2D
from keras import optimizers
from keras.preprocessing import image
from PIL import Image,ImageOps
import cv2
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import confusion_matrix, classification_report
import matplotlib.pyplot as plt
import tensorflow
from tensorflow.keras import Model
from tensorflow.keras.layers import Input, BatchNormalization, ReLU, ELU, Dropout, Conv2D, Dense, MaxPool2D, AvgPool2D, GlobalAvgPool2D, Concatenate
import tensorflow as tf
import tensorflow.keras
from tensorflow.keras import models, layers
from tensorflow.keras.models import Model, model_from_json, Sequential
from tensorflow.keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
from tensorflow.keras.callbacks import TensorBoard
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D, SeparableConv2D, UpSampling2D, BatchNormalization, Input, GlobalAveragePooling2D
from tensorflow.keras.regularizers import l2
from tensorflow.keras.optimizers import SGD, RMSprop
from tensorflow.keras.utils import to_categorical
STEP 2:
Data collection and preparation
Load Dataset
This section of code is mainly focused on arranging the paths of the dataset. It starts by guiding the program to the main folder containing the blood-cell Datasets located on Google Drive. After that, it defines two different paths. One for the training set and another for the validation set.
dataset='/content/drive/MyDrive/Blood_Cell_Datasets'
train_folder = os.path.join(dataset,"training")
test_folder = os.path.join(dataset,"validation")
Listing categories
The code sets the size of the images, creates a list to hold the names of different classes, checks which classes are available in the training folder, and then prints those class names. This makes it easier to keep track of and understand the different types of images that will be used for training the model.
img_size = 128
categories = []
for i in os.listdir(train_folder):
categories.append(i)
print(categories)
STEP 3:
Data processing
This function iterates over different folders containing categories of images. Where it also performs reading and resizing images. It keeps the count of images across all categories and stores the processed images alongside their corresponding class numbers in a list. This takes care of the image preparation needed for training a model afterward.
# Function to process data
def process_data(folder, categories, img_size):
data = []
class_counts = {category: 0 for category in categories}
for c in categories:
path = os.path.join(folder, c)
class_num = categories.index(c)
for img in tqdm(os.listdir(path), desc=f"Processing {c}"):
try:
img_array = cv2.imread(os.path.join(path, img))
img_resized = cv2.resize(img_array, (img_size, img_size))
data.append([img_resized, class_num])
class_counts[c] += 1
except Exception as e:
pass
print(f"Class '{c}' has {class_counts[c]} images")
return data, class_counts
Processing Training Data
This code calls the process_data function. This function processes all the images in the training folder, resizes them, labels them by category, and then prints the total number of training images processed.
training_data, train_class_counts = process_data(train_folder, categories, img_size)
print(f"Total training data: {len(training_data)}")
Plotting Training Class Distributions
This code creates a visual bar chart that displays the count of images in each class for the training data. It helps in visualizing the distribution of data across different categories. This highlights whether it is balanced or skewed.
plt.figure(figsize=(10, 6))
plt.bar(train_class_counts.keys(), train_class_counts.values())
plt.xlabel('Categories')
plt.ylabel('Number of Images')
plt.title('Class Distribution (Training Data)')
plt.xticks(rotation=90, ha='right')
# Add labels to the bars
colors = plt.cm.get_cmap('viridis', len(train_class_counts))
for i, bar in enumerate(plt.gca().patches):
bar.set_color(colors(i))
plt.tight_layout()
plt.show()
Processing Validation Data
This code calls the process_data function. This function processes all the images in the validation folder, resizes them, labels them by category, and then prints the total number of validation images processed.
validation_data, val_class_counts = process_data(test_folder, categories, img_size)
print(f"Total validation data: {len(validation_data)}")
Plotting Training Class Distributions
This code creates a visual bar chart that displays the count of images in each class for the validation data.
plt.figure(figsize=(10, 6))
plt.bar(val_class_counts.keys(), val_class_counts.values())
plt.xlabel('Categories')
plt.ylabel('Number of Images')
plt.title('Class Distribution (Validation Data)')
plt.xticks(rotation=90, ha='right')
# Add labels to the bars
colors = plt.cm.get_cmap('viridis', len(val_class_counts))
for i, bar in enumerate(plt.gca().patches):
bar.set_color(colors(i))
plt.tight_layout()
plt.show()
Preparing Data for Model
This code’s task is to arrange and ready images (X_train) and labels (Y_train) for conducting the training process. It reshapes the images in the required form of 128 x 128 pixels and 3 color channels and creates NumPy arrays ready to be fed to a neural network.
X_train = []
Y_train = []
for img, label in training_data:
X_train.append(img)
Y_train.append(label)
X_train = np.array(X_train).astype('float32').reshape(-1, img_size, img_size, 3)
Y_train = np.array(Y_train)
print(f"X_train= {X_train.shape} Y_train= {Y_train.shape}")
This code’s task is to arrange and ready images (X_test) and labels (Y_test) for conducting the validation process. It reshapes the images in the required form of 128 x 128 pixels and 3 color channels and creates NumPy arrays ready to be fed to a neural network.
X_test = []
Y_test = []
for features,label in validation_data:
X_test.append(features)
Y_test.append(label)
X_test = np.array(X_test).astype('float32').reshape(-1, img_size, img_size, 3)
Y_test = np.array(Y_test)
print(f"X_test= {X_test.shape} Y_test= {Y_test.shape}")
X_train, X_test = X_train / 255.0, X_test / 255.0
STEP 4:
Visualization
This code randomly selects three images from every category in the training dataset and arranges them in a grid. Each image features its category name as the title. It makes an easy visualization of a sample from each class.
images = []
for img_folder in sorted(os.listdir(train_folder)):
img_items = os.listdir(train_folder + '/' + img_folder)
img_selected = np.random.choice(img_items)
images.append(os.path.join(train_folder,img_folder,img_selected))
fig=plt.figure(1, figsize=(15, 10))
for subplot, image_ in enumerate(images):
category = image_.split('/')[-2]
imgs = plt.imread(image_)
ax = fig.add_subplot(3, 3, subplot + 1)
ax.set_title(category, pad=10, size=14)
ax.imshow(imgs)
ax.axis('off')
plt.tight_layout()
STEP 5:
Model Building
CNN Architecture
This code builds a simple architecture of a Convolutional Neural Network (CNN) to classify images into 4 different classes.
The first two layers are the convolutional layers, comprising 32 filters and 64 filters receptively. We use ReLU activation for these two levels and use MaxPooling to reduce the size of the shape afterward. After flattening the output it passes through a fully connected layer of 128 neurons with ReLU activation. The final layer uses softmax activation with 9 output neurons for multi-class classification.
The model is built to use the Adam optimizer and sparse categorical cross-entropy loss while monitoring the accuracy of the model.
input_shape = (img_size, img_size, 3)
num_classes = 4
def build_basic_cnn(input_shape, num_classes):
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=input_shape))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
return model
model = build_basic_cnn(input_shape, num_classes)
model.summary()
Training the Basic CNN Model
This code creates a checkpoint and saves the best version of the model while training depending on the validation accuracy. The model is trained with 100 epochs, with a batch size of 32 using the training data. The checkpoint is set only to basic_cnn.h5 if the loss of the validation decreases and in this way, the best model is saved for further use.
checkpoint = ModelCheckpoint('basic_cnn.h5', monitor='val_accuracy', save_best_only=True, mode='max')
history = model.fit(X_train, Y_train, validation_data=(X_test, Y_test), epochs=100, batch_size=32, callbacks=[checkpoint])
Plotting CNN Training History
This code produces two plots side by side. One plot displays the CNN model's accuracy improvement over time. And the other shows the changes in loss. This effectively visualizes the model's performance throughout the training and validation phases.
def plot_history(history, title):
plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(history.history['accuracy'], label='train_accuracy')
plt.plot(history.history['val_accuracy'], label='val_accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.title(f'{title} Accuracy Curves')
plt.subplot(1, 2, 2)
plt.plot(history.history['loss'], label='train_loss')
plt.plot(history.history['val_loss'], label='val_loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.title(f'{title} Loss Curves')
plt.show()
plot_history(history, 'Basic CNN')
Evaluating Model performance
This code evaluates the CNN model’s performance on the training, validation, and test datasets by assessing accuracy and loss for each. It offers a clear insight into the model's performance.
valid_loss, valid_acc = model.evaluate(X_test, Y_test)
train_loss, train_acc= model.evaluate(X_train, Y_train)
print('\nValidation Accuracy:', valid_acc)
print('\nValidation Loss:', valid_loss)
print('\nTrain Accuracy:', train_acc)
print('\nTrain Loss:', train_loss)
This code loads a pre-trained model, basic_cnn.h5, and evaluates its performance on the test dataset (X_test and Y_test). It calculates the loss (loss2) and accuracy, and then prints the accuracy as a percentage, formatted to two decimal places. This provides a quick view of how well the model performs on unseen data.
model = tf.keras.models.load_model('basic_cnn.h5')
loss2, accuracy = model.evaluate(X_test, Y_test)
print(f"Accuracy: {accuracy * 100:.2f}%")
Plotting Confusion Matrix
The code predicts the test data and creates a confusion matrix. It provides a classification report. The confusion matrix is used to visually analyze the number of correct and incorrect predictions. The classification report presents class-wise metrics for the performance of the model. This is useful in assessing the model’s ability.
def plot_confusion_matrix(model, X_test, Y_test, categories, title):
Y_pred = model.predict(X_test)
Y_pred_classes = np.argmax(Y_pred, axis=1)
cm = confusion_matrix(Y_test, Y_pred_classes)
plt.figure(figsize=(10, 8))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=categories, yticklabels=categories)
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.title(title)
plt.show()
print("\n Classification Report:\n")
print(classification_report(Y_test, Y_pred_classes, target_names=categories))
plot_confusion_matrix(model, X_test, Y_test, categories, 'Basic CNN Confusion Matrix')
Building a Densenet121
The command !pip install -q efficientnet installs the efficientnet library quietly (without verbose output). This library provides pre-trained EfficientNet models, which are commonly used for tasks in image classification due to their efficiency and high performance.
!pip install -q efficientnet
This code creates an image classification model by utilizing EfficientNetB4 with ImageNet weights. It removes the top layers so that custom layers can be added for fine-tuning purposes. After extracting features, it includes layers for pooling, dense activation, batch normalization, and dropout, finishing with a softmax layer for multi-class output. The model uses the Adam optimizer and sparse categorical cross-entropy loss for compilation.
import efficientnet.tfkeras as efn
enet = efn.EfficientNetB4(
input_shape=input_shape,
weights='imagenet',
include_top=False
)
x = enet.output
x = tf.keras.layers.GlobalMaxPooling2D()(x)
x = tf.keras.layers.Dense(256, activation='relu')(x)
x = tf.keras.layers.BatchNormalization()(x)
x = tf.keras.layers.Dropout(0.5)(x)
y = tf.keras.layers.Dense(num_classes, activation='softmax')(x)
e_model_b4 = tf.keras.Model(inputs=enet.input, outputs=y)
e_model_b4.compile(
optimizer = tf.keras.optimizers.Adam(learning_rate=5e-4),
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
The model is trained with 100 epochs, with a batch size of 64 using the training data.
efficientnet_b4 = e_model_b4.fit(x=X_train, y=Y_train, epochs=100, validation_data=(X_test, Y_test), batch_size=64)
This code produces two plots side by side. One plot displays the EfficientNetB4 model's accuracy improvement over time. And the other shows the changes in loss. This effectively visualizes the model's performance throughout the training and validation phases.
def plot_history(history, title):
plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(history.history['accuracy'], label='train_accuracy')
plt.plot(history.history['val_accuracy'], label='val_accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.title(f'{title} Accuracy Curves')
plt.subplot(1, 2, 2)
plt.plot(history.history['loss'], label='train_loss')
plt.plot(history.history['val_loss'], label='val_loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.title(f'{title} Loss Curves')
plt.show()
plot_history(efficientnet_b4, 'EfficientNetB4')
This code evaluates the 'EfficientNetB4' model’s performance on the training, validation, and test datasets by assessing accuracy and loss for each. It offers a clear insight into the model's performance.
valid_loss, valid_acc = e_model_b4.evaluate(X_test, Y_test)
train_loss, train_acc = e_model_b4.evaluate(X_train, Y_train)
print('\nValidation Accuracy:', valid_acc)
print('\nValidation Loss:', valid_loss)
print('\nTrain Accuracy:', train_acc)
print('\nTrain Loss:', train_loss)
This code evaluates the e_model_b4 model on the test data (X_test and Y_test), calculating both the loss and accuracy metrics. The accuracy is then printed as a percentage, formatted to two decimal places, providing a concise summary of the model’s performance on the test dataset and indicating its effectiveness in classifying unseen data accurately.
loss, accuracy = e_model_b4.evaluate(X_test, Y_test)
print(f"Accuracy: {accuracy * 100:.2f}%")
This code saves the 'EfficientNetB4' model.
# Save the model
e_model_b4.save('efficientnet_b4_model.h5')
The code predicts the test data and creates a confusion matrix. It provides a classification report. The confusion matrix is used to visually analyze the number of correct and incorrect predictions. The classification report presents class-wise metrics for the performance of the model. Which is useful in assessing the model’s ability.
def plot_confusion_matrix(model, X_test, Y_test, categories, title):
Y_pred = model.predict(X_test)
Y_pred_classes = np.argmax(Y_pred, axis=1)
cm = confusion_matrix(Y_test, Y_pred_classes)
plt.figure(figsize=(10, 8))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=categories, yticklabels=categories)
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.title(title)
plt.show()
print("\n Classification Report:\n")
print(classification_report(Y_test, Y_pred_classes, target_names=categories))
# Plot confusion matrix and classification report
plot_confusion_matrix(loaded_model, X_test, Y_test, categories, 'EfficientNetB4 Confusion Matrix')
Building VGG16 Model
This code builds a custom model using VGG16 architecture which has 13 convolutional layers and is pre-trained on the ImageNet dataset. Here, global average pooling is incorporated, followed by the addition of two dense layers with 512 units (ReLU activation), batch normalization, and 50% dropout, further supplemented with a final softmax layer of num_classes. The model is compiled utilizing the Adam optimizer with a sparse categorical cross-entropy loss.
from keras.applications import VGG16
vgg16_model = VGG16(weights='imagenet', include_top=False, input_shape=input_shape)
for layer in vgg16_model.layers:
layer.trainable = False
vgg16_custom_model = Sequential()
vgg16_custom_model.add(vgg16_model)
vgg16_custom_model.add(GlobalAveragePooling2D())
vgg16_custom_model.add(Dense(512, activation='relu'))
vgg16_custom_model.add(BatchNormalization())
vgg16_custom_model.add(Dropout(0.5))
vgg16_custom_model.add(Dense(512, activation='relu'))
vgg16_custom_model.add(BatchNormalization())
vgg16_custom_model.add(Dropout(0.5))
vgg16_custom_model.add(Dense(num_classes, activation='softmax'))
# Compile the model
vgg16_custom_model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# Print model summary
vgg16_custom_model.summary()
This code trains the VGG16 model on the dataset for 100 epochs using a batch size of 64.
vgg16_pretrained = vgg16_custom_model.fit(
x=X_train,
y=Y_train,
epochs=100,
validation_data=(X_test, Y_test),
batch_size=64
)
This code produces two plots side by side. One plot displays the VGG16 model's accuracy improvement over time. And the other shows the changes in loss. This effectively visualizes the model's performance throughout the training and validation phases.
def plot_history(history, title):
plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(history.history['accuracy'], label='train_accuracy')
plt.plot(history.history['val_accuracy'], label='val_accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.title(f'{title} Accuracy Curves')
plt.subplot(1, 2, 2)
plt.plot(history.history['loss'], label='train_loss')
plt.plot(history.history['val_loss'], label='val_loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.title(f'{title} Loss Curves')
plt.show()
plot_history(vgg16_pretrained, 'VGG16')
This code evaluates the VGG16 model’s performance on the training, validation, and test datasets by assessing accuracy and loss for each. It offers a clear insight into the model's performance.
valid_loss, valid_acc = vgg16_custom_model.evaluate(X_test, Y_test)
train_loss, train_acc = vgg16_custom_model.evaluate(X_train, Y_train)
print('\nValidation Accuracy:', valid_acc)
print('\nValidation Loss:', valid_loss)
print('\nTrain Accuracy:', train_acc)
print('\nTrain Loss:', train_loss)
This code evaluates the vgg16_custom_model on the test dataset (X_test and Y_test), calculating the loss and accuracy metrics. It then prints the accuracy as a percentage, formatted to two decimal places, offering a clear summary of the model's classification performance on the test data.
loss, accuracy = vgg16_custom_model.evaluate(X_test, Y_test)
print(f"Accuracy: {accuracy * 100:.2f}%")
The code predicts the test data and creates a confusion matrix. It provides a classification report. The confusion matrix is used to visually analyze the number of correct and incorrect predictions. The classification report presents class-wise metrics for the performance of the model. Which is useful in assessing the model’s ability.
def plot_confusion_matrix(model, X_test, Y_test, categories, title):
Y_pred = model.predict(X_test)
Y_pred_classes = np.argmax(Y_pred, axis=1)
cm = confusion_matrix(Y_test, Y_pred_classes)
plt.figure(figsize=(10, 8))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=categories, yticklabels=categories)
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.title(title)
plt.show()
print("\n Classification Report:\n")
print(classification_report(Y_test, Y_pred_classes, target_names=categories))
plot_confusion_matrix(vgg16_custom_model, X_test, Y_test, categories, 'VGG16 Confusion Matrix')
STEP 6:
Prediction
Here, we present a modified dropout layer called FixedDropout, which allows for the change in the shape of dropout noise to control the dropout function across dimensions. A Creative Dropout class extends Dropout modifying _get_noise_shape() to support different noise dimensions according to the inputs needed. Once this FixedDropout layer is defined, it loads another model (efficientnet_b4_model.h5) within a custom object scope of FixedDropout meaning that this custom layer would be recognized when loading the model.
!pip install keras-applications
import tensorflow as tf
from keras.layers import Dropout
class FixedDropout(Dropout):
def _get_noise_shape(self, inputs):
if self.noise_shape is None:
return self.noise_shape
symbolic_shape = tf.shape(inputs)
noise_shape = [symbolic_shape[axis] if shape is None else shape
for axis, shape in enumerate(self.noise_shape)]
return tuple(noise_shape)
with tf.keras.utils.custom_object_scope({'FixedDropout': FixedDropout}):
loaded_model = tf.keras.models.load_model('/content/efficientnet_b4_model.h5')
This code loads, processes, and predicts the class of an image using a pre-trained model. It first reads an image file, resizes it to 128 x 128, scales the pixel values to a range of 0-1, and adds an extra dimension to match the input shape expected by the model.
The loaded_model then predicts the class probabilities, with np.argmax used to determine the index of the highest probability, which corresponds to the predicted class. The predicted class name is printed, and the image is displayed with a title indicating the predicted class.
img_array = cv2.imread('/content/drive/MyDrive/new_projects/p3/Datasets/validation/EOSINOPHIL/img_001.jpeg') # Replace with your image path
img_resized = cv2.resize(img_array, (img_size, img_size))
img_array = img_resized / 255.0
img_array = np.expand_dims(img_array, axis=0)
prediction = loaded_model.predict(img_array)
predicted_class_index = np.argmax(prediction)
predicted_class = categories[predicted_class_index]
print("Predicted Class:", predicted_class)
# Display the image
plt.imshow(cv2.cvtColor(img_resized, cv2.COLOR_BGR2RGB))
plt.title(f"Predicted: {predicted_class}")
plt.axis('off')
plt.show()
Project Conclusion
In this Blood Cell Classification, we build three different models which are Custom CNN, EfficientNetB4, and VGG16 to distinguish the different types of blood cells. Both models showcased that each of them has the capability of preprocessing and recognizing cell images. To prepare the dataset for high performance, we augmented, resized, and normalized the preprocessed dataset.
After testing and evaluation, we observed that EfficientNetB4 provided higher accuracy, in comparison to Custom CNN and VGG16. Because of both its speed and efficiency it was the most accurate choice for this particular task, Entrance on Classification. This work showcases the application of deep learning with particular emphasis on the automation of blood cell classification that improves diagnostic accuracy and efficiency.
Regardless of whether you operate in the field of biomedical research and development, healthcare-oriented start-ups, or AI technology, the approaches and instruments represented here contain strong guidelines for using AI in diagnostics. With additional refinement, this system may be expanded far beyond mammography toward other varieties of medical imaging, and be of exceptional use to the numerous members of the healthcare community who embrace it.
Challenges and Troubleshoot
Implementing such a large project, of course, has its own challenges. Let’s break them down and look at how you can overcome them:
Data Imbalance: A few categories of skin cancer may not have enough data which may lead to biased predictions and poor performance on the minority class.
- Solution: Implement data augmentation strategies such as rotation, flipping, or zooming enhancing the class size of the datasets where the class instances are underrepresented for better generalization.
Small Dataset: Limitation of data may result in inadequate performance of the model.
- Solution: Utilize Transfer Learning by applying the EfficientNet or DenseNet pre-trained models which are less data-greedy and designed for more adaptation.
Overfitting: When the data sample is limited or the data has an imbalanced nature, the model easily overfits and performs badly when generalizing.
- Solution: Incorporate Dropout layers to control overfitting and also implement early stopping when validation does not show any improvements.
Computational Resources: Training deep artificial networks requires a large amount of resources
Solution: Train on the free GPU offered by Google Colab or use a lighter pre-trained model.
FAQ:
Question 1: Which type of models were applied in blood cell classification?
Answer: For classification purposes, we employed three different models. Basic CNN model, pre-trained CNN architectures which are VGG16 and EfficientNetB4.
Question 2: How accurate is the model?
Answer: EfficientNetB4 gave the highest accuracy above 80% on the test set.
Question 3: What type of dataset was employed?
Answer: The blood cell image dataset used in the project was labeled image dataset comprising different types of blood cells. The dataset was collected from Kaggle.
Question 4: Why use pre-trained models like EfficientNetB4 and VGG16 in medical image classification?
Answer: Most of these models have been trained on large-scale datasets so are capable of learning a series of patterns in images. It is uncommon to make wrong predictions using it especially when working with small-sized data sets.
Question 5: How is overfitting handled in medical image classification?
Answer: To avoid signs of overfitting we employed other strategies such as implementing the Dropout layer and other forms of stopping once the model starts to over-fit.