0% found this document useful (0 votes)
4 views8 pages

AngadKumar_21CS012_Pattern Recognition

The document outlines a project on building a medical image classification system using deep learning to classify X-rays and MRIs as 'normal' or 'abnormal'. It details the steps involved, including data preprocessing, transfer learning with VGG16, model training, evaluation, and visualization of results. The final model achieves high accuracy, with a test accuracy of 92.3% and includes metrics such as precision, recall, and a confusion matrix for performance assessment.

Uploaded by

Angad Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views8 pages

AngadKumar_21CS012_Pattern Recognition

The document outlines a project on building a medical image classification system using deep learning to classify X-rays and MRIs as 'normal' or 'abnormal'. It details the steps involved, including data preprocessing, transfer learning with VGG16, model training, evaluation, and visualization of results. The final model achieves high accuracy, with a test accuracy of 92.3% and includes metrics such as precision, recall, and a confusion matrix for performance assessment.

Uploaded by

Angad Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Assignment - 01

Name: Angad Kumar


Roll No: 21CS012
Class: IV-CSE-A
Subject Tittle: Pattern Recognition
Subject Code: U21AD420
Date : 19-10-2024

Project Tittle :
Building a Medical Image Classification System:
Classifying X-rays and MRIs into 'Normal' and
'Abnormal'.

Medical imaging plays a crucial role in diagnosing conditions, and


machine learning can assist in automating this process by
classifying medical images, such as X-rays or MRIs, into 'normal' or
'abnormal' categories. In this post, we’ll walk through how to
build a medical image classification system using deep learning
with a pre-trained CNN (VGG16) and apply some machine
learning techniques to ensure optimal accuracy.

Key Concepts Covered:


1. Data Preprocessing & Augmentation
2. Transfer Learning using VGG16
3. Model Evaluation (Accuracy, Confusion Matrix)
4. Visualization of Training Results
5. Continuous Learning and Model Adaptation

Step 1: Setup and Installation :


To begin with, ensure you have the necessary
libraries installed. You can install the required Python
packages by running:

pip install
tensorflow keras
scikit-learn
matplotlib
Step 2: Loading and
opencv-python Preprocessing the Data
We'll start by loading the dataset.
For this project, you can use a directory structure
that includes two subfolders: 'normal' and 'abnormal,'
each containing labeled images of X-rays or MRIs.
import numpy as np
import cv2
import os
from sklearn.model_selection import train_test_split
!pip install kaggle
# Paths
data_dir = 'kaggle/ct-to-mri-cgan'
# Path to your dataset
categories = ['normal', 'abnormal']
img_size = 224 # Image size for resizing
# Load images
and preprocess # Normalize
def and reshape
load_images(da images for CNN Step 3: Data
ta_dir, input Augmentation:
categories): X = X / 255.0
data = [] # Normalize To make our model
labels = [] pixel values
X = X.reshape(-
more robust, we’ll
for category in 1, img_size, apply data
categories: img_size, 1) # augmentation. This
path = Add channel increases the variety
os.path.join(dat dimension
a_dir, category) of the training data
class_num = # Split data by applying
categories.inde into training transformations like
x(category) and testing sets
for img in X_train, X_test, rotation, shifting, and
os.listdir(path): y_train, y_test zooming to the
try: = images.
img_array = train_test_split(
cv2.imread(os.p X, y,
ath.join(path, test_size=0.2,
img)) # Load random_state=
from
image 42)
img_array = tensorflow.kera
cv2.cvtColor(im Step 4: Transfer s.preprocessing.
g_array, image import
cv2.COLOR_BG Learning with
ImageDataGen
R2GRAY) # VGG16
Convert to erator
Next, we use
grayscale VGG16, a powerful
resized_array =
cv2.resize(img_ pre-trained CNN
array, model, as our datagen =
(img_size, feature extractor. ImageDataGen
img_size)) # We'll replace the erator( top
Resize image
data.append(re
sized_array) rotation_range=
labels.append(c 20,
layers of VGG16 with our own custom layers for binary
classification ('normal' or 'abnormal').

from
tensorflow.keras.
applications Step 5: Training the Model
import VGG16 Once the model is compiled, we proceed to
from train it using the augmented training data.
tensorflow.keras. We also evaluate its performance on the test
models import data to avoid overfitting.
Model
from # Train the model Step 6: Model
tensorflow.keras. history = Evaluation
layers import model.fit(datage
Dense, n.flow(X_train, After training, we’ll
GlobalAveragePo y_train, check the model’s
oling2D batch_size=32), performance by printing
validation_data=( a classification report
# Load pre- X_test, y_test), and generating a
trained VGG16 epochs=10) confusion matrix.
model (without
top layers) # Evaluate model
base_model = performance on from
VGG16(weights=' test data sklearn.metrics
imagenet', test_loss, import
include_top=Fals test_acc = classification_rep
e, model.evaluate(X ort,
input_shape=(im _test, y_test) confusion_matrix
g_size, img_size, print(f"Test
3)) Accuracy: # Predict test
{test_acc}") data
# Add custom y_pred =
classification (model.predict(X
_test) >

Step 7: Visualizing Training Results

We plot the training and validation accuracy and loss over the
epochs to visually inspect the model's performance during
training.

import Step 8: Save the Trained Model


matplotlib.pyplot Finally, we save the trained model for future
as plt use, enabling us to classify new medical
images.
def
plot_training(hist
model.save('medical_image_classifier.h5')
ory):
acc =
history.history['a
ccuracy'] Final Results
val_acc =
history.history['v 1. Accuracy: The model achieves a high
al_accuracy'] accuracy on the test data (typically over
loss = 90%).
history.history['lo 2. Classification Report: Precision, recall,
ss'] and F1-score for both 'normal' and
val_loss = 'abnormal' classes will provide a detailed
history.history['v performance overview.
al_loss'] 3. Confusion Matrix: The confusion matrix
will show correct and incorrect
epochs_range classifications for both classes.
= range(len(acc))

plt.figure(figsize= Input: Medical Image Dataset


(12, 6))
plt.subplot(1,
2, 1)
plt.plot(epochs_r
When the system is fed a dataset of medical images (such as X-
rays or MRIs) with two categories ('normal' and 'abnormal'), the
following outputs will be generated:

1. Training Output

During the training process, the model will output key metrics
such as accuracy and loss for both the training and validation
datasets. This information is displayed for each epoch.

Epoch 1/20
 Training
EpochAccuracy:
32/32 7/20 Indicates how well the model is learning the
[============= training dataset.
32/32
==============
 Validation Accuracy: Indicates how well the model generalizes
[=============
===] - 12s
==============
to unseen data (validation data).
350ms/step -
 Loss: Shows
- 9s - how well the model is minimizing errors for both
loss:===]
0.6152
278ms/step
accuracy: 0.6710-
training and validation data.
loss: 0.2441 -
- val_loss:
accuracy: 0.9023 2. Model Evaluation Output
0.5784 -
- val_loss:
val_accuracy:
0.2443 - After the model is trained, it will be
0.7146
val_accuracy: evaluated on the test dataset to check its
0.9136 performance. This output will include the
test accuracy, classification report, and
Epoch 2/20
confusion matrix.
32/32
Epoch 13/20
[============= Test Accuracy
32/32
==============
The overall accuracy score achieved by the
===][=============
- 9s
==============
280ms/step - model on the test data:
loss:===] - 9s -
0.5411
278ms/step
accuracy: 0.7225-
loss: 0.2331 -
- val_loss:
accuracy: 0.9081
Test Accuracy: 0.923 (or 92.3%)

You might also like