Posts Tagged

early detection

Identifying Alzheimer’s Disease with Deep Learning: A Transfer Learning Approach

Identifying Alzheimer’s Disease with Deep Learning: A Transfer Learning Approach

Alzheimer’s disease is a degenerative brain disorder that affects millions of people worldwide. It is a progressive disease that leads to memory loss, cognitive decline, and eventually the inability to carry out basic tasks. Early diagnosis and intervention can improve the quality of life of those affected by the disease. In this tutorial, we will use deep learning techniques to identify Alzheimer’s disease from MRI brain scans.

Data Preprocessing

We will be using the Alzheimer’s Disease Neuroimaging Initiative (ADNI) dataset for this tutorial. The dataset contains MRI brain scans of patients with Alzheimer’s disease and healthy individuals. We will use the T1-weighted MRI images for our analysis.

First, we will load the dataset and split it into training and testing sets. We will also preprocess the data by resizing the images and normalizing the pixel values.

# Import the necessary libraries
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from tensorflow.keras.preprocessing.image import load_img, img_to_array
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input

# Load the metadata file
metadata = pd.read_csv('ADNI_Metadata.csv')
# Create lists to store the images and labels
images = []
labels = []
# Loop through the metadata file and load the images and labels
for i, row in metadata.iterrows():
    # Load the image and resize it to 224x224
    img = load_img(row['Image'], target_size=(224, 224))
    img_array = img_to_array(img)
    # Preprocess the image
    img_array = preprocess_input(img_array)
    # Add the label to the list
    label = row['Label']
    if label == 'CN':
    elif label == 'AD':
# Convert the data to arrays
images = np.array(images)
labels = np.array(labels)
# Split the data into training and testing sets
train_images, test_images, train_labels, test_labels = train_test_split(images, labels, test_size=0.2, random_state=42)

Building the Model

We will use transfer learning to build our model. We will use the MobileNetV2 architecture, which has been pre-trained on the ImageNet dataset. We will add a GlobalAveragePooling2D layer to reduce the dimensionality of the output and a Dense layer with a sigmoid activation function to classify the images as Alzheimer’s disease or healthy.

from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2
from tensorflow.keras.models import Model
from tensorflow.keras.layers import GlobalAveragePooling2D, Dense

# Load the pre-trained MobileNetV2 model
base_model = MobileNetV2(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
# Add a GlobalAveragePooling2D layer
x = base_model.output
x = GlobalAveragePooling2D()(x)
# Add a Dense layer with a sigmoid activation function
output = Dense(1, activation='sigmoid')(x)
# Create the model
model = Model(inputs=base_model.input, outputs=output)
# Freeze the layers of the pre-trained model
for layer in base_model.layers:
    layer.trainable = False
# Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

Training the Model

We will train the model using the training data and evaluate it on the testing data. We will use the binary cross-entropy loss function and the Adam optimizer.

# Train the model
history =, train_labels, epochs=10, batch_size=32, validation_data=(test_images, test_labels))

# Evaluate the model on the testing data
test_loss, test_acc = model.evaluate(test_images, test_labels)
print('Test accuracy:', test_acc)

Predicting Alzheimer’s Disease

We can now use our trained model to predict Alzheimer’s disease from MRI brain scans. We will load a sample image and preprocess it before making a prediction.

# Load a sample image
img_path = 'sample_image.jpg'
img = load_img(img_path, target_size=(224, 224))
img_array = img_to_array(img)
img_array = preprocess_input(img_array)
img_array = np.expand_dims(img_array, axis=0)

# Make a prediction
prediction = model.predict(img_array)

# Print the prediction
if prediction[0] < 0.5:
    print('The image is classified as healthy.')
    print('The image is classified as Alzheimer\'s disease.')

In this tutorial, we have learned how to use deep learning techniques to identify Alzheimer’s disease from MRI brain scans. We used transfer learning with the MobileNetV2 architecture and achieved good accuracy on the testing data. This technique can be applied to other medical imaging datasets to aid in the early detection and diagnosis of diseases.