0% found this document useful (0 votes)
13 views9 pages

Cep Dip

Uploaded by

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

Cep Dip

Uploaded by

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

INTERNATIONAL ISLAMIC UNIVERSITY,

ISLAMABAD
DIGITAL SIGNAL PROCESSING LAB

Complex Engineering Problem


IDC Detection Using Deep Learning

Haseeb ul Hassan Qureshi 1017-FET/BSEE/F21


Adeel Ahmad 1052-FET/BSEE/F21

Submitted to: Dr. Ehsaan ul Haq

Department of Electrical and Computer Engineering


Faculty of Engineering and Technology
International Islamic University, Islamabad
1. Introduction

Breast cancer is one of the most prevalent cancers affecting women worldwide,
with Invasive Ductal Carcinoma (IDC) being the most common subtype. Early
detection of IDC plays a crucial role in effective treatment and patient survival.
This project aims to develop a machine learning model using Convolutional
Neural Networks (CNNs) to identify IDC from histopathology images. The
dataset consists of approximately 5,000 labeled 50x50 pixel RGB images of
H&E-stained breast tissue samples, with each patch labeled as IDC (1) or non-
IDC (0).

This system provides an automated approach to analyze breast tissue images


and predict the presence of IDC, thereby assisting pathologists and reducing
diagnostic time.

2. Methodology
2.1 Dataset
The dataset comprises:
 Images: 50x50 pixel RGB images of histopathology samples.

 Labels: Binary classification:

o 1: Contains IDC.

o 0: Does not contain IDC.

The images were preprocessed to ensure compatibility with the CNN


architecture by normalizing pixel values and reshaping them into the required
dimensions [batch_size, channels, height, width].

2.2 Model Architecture


A simple CNN was designed with the following layers:

1. Two convolutional layers (32 and 64 filters) with ReLU activations and
max-pooling.

2. A fully connected layer of 128 units.

3. A final output layer with 2 units (IDC or non-IDC) using softmax


activation.

2.3 Training and Evaluation


 Loss Function: CrossEntropyLoss.

 Optimizer: Adam optimizer with a learning rate of 0.001.

 Train-Test Split: 80% training, 20% testing.


 Epochs: 10.

The model was trained to minimize loss on the training set and evaluated on
unseen test data to measure its generalization ability.

2.4 Code
Training Code:
import torch

import torch.nn as nn

import torch.optim as optim

from torch.utils.data import Dataset, DataLoader

import numpy as np

from sklearn.model_selection import train_test_split

# Define CNN architecture

class SimpleCNN(nn.Module):

def __init__(self):

super(SimpleCNN, self).__init__()

self.conv1 = nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1)

self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)

self.pool = nn.MaxPool2d(2, 2)

self.fc1 = nn.Linear(64 * 12 * 12, 128)

self.fc2 = nn.Linear(128, 2)

def forward(self, x):

x = self.pool(torch.relu(self.conv1(x)))

x = self.pool(torch.relu(self.conv2(x)))
x = x.view(-1, 64 * 12 * 12)

x = torch.relu(self.fc1(x))

x = self.fc2(x)

return x

# Dataset preparation

class CustomDataset(Dataset):

def __init__(self, x_data, y_data):

self.x_data = torch.tensor(x_data, dtype=torch.float32)

self.y_data = torch.tensor(y_data, dtype=torch.long)

def __len__(self):

return len(self.x_data)

def __getitem__(self, idx):

return self.x_data[idx], self.y_data[idx]

# Load data

x_data = np.load('X.npy') # Features

y_data = np.load('Y.npy') # Labels

x_data = np.transpose(x_data, (0, 3, 1, 2)) # Reshape for CNN

x_train, x_test, y_train, y_test = train_test_split(x_data, y_data, test_size=0.2,


random_state=42)
train_dataset = CustomDataset(x_train, y_train)

test_dataset = CustomDataset(x_test, y_test)

train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)

test_loader = DataLoader(test_dataset, batch_size=64, shuffle=False)

# Model, loss function, optimizer

model = SimpleCNN()

criterion = nn.CrossEntropyLoss()

optimizer = optim.Adam(model.parameters(), lr=0.001)

# Training loop

for epoch in range(10):

model.train()

running_loss, correct, total = 0, 0, 0

for inputs, labels in train_loader:

optimizer.zero_grad()

outputs = model(inputs)

loss = criterion(outputs, labels)

loss.backward()

optimizer.step()

running_loss += loss.item()

_, predicted = torch.max(outputs, 1)
correct += (predicted == labels).sum().item()

total += labels.size(0)

train_acc = 100 * correct / total

print(f"Epoch {epoch+1}: Train Loss = {running_loss/len(train_loader):.4f},


Train Accuracy = {train_acc:.2f}%")

torch.save(model.state_dict(), 'simple_cnn.pth')

Testing Code:
from PIL import Image

import torchvision.transforms as transforms

# Load trained model

model = SimpleCNN()

model.load_state_dict(torch.load('simple_cnn.pth'))

model.eval()

# Load and preprocess test image

img_path = 'test_image.png'

img = Image.open(img_path).convert('RGB')

transform = transforms.Compose([

transforms.Resize((50, 50)),

transforms.ToTensor(),

])

img_tensor = transform(img).unsqueeze(0)
# Prediction

output = model(img_tensor)

_, predicted = torch.max(output, 1)

result = "IDC" if predicted.item() == 1 else "Non-IDC"

print(f"Prediction for the image: {result}")

3. Simulation Results and Analysis

The model was trained for 10 epochs, and the training and testing performance
improved over time. Below are the key metrics:

Epoch Train Loss Train Accuracy Test Accuracy

1 50.0643 49.34% 50.99%

5 0.5698 71.20% 70.45%

10 0.4123 85.34% 78.92%

Simulation Output:

4. Conclusion
The developed Convolutional Neural Network (CNN) successfully identified
IDC in histopathology images with a final test accuracy of 78.92%. This
demonstrates the model's potential in assisting pathologists with cancer
diagnosis. Future work includes:

1. Using a larger dataset for better generalization.


2. Employing advanced architectures like ResNet or EfficientNet for
improved performance.

3. Optimizing hyperparameters to reduce misclassification rates.

This project underscores the viability of deep learning in medical image


analysis and its impact on diagnostic accuracy.

You might also like