← Jesse Ifrah

DementiDetect: teaching a neural network to read brain scans

Science fair project · 2024–2025 · Third place, Montreal Regional Science and Technology Fair · Quebec provincial nominee

Before she passed away, my grandmother lived with Alzheimer's for many years. What stayed with me was how late everything happened. There is no cure, so the best anyone can do is catch it early and slow it down, and yet most people who have it are never diagnosed. A scan is expensive, a diagnosis can take weeks, and telling an early-stage brain from a healthy one is hard even for an experienced doctor.

That is a pattern-recognition problem on images, which is what convolutional neural networks are good at. So I learned how to build one. DementiDetect takes an MRI slice of a brain and classifies it as showing early-stage Alzheimer's or no dementia.

1,081MRI scans in the final dataset
3.7Mtrainable parameters
92%validation accuracy, as presented
~80%on scans from datasets it had never seen

What the model looks at

An axial MRI slice of a brain with no dementia

No dementia

An axial MRI slice of a brain with early-stage Alzheimer's

Early-stage Alzheimer's

Two scans from the test images. As neurons die the brain shrinks and the fluid-filled spaces in the middle grow, but at an early stage the difference is subtle.

Alzheimer's is thought to begin with amyloid and tau proteins building up in the brain. Plaques form, neurons slow down and stop working, and unused tissue wastes away. In a textbook diagram of a severe case the damage is obvious. In a real early-stage scan it is not, and that gap is where a model can help.

Getting the data right

This took longer than the model. The dataset is built from public MRI collections on Kaggle, including slices derived from the Alzheimer's Disease Neuroimaging Initiative. Three problems came up before anything trained properly:

Four MRI brain scans from one training batch
Four scans from one training batch. Every image is resized to 256×256 and its pixel values are scaled from 0–255 down to 0–1 before training.

The network

Three convolution blocks find patterns at increasing scale, a dense layer combines them, and a single sigmoid output gives a number between 0 and 1.

256×256×3 scan Conv 16 Conv 32 Conv 16 Flatten 14,400 Dense 256 Sigmoid
LayerOutput shapeParameters
Conv2D, 16 filters, 3×3, ReLU254 × 254 × 16448
MaxPooling2D127 × 127 × 160
Conv2D, 32 filters, 3×3, ReLU125 × 125 × 324,640
MaxPooling2D62 × 62 × 320
Conv2D, 16 filters, 3×3, ReLU60 × 60 × 164,624
MaxPooling2D30 × 30 × 160
Flatten14,4000
Dense, 256, ReLU, L2 = 0.012563,686,656
Dropout, 0.32560
Dense, 1, sigmoid1257
Total3,696,625

Almost all of the parameters sit in the one dense layer, because it connects 14,400 flattened values to 256 units. That is also where a small dataset gets memorised, which is why the regularisation is placed there.

model = Sequential()
model.add(Conv2D(16, (3,3), 1, activation='relu', input_shape=(256,256,3)))
model.add(MaxPooling2D())
model.add(Conv2D(32, (3,3), 1, activation='relu'))
model.add(MaxPooling2D())
model.add(Conv2D(16, (3,3), 1, activation='relu'))
model.add(MaxPooling2D())
model.add(Flatten())
model.add(Dense(256, activation='relu', kernel_regularizer=l2(0.01)))
model.add(Dropout(0.3))
model.add(Dense(1, activation='sigmoid'))

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

early_stopping = EarlyStopping(monitor='val_loss', patience=5,
                               restore_best_weights=True)
history = model.fit(train, validation_data=val, epochs=50,
                    callbacks=[early_stopping])

100% accuracy was the warning sign

My first working version, trained on a few hundred images for 20 epochs, reported 100% accuracy and a loss of almost zero. It then failed on scans from anywhere else. It had memorised its training set.

Four changes fixed that:

The numbers went down and the model got better. Accuracy on its own data dropped from a perfect score to the low nineties, and for the first time it classified scans it had never seen.

Training

Everything ran on a MacBook Air with an M2 chip and 8 GB of memory. The tutorial I started from assumed an Nvidia GPU with CUDA, which a Mac does not have, so I set up TensorFlow 2.16 with Apple's Metal plugin to train on the laptop's own GPU. Each epoch took about two seconds.

The data is split 80% training, 10% validation and 10% test, in batches of 32. The optimiser is Adam and the loss is binary cross-entropy.

Results

The run I presented at the fair stopped itself after 26 epochs with 95.0% training accuracy and 91.7% validation accuracy (loss 0.34, validation loss 0.41). Both curves fall together, which is what a model that is learning rather than memorising looks like.

Loss falling from about 1.8 to about 0.4 over 26 epochs, training and validation together
Accuracy rising from about 0.7 to 0.95 over 26 epochs
Loss and accuracy from the 26-epoch run, as they appeared in my presentation. Teal is training, orange is validation.

Later I trained it further. Early stopping ended the first pass after 11 epochs, and I then let it continue for another 50. It finished at 97.8% training and 97.9% validation accuracy.

training accuracyvalidation accuracy
60%70%80%90%100%1112030405061
Accuracy over 61 epochs, drawn from the training log. The dashed line marks where early stopping ended the first pass.
training lossvalidation loss
0.00.30.60.91.21112030405061
Loss over the same run. The first epoch starts at 3.6, off the top of the chart.

The validation line jumps around for a simple reason: the validation set is three batches, 96 images, so a single scan moving from wrong to right shifts the number by a full point.

The result I trust most is the least flattering one. On scans from completely different datasets, with different scanners and different processing, the model was right about 80% of the time. That is the honest measure of how it would do outside my laptop.

What limited it

What I would do next

The fairs

DementiDetect won my school's science fair, placed third at the Montreal Regional Science and Technology Fair in March 2025, and was nominated to the Quebec provincial final. It is not a diagnostic tool. It is evidence that a student with a laptop and public data can build a first screen that is right four times out of five on scans it has never seen, and that this is worth building properly.