DementiDetect: teaching a neural network to read brain scans
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.
What the model looks at

No dementia

Early-stage Alzheimer's
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:
- Images that loaded as almost all zeros. My first datasets were tiny, heavily compressed files of about 5 KB each. After checking pixel ranges and image shapes batch by batch, I replaced them with larger, cleaner scans.
- A model that only ever gave one answer. I had 181 scans in one class and about 360 in the other, and the network learned to predict a single class for everything. I collected more data until the classes were close to even: 531 early-stage and 550 healthy.
- Not knowing which label was which. Keras assigns class numbers alphabetically by folder name, so I printed labelled batches and confirmed that 0 meant early-stage Alzheimer's and 1 meant no dementia before trusting any prediction.

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.
| Layer | Output shape | Parameters |
|---|---|---|
| Conv2D, 16 filters, 3×3, ReLU | 254 × 254 × 16 | 448 |
| MaxPooling2D | 127 × 127 × 16 | 0 |
| Conv2D, 32 filters, 3×3, ReLU | 125 × 125 × 32 | 4,640 |
| MaxPooling2D | 62 × 62 × 32 | 0 |
| Conv2D, 16 filters, 3×3, ReLU | 60 × 60 × 16 | 4,624 |
| MaxPooling2D | 30 × 30 × 16 | 0 |
| Flatten | 14,400 | 0 |
| Dense, 256, ReLU, L2 = 0.01 | 256 | 3,686,656 |
| Dropout, 0.3 | 256 | 0 |
| Dense, 1, sigmoid | 1 | 257 |
| Total | 3,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:
- Twice as much data, with the two classes balanced.
- L2 regularisation on the dense layer, which penalises large weights.
- Dropout of 30%, so the network cannot lean on any single unit.
- Early stopping: training halts when validation loss has not improved for five epochs, and the best weights are restored.
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.


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.
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
- Data. About a thousand scans is very little for a medical problem, and all of it came from public collections.
- Compute. One laptop, which ruled out larger networks and longer experiments.
What I would do next
- Split the data by patient, not by image, so slices from one person's brain can never appear in both training and validation.
- Report precision and recall alongside accuracy. In screening, a missed case costs far more than a false alarm.
- Combine the scan with other indicators of Alzheimer's instead of relying on one image.
- Put it behind a simple website, so a scan can be uploaded and checked in seconds.
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.