MRI classification using Intel oneAPI and Keras [AI for Autism]

Usha Rengaraju
6 min readMar 3, 2024

--

Autism Spectrum Disorder (ASD) is a complex neurodevelopmental condition that affects communication, social interaction, and behavior. Early diagnosis and intervention are crucial for improving outcomes for individuals with ASD. Magnetic Resonance Imaging (MRI) is a powerful tool used in the diagnosis and study of ASD, offering detailed images of the brain’s structure and function. Recent advancements in Artificial Intelligence (AI) have led to the development of sophisticated algorithms capable of analyzing MRI scans to aid in the classification and diagnosis of ASD.

Understanding MRI Classification for AI in Autism:

MRI classification for ASD using AI involves the analysis of MRI scans to identify patterns and abnormalities associated with ASD. AI algorithms are trained on large datasets of MRI scans from individuals with and without ASD to learn these patterns. The trained algorithms can then analyze new MRI scans and provide insights to assist clinicians in diagnosing ASD.

Types of MRI Scans Used in ASD Diagnosis:

  • Structural MRI: This type of MRI provides detailed images of the brain’s structure, including the size and shape of different brain regions. Structural MRI scans can reveal abnormalities in brain structure that may be associated with ASD.
  • Functional MRI (fMRI): fMRI measures brain activity by detecting changes in blood flow. It can show how different brain regions communicate and activate during tasks. fMRI can help identify differences in brain function between individuals with and without ASD.

source

The Rise of AI in MRI Classification: A Powerful Partnership

AI, particularly machine learning (ML) algorithms, are trained on large datasets of MRI scans from both individuals with and without ASD. These algorithms learn to identify patterns in the scans that are subtle to the human eye but hold significant diagnostic potential.

Here’s how AI-powered MRI classification works:

  • Feature Extraction: The AI analyzes the MRI scans, extracting relevant features like brain region volume, white matter integrity, or specific activation patterns during tasks.
  • Classification: Based on the extracted features, the AI model distinguishes between scans with and without ASD signatures.

The Promise and Challenges of AI-Based Diagnosis

Research suggests promising results, with studies reporting accuracy rates exceeding 90% for identifying ASD using AI and MRI. This paves the way for:

  • Earlier diagnosis: Early intervention is crucial for improving developmental outcomes in ASD. AI can potentially lead to earlier and more objective diagnosis, allowing for timely intervention strategies.
  • Improved objectivity: AI removes human bias from the diagnostic process, potentially leading to more consistent and reliable assessments.
  • Personalized medicine: AI could help tailor interventions by identifying specific brain patterns associated with individual needs and strengths.

However, significant challenges remain:

  • Data limitations: Training AI models requires large and diverse datasets, which are currently limited in the field of ASD research.
  • Interpretability: Understanding how AI models reach their conclusions is crucial for building trust and ensuring ethical use in healthcare settings.
  • Ethical considerations: Ensuring fairness, privacy, and transparency are crucial throughout the development and implementation of AI-based diagnostic tools.

Intel oneAPI

Intel oneAPI is a unified programming model that simplifies the development of applications for heterogeneous architectures. It provides a single set of tools and libraries that developers can use to target a wide range of hardware platforms. Intel has been actively involved in optimizing popular machine learning frameworks like TensorFlow and XGBoost to leverage the performance benefits of Intel architecture, including CPUs and accelerators like Intel Xeon processors and Intel FPGAs. These optimizations aim to improve the speed and efficiency of machine learning workloads, enabling developers to train and deploy models faster and more cost-effectively.

In this blog we have used the Intel oneAPI to show how efficiently we can train and infer models using it.

Dataset

The ABIDE (Autism Brain Imaging Data Exchange) dataset is a collection of MRI (Magnetic Resonance Imaging) and phenotypic data from individuals with autism spectrum disorder (ASD) and typically developing controls. It was created to facilitate research on the neuroimaging aspects of ASD and to provide a standardized dataset for the development and validation of computational models for ASD diagnosis and prognosis.

from nilearn import datasets
from nilearn.input_data import NiftiLabelsMasker
from nilearn.connectome import ConnectivityMeasure
from argparse import ArgumentParser
import numpy as np
from sklearn.decomposition import PCA
import os
import pandas as pd

def prepare_data(data_dir, output_dir, pipeline = "cpac", quality_checked = True):
# get dataset
print("Loading dataset...")
abide = datasets.fetch_abide_pcp(data_dir = data_dir,
pipeline = pipeline,
n_subjects = 10,
quality_checked = quality_checked)
# make list of filenames
fmri_filenames = abide.func_preproc

# load atlas
multiscale = datasets.fetch_atlas_basc_multiscale_2015()
atlas_filename = multiscale.scale064

# initialize masker object
masker = NiftiLabelsMasker(labels_img=atlas_filename,
standardize=True,
memory='nilearn_cache',
verbose=0)

# initialize correlation measure
correlation_measure = ConnectivityMeasure(kind='correlation', vectorize=True,
discard_diagonal=True)

try: # check if feature file already exists
# load features
feat_file = os.path.join(output_dir, 'ABIDE_BASC064_features.npz')
X_features = np.load(feat_file)['a']
print("Feature file found.",X_features.shape)

except: # if not, extract features
X_features = [] # To contain upper half of matrix as 1d array
print("No feature file found. Extracting features...")

for i,sub in enumerate(fmri_filenames):
# extract the timeseries from the ROIs in the atlas
time_series = masker.fit_transform(sub)
# create a region x region correlation matrix
correlation_matrix = correlation_measure.fit_transform([time_series])[0]
# add to our container
X_features.append(correlation_matrix)
# keep track of status
print('finished extracting %s of %s'%(i+1,len(fmri_filenames)))
# Save features
np.savez_compressed(os.path.join(output_dir, 'ABIDE_BASC064_features'),
a = X_features)

# Dimensionality reduction of features with PCA
# print("Running PCA...")
# pca = PCA(0.99).fit(X_features) # keeping 99% of variance
# X_features_pca = pca.transform(X_features)

# Transform phenotypic data into dataframe
abide_pheno = pd.DataFrame(abide.phenotypic)

# Get the target vector
y_target = abide_pheno['DX_GROUP']

return(X_features, y_target)

#Define data and output directories
data_dir = 'data/'
output_dir = data_dir

X, y = prepare_data(data_dir,output_dir)

X= np.array(X)
y = np.array(y)
X = np.reshape(X,(X.shape[0],42,-1))
y=y-1
Xx = np.stack([X,X,X],axis=3)
Xx = np.resize(Xx,(Xx.shape[0],224,224,3))

Model Building

Here we will be using a multistacked model using Resnet and XgBoost

import tensorflow as tf
from keras import Model, Sequential
from keras.layers import Layer
import keras.layers as nn
import keras

base_cnn = keras.applications.resnet50.ResNet50(
include_top=True,
weights='imagenet',
)
model_cnn_sup = Sequential([
base_cnn,
nn.Dense(1,activation='sigmoid')
])
model_cnn_sup.compile(loss='bce',optimizer='adam',metrics=['accuracy'])
model_cnn_sup.fit(Xx,y,epochs=5,verbose=1,batch_size=1)


XX = base_cnn.predict(Xx)

We extract the features using the Resnet and then train our xgboost model using these features. We also showcase the difference in times with and without intel optimisation.

import sklearn
from sklearnex import patch_sklearn, unpatch_sklearn
patch_sklearn()
from pandas import MultiIndex, Int16Dtype # if you don't import in this order you will get a pandas.Int64Index fix for FutureWarning error.
import xgboost as xgb
from time import perf_counter

xgb_params = {
'objective': 'binary:logistic',
'predictor': 'cpu_predictor',
'disable_default_eval_metric': 'true',
}

# Train the model
warnings.simplefilter(action='ignore', category=UserWarning)
t1_start = perf_counter() # Time fit function
model_xgb= xgb.XGBClassifier(**xgb_params)
model_xgb.fit(XX,y[:,0].astype(int))
t1_stop = perf_counter()
print ("It took", t1_stop-t1_start," to fit.")

t1_start = perf_counter() # Time fit function
model_xgb.predict(XX)
t1_stop = perf_counter()
print ("It took", t1_stop-t1_start," to fit.")

Now without the optimisation

unpatch_sklearn()

xgb_params = {
'objective': 'binary:logistic',
'predictor': 'cpu_predictor',
'disable_default_eval_metric': 'true',
}

# Train the model
warnings.simplefilter(action='ignore', category=UserWarning)
t1_start = perf_counter() # Time fit function
model_xgb= xgb.XGBClassifier(**xgb_params)
model_xgb.fit(XX,y[:,0].astype(int))
t1_stop = perf_counter()
print ("It took", t1_stop-t1_start," to fit.")

t1_start = perf_counter() # Time fit function
model_xgb.predict(XX)
t1_stop = perf_counter()
print ("It took", t1_stop-t1_start," to fit.")

Demo

We also have a huggingface demo. Click here.

Conclusion:

MRI classification for ASD using AI has the potential to revolutionize the diagnosis and management of ASD. By leveraging the power of AI to analyze MRI scans, clinicians can more accurately and efficiently diagnose ASD, leading to improved outcomes for individuals with ASD. Continued research and development in this field will help unlock the full potential of AI in autism diagnosis and treatment.

References:

--

--

Usha Rengaraju

Chief of Research at Exa Protocol | Autism Advocate | Corporate Trainer | Adjunct Faculty