X-ray classification using Keras

Aron Szeles
2 min readJan 4, 2021
Photo by CDC on Unsplash

I had a sudden impulse during the Christmas break to learn more about computer vision, so I started browsing Kaggle in search of a worthy dataset. The classic ones like MNIST and CIFAR10 were not motivating enough, so I ended up using the chest X-ray dataset.

I had a comfortable amount of knowledge about computer vision and CNNs, so I felt confident. Well, after training for half an hour the accuracy stuck at 84% with a constant loss. Thus, I started to investigate.

Turns out this dataset is quite imbalanced — 75, 25. So I started debugging, using precision and recall as metrics instead of accuracy. Turns out the model diagnosed everyone with pneumonia, which is definitely not what I wanted.

I started to change the model trying to balance the precision and recall, fine-tuning, using learning rate decay and applying some level of randomness to the training data. TBH, Keras makes all this incredibly easy.

Keras implementation of importing and randomizing the data

I built a CNN with three convolutional layers, 64 filters (297,241 parameters) each while taking advantage of dropout and BatchNorm and, of course, my optimizer of choice was Adam (reasoning: https://arxiv.org/abs/2007.01547).

I also tried using early stopping, but the gradient updates were very strange sometimes, so I ended up babysitting the model.

After a short training, the model showed 89% accuracy on the held-out dataset, with an F1 score of 0.9. I could have bumped the accuracy well above 90%, but I did not have the resources to do so, I was constrained to using google collab, and due to my non-reliable internet connection that was not an option, but I might come back to the problem later.

The full code and the model are available on my GitHub, including the local notebook version and the collab version.

--

--