Cervical Cancer Detection Using Deep Learning
Greetings everyone and thanks for joining this engaging project. In this project, we are making use of AI for a good reason and that is Cervical Cancer Detection using Deep Learning. Well, you're here if you are a geek, or a person more interested to know how deep-learning approaches are revolutionizing the healthcare sector. The mission of this project is to help you create a cervical cancer detection system using deep learning technology.
Interesting right? Let's dive in!
Project Overview
Cervical Cancer is extremely severe and to detect and classify it without any error is very crucial. In this project, you will learn to implement a working model for cervical cancer detection using deep learning. We'll show you how to do this on Google Colab from its computing capabilities. Also, you will learn how to work with an external dataset, process the data for the tasks, and include visualization for the assessment of the model's performance.
By the end of this project, you will have a complete application that can receive medical images and output an appropriate type of cervical cancer. Imagine having the ability for AI to assist a doctor in less than 5 minutes. Cervical cancer diagnosis using AI is the future that we are looking forward to today.
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
-
The cervical cancer dataset consists of images labeled corresponding to the types.
Do not stress if you are new to the deep learning world. This project breaks down complex deep-learning tasks in a simple way.
Approach
The process is aimed at developing an accurate Cancer detection CNN model in the following order:
-
Data Preprocessing: Here we have to prepare our dataset for feeding the model. We applied resizing, normalizing techniques.
-
Model design: Propose the architecture of CNN including the layers. Which would be able to extract relevant fields from the given images.
-
Training & Evaluation: Train the model and check how well the model works in real-time.
Workflow and Methodology
Let's explain this project in a sequential manner
-
We collected the dataset labeled in different types of cancer from Kaggle.
-
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.
-
In this project, a CNN structure is developed. This will conduct the analysis on the acquired images and predict the results.
-
The model has been trained on the preprocessed dataset and later, tested on the dataset that was not used during training.
-
The evaluation of the model's performance is done by evaluating accuracy, precision, recall, confusion matrix, etc.
The methodology
-
Data Preprocessing: The images are resized to 128x128. And pixel values are normalized for uniformity and better processing speed.
-
Convolutional Layers: Multiple convolutional layers are used to compute image characteristics such as edges and texture patterns.
-
Max Pooling: Helps to downscale the size of the feature map while preserving the essential information, thus making the model faster.
-
Dense Layers: The process of classifying images after the features have been extracted. The last layer has a Softmax activation function for multi-class classification.
-
Evaluation Metrics: Use of accuracy, confusion matrix, and classification report (precision, recall, and F1 score) to evaluate the performance.
Data Collection
Data is the backbone of any such work. While carrying out cervical cancer, we used a cervical cancer dataset from Kaggle consisting of a variety of countries' cervical cell images. The data was then used to create the main training set and the validation set for training and testing purposes respectively.
Data Preparation
The images were also preprocessed by using OpenCV to re-sized the images to a standard size of 128 x 128 pixels.
Data Preparation Workflow
-
Resize Images: Adjustment of image size to a particular standard.
-
Normalization: Scale the pixel values between 0 and 1.
Code Explanation
STEP 1:
Mounting Google Drive
We mount Google Drive to access our dataset stored in the cloud.
from google.colab import drive
drive.mount('/content/drive')
Importing 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 Cervical cancer 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/Cervical_Cancer_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)
This function iterates over different folders containing categories of images. Where it also performs reading and resizing images. Also keeps 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
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)}")
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, 8))
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()
Process 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.
# Process validation data
validation_data, val_class_counts = process_data(test_folder, categories, img_size)
print(f"Total validation data: {len(validation_data)}")
Plotting Validation Data Distribution
This code creates a visual bar chart that displays the count of images in each class for the validation data.
plt.figure(figsize=(10, 8))
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()
This code's task is to arrange and prepare images (X_train) and labels (Y_train) for conducting the training process. It reshapes the images in the required form (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 prepare images (X_test) and labels (Y_test) for conducting the validation process. It reshapes the images in the required form (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 3:
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 4:
Model Building
Building a basic CNN Model
This piece of code presents a basic but powerful convolutional neural network (CNN) model for image classification tasks. For instance, the network can be trained to classify images that depict cervical cancer cells into 7 different classes by associating the images with their patterns. The model employs convolutional layers for feature extraction, pooling layers for downscaling the dimensionality, and fully connected layers for classification. All are aimed at maximizing the model's accuracy.
input_shape = (img_size, img_size, 3)
num_classes = 7
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()
This is the part of the code that's necessary for training the model. Only the best version (in terms of highest validation accuracy) is saved to the file 'model.h5'. Training runs for 100 epochs with a batch size of 32. We evaluate performance on a validation set just once at the end of each epoch. The code watches the validation accuracy and checks the checkpoint to ensure that the best model it learns during the training is being used. Thus, it makes the training process much more efficient.
checkpoint = ModelCheckpoint('model.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])
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, 'Model 1')
This code is used for loading the best-performing model from training and evaluating on the test set (X_test, Y_test). Then print out the test accuracy as the percentage. This step allows you to validate how well the trained model generalizes to data that was not seen by the model yet. This is called a real-world performance picture of a model. For easy reading purposes, the accuracy in this example is printed with two decimal precision.
model = tf.keras.models.load_model('model.h5')
loss2, accuracy = model.evaluate(X_test, Y_test)
print(f"Model 2 Test 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.
We plot the confusion matrix and print the classification report.
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, 'Model 1 Confusion Matrix')
Building EfficientNetB0 model
This line of code allows you to use its pre-trained models and other functionalities in your deep-learning projects.
!pip install -q efficientnet
The EfficientNet model is imported, with RGB channels and pre-trained weights from ImageNet. 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 GlobalMaxPooling2D, 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.EfficientNetB0(
input_shape=(128, 128, 3),
weights='imagenet',
include_top=False
)
# Use the 'model' variable instead of 'loaded_model'
for layer in model.layers:
layer.trainable = 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_b0= tf.keras.Model(inputs=enet.input, outputs=y)
e_model_b0.compile(
optimizer = tf.keras.optimizers.Adam(learning_rate=5e-4),
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
Training the EfficientNetB0
The EfficientNetB0 architecture in this line of code is trained on the training data for 100 epochs using a batch size of 64. It also conducts the validation of the model at the end of each epoch. It tracks the learning and generalization performance of the model effectively. The training history is saved.
efficientnet_b0 = e_model_b0.fit(x=X_train, y=Y_train, epochs=100, validation_data=(X_test, Y_test), batch_size=64)
Saving the EfficientNet Model
This code saves the architecture of the EfficientNetB0 model as a JSON file and its trained weights in HDF5 format.model_json = e_model_b0.to_json()
with open('/content/drive/MyDrive/new_projects/enet_updated.json', 'w') as json_file:
json_file.write(model_json)
e_model_b0.save_weights('/content/drive/MyDrive/cervical_cancer/efficientnet_b0_model2_updated.h5')
Plotting 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(efficientnet_b0, 'EfficientNetB0')
Evaluating the Model
This code is used for loading the best-performing model from training and evaluating the test set (X_test, Y_test). Then print out the test accuracy as the percentage. This step allows you to validate how well the trained model generalizes to data that was not seen by the model yet.
loss, accuracy = e_model_b0.evaluate(X_test, Y_test)
print(f"EfficientNetB0 Test 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.
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(e_model_b0, X_test, Y_test, categories, 'EfficientNetB0 Confusion Matrix')
Building a Sequential Model
This code segment demonstrates a function for creating a Convolutional Neural network (CNN) which aims at image classification. The function build_model receives input_shape and num_classes to create a Sequential model.
Three sets of Conv2D layers with filter sizes of 32, 64, and 128, respectively, followed by MaxPooling2D layers for feature extraction as well as reduction of spatial dimensions are added. Then an output of this layer is converted into a 1D vector using a Flatten layer. It is then followed by a fully connected Dense layer with 128 neurons and the ReLU activation function. Further, to reduce the chances of overfitting, a Dropout layer with a 50% grade is fitted earlier than the last fully connected layer that uses softmax activation to output probabilities per class. The model is fitted with an Adam optimizer and also a sparse categorical cross-entropy loss function.
def build_model(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(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
return model
input_shape = (img_size, img_size, 3)
num_classes = len(categories)
model1 = build_model(input_shape, num_classes)
model1.summary()
Training the Sequential Model
The model is trained on the training data for 100 epochs using a batch size of 32. It also conducts the validation of the model at the end of each epoch. It tracks the learning and generalization performance of the model effectively. The training history is saved.
checkpoint3 = ModelCheckpoint('model3.h5', monitor='val_accuracy', save_best_only=True, mode='max')
history3 = model3.fit(X_train, Y_train, validation_data=(X_test, Y_test), epochs=100, batch_size=32, callbacks=[checkpoint3])
Plotting 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(history3, 'Model 3')
Evaluating the Model
This code evaluates the custom CNN model's performance on the test datasets by assessing accuracy and loss for each. It offers a clear insight about the model performance.
model3 = tf.keras.models.load_model('model3.h5')
loss3, accuracy3 = model3.evaluate(X_test, Y_test)
print(f"Model 3 Test Accuracy: {accuracy3 * 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(model3, X_test, Y_test, categories, 'Model 3 Confusion Matrix')
STEP 5:
Prediction
Load The Training Model
This code loads previously saved models. First, it reads its architecture from a JSON file and then reconstructs it. It then loads the model's weights from an HDF5 file, enabling immediate use of the trained model for further evaluation or predictions without the need to retrain it.
with open('/content/drive/MyDrive/new_projects/enet_updated.json', 'r') as json_file:
loaded_model_json = json_file.read()
loaded_model = model_from_json(loaded_model_json, custom_objects={'swish': tf.nn.swish, 'EfficientNetB0': efn.EfficientNetB0})
# Load the model weights
loaded_model.load_weights('/content/drive/MyDrive/carvical_cancer/efficientnet_b0_model2_updated.h5')
Compile the loaded model is used to compile the earlier loaded model, together with an Adam optimizer and a loss function of sparse categorical cross-entropy. It makes it ready for evaluation or further training. In addition, it creates a category map that assigns numeric labels to each cervical cancer class for a better understanding of the output of the model.
next predict a single image function involves image preprocessing and classification of the retrained model's input. It analyzes a given image, as well as resizes and normalizes the image before predicting as to what category the image belongs. It finds the class which has the maximum predicted probability. Then maps it to the names of different categories stored in a predefined array of category names. Then it displays the image with the predicted title.
# Compile the loaded model
loaded_model.compile(
optimizer=tf.keras.optimizers.Adam(learning_rate=5e-4),
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
# Category mapping
category = {
0:'abnormal severe dysplasia',
1: 'normal superficial squamous cell',
2: 'abnormal mild squamous non-keratinizing dysplasia',
3: 'normal intermediate squamous cell',
4: 'abnormal carcinoma in situ',
5: 'abnormal moderate dysplastic',
6: 'normal columnar cell'
}
# Function to predict a single image
def predict_image(filename, model):
img_ = image.load_img(filename, target_size=(128, 128)) # Change target_size to (128, 128)
img_array = image.img_to_array(img_)
img_processed = np.expand_dims(img_array, axis=0)
img_processed /= 255.0
prediction = model.predict(img_processed)
index = np.argmax(prediction)
plt.title("prediction_output_image1_for_cervical_cancer - {}".format(category[index]))
plt.imshow(img_array)
plt.show()
This line of code calls the predict_image function to predict a specified image of a cervical cancer case using the previously loaded model.
img_path = '/content/drive/MyDrive/new_projects/Cervical_Cancer_testset//img_001.jpg'
predict_image(img_path, loaded_model)
img_path = '/content/drive/MyDrive/new_projects/Cervical_Cancer_testset/img_002.jpg'
predict_image(img_path, loaded_model)
Project Conclusion
In this project, we set out to tackle the task of cervical cancer detection using deep learning. We were able to classify cervical cancer images into seven classes using high accuracy by using advanced CNN architectures and Sequential models. We also applied a transfer learning technique using the EfficientNetB0 model. All models did well while EfficientNetB0 achieved the highest accuracy of 97.79%.
Now we have an Advanced AI tool that does an excellent job of achieving high accuracy and demonstrating the power of AI in healthcare. If you want to dabble with medical image classification, this is a great way to get hands-on experience and potentially make a difference in the real world!
Challenges New Coders Might Face
As cool as it is to build something like this, it of course doesn't always run like clockwork, but that's part of the fun! Here are a few bumps you might hit:
Computational Resources: Training models such as EfficientNetB0 are computationally intensive and can be a hurdle when working on a local machine.
Solution: Use Google Colab or AWS for cloud-based GPU use, which either offers free access or paid. This makes training so much quicker.
Data Imbalance: When an algorithm is applied to an imbalanced dataset where one class has more images than other classes, we might get biased predictions.
Solution: You can apply data augmentation to underrepresented classes, or use the class weighting on your model to give more weight to the less frequent classes in your training.
Overfitting: If a model performs very well for training data, but very poorly otherwise, we say the model has overfitted.
Solution: Regularization: Dropout plus early stopping during training. Data augmentation can also be used to increase data for the model in a way that attempts to simulate a dataset that consists of more samples.
Image Quality Variations
Images of varying quality, lighting, or resolution may make the model struggle to classify the images properly.
Solution: Normalize image size and lighting condition and preprocess your images. There are a lot of techniques you use on images during the training process such as random rotation, zoom, and contrast adjustment to simulate different conditions.
FAQ
Q1: How are models integrated in the detection of cervical cancer?
A: This project is built upon three main models: a basic CNN, EfficientNetB0 (for enhancement of accuracy), and a Sequential one-this last one is CNN
Q2: Why was EfficientNetB0 used in cervical cancer detection?
A: For efficiency and accuracy, EfficientNetB0 was chosen. Its tradeoff between performance and computational cost offers a great balance for image classification tasks such as cervical cancer detection.
Q3: How many categories were the cervical cell images classified into?
A: The model was able to classify cervical cell images into seven categories, normal and abnormal.
Q4: How was the dataset processed for medical image classification?
A: Images were resized, data augmentation techniques were used, and the data was split to training and validation data (80% for training, 20% for validation).
Q5: What was the accuracy of the CNN models?
A: We used the test data to test the EfficientNetB0 model and saw an accuracy of 97.79% and the Sequential model gave 91.29% accuracy.
You can do this
So now it's time for action!
Want to build it yourself? One can download the code, go through the instructions on the site, and then modify the type of crop you prefer.
Spread the word! Spread this project across your network, let's change how we can change the health system.