OpenCV Project – Cell Classification

Machine Learning courses with 100+ Real-time projects Start Now!!

Cell classification is a crucial task in biomedical image analysis involving the categorization of cells into distinct groups based on visual characteristics. Leveraging advanced technologies such as convolutional neural networks (CNNs) and cell classification aids in automating disease diagnosis, drug development and understanding cellular behavior. By training models on diverse cell images, these systems contribute to more efficient and accurate analyses.

VGG16

VGG16 is a deep convolutional neural network architecture renowned for its simplicity and effectiveness in image classification. It was developed by the Visual Geometry Group (VGG) at Oxford. It consists of 16 weight layers, including convolutional and fully connected layers. VGG16’s uniform structure facilitates feature extraction, making it a cornerstone in computer vision and deep learning applications.

Dataset

The cell dataset consists of four classes – eosinophils, monocytes, lymphocytes and neutrophils. Each class represents distinct cell types, contributing to a diverse and comprehensive dataset for training and evaluating machine learning models, particularly in the field of cell classification using technologies like CNNs.

Prerequisites For Cell Classification Using OpenCV

Proficiency in Python and familiarity with the OpenCV library are prerequisites, as are the following system requirements.

  • Python 3.7 (64-bit) and above
  • Any Python editor (VS code, Pycharm)
  • GPU 4.00 GB+

Note:- In this Tutorial, Google Colab is used.

Download the OpenCV Cell Classification Project

Please download the source code for the OpenCV Cell Classification Project: OpenCV Cell Classification Project Code.

Installation

Open Windows cmd as administrator

1. Install OpenCV library.

pip install opencv-python

2. Install TensorFlow library.

pip install tensorflow

3. Install matplotlib library.

pip install matplotlib

Let’s Implement

1. Import all the packages.

import os
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications import VGG16
from tensorflow.keras import layers, models
from tensorflow.keras.optimizers import Adam
import matplotlib.pyplot as plt
from tensorflow.keras.preprocessing import image
from tensorflow.keras.models import load_model
import numpy as np

2. Set the dataset path.

Data = '/content/drive/MyDrive/Data_Flair/Cell/data/TRAIN'

3. It defines the input image shape, number of classes, batch size and number of epochs.

input_shape = (224, 224, 3)
num_classes = 4  
batch_size = 32
epochs = 20

4. It defines an image data generator and creates a directory-based data generator for training with specified target size, batch size and categorical loss mode.

train_datagen = ImageDataGenerator(
    rescale=1./255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True
)

train_generator = train_datagen.flow_from_directory(
    Data,
    target_size=input_shape[:2],
    batch_size=batch_size,
    class_mode='categorical'
)

5. It loads the VGG16 model with pre-trained Imagenet weights, excluding the top classification layer, and sets it as trainable.

base_model = VGG16(weights='imagenet', include_top=False, input_shape=input_shape)
base_model.trainable = True

6. It freezes the first layers of the base model up to the fourth-to-last layer for fine-tuning.

fine_tune_at = -4
for layer in base_model.layers[:fine_tune_at]:
    layer.trainable = False

7. It specifies the model architecture.

model = models.Sequential([
    base_model,
    layers.Flatten(),
    layers.Dense(256, activation='relu'),
    layers.Dropout(0.5),
    layers.Dense(128, activation='relu'),  
    layers.Dropout(0.5),
    layers.Dense(num_classes, activation='softmax')
])

8. It compiles the model and starts to train the model.

model.compile(optimizer=Adam(learning_rate=1e-5), 
              loss='categorical_crossentropy',
              metrics=['accuracy'])

history = model.fit(train_generator, epochs=epochs)

9. It prints the training accuracy of the model.

_, training_accuracy = model.evaluate(train_generator)
print(f'Training Accuracy: {training_accuracy * 100:.2f}%')

The output of this step

training accuracy

10. It defines and calls a function to plot training accuracy and loss over epochs using matplotlib.

def plot_history(history):
    plt.figure(figsize=(12, 6))
    plt.subplot(1, 2, 1)
    plt.plot(history.history['accuracy'])
    plt.title('Training Accuracy')
    plt.xlabel('Epoch')
    plt.ylabel('Accuracy')
    plt.subplot(1, 2, 2)
    plt.plot(history.history['loss'])
    plt.title('Training Loss')
    plt.xlabel('Epoch')
    plt.ylabel('Loss')
    plt.show()

plot_history(history)

The output of this step

function to plot training

11. It saves the trained model.

model.save('cell_classification_model_improved.h5')

12. It loads the pre-trained model, predicts the class of an input image, and displays the image with the predicted class label using matplotlib.

model = load_model('cell_classification_model_improved.h5')  
Input_img = '/content/drive/MyDrive/Data_Flair/Cell/data/TRAIN/NEUTROPHIL/_151_8029.jpeg'
img = image.load_img(Input_img, target_size=(224, 224))
img_array = image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
img_array /= 255.0  
predictions = model.predict(img_array)
predicted_class = np.argmax(predictions, axis=1)
class_names = {0: 'EOSINOPHIL', 1: 'LYMPHOCYTE', 2: 'MONOCYTE', 3: 'NEUTROPHIL'}
plt.imshow(img)
plt.title(f'Predicted Class: {class_names[predicted_class[0]]}')
plt.show()

OpenCV Cell Classification Output

cell classification output

cell classification output image

Conclusion

In conclusion, the application of Convolutional Neural Networks (CNNs) for cell classification has proven to be a transformative approach, demonstrating remarkable accuracy and efficiency. By harnessing the power of deep learning, CNNs enable the automated and precise identification of cell types, paving the way for advancements in medical diagnostics, drug discovery, and beyond. The ability to analyze complex cellular structures with high accuracy underscores the potential of CNNs in revolutionizing our understanding of biology and facilitating breakthroughs in various scientific and medical domains.

Did you like this article? If Yes, please give DataFlair 5 Stars on Google

courses
Image

TechVidvan Team

TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.

Leave a Reply

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