Knee OA Analysis with X-ray Images, Deep Learning & Streamlit

fernanda rodríguez
4 min readJan 18, 2023

--

Web app to predict knee osteoarthritis grade using Deep Learning and Streamlit

Photo by National Cancer Institute on Unsplash

Introduction

Knee osteoarthritis is a pathology that occurs due to wear on the cartilage that protects the bones in this region from friction and impacts.

Some medical procedures are necessary to identify this pathology, such as X-rays or magnetic resonance imaging, in which it is possible to assess the loss in joint spacing, thus indicating the severity of the disease.

The severity of osteoarthritis was classified into 5 levels based on KL score, from the healthy level to the severe level, where the greater the degree of severity, the smaller the spacing of the joint.

KL score:

  • Grade 0: Healthy
  • Grade 1: Doubtful
  • Grade 2: Minimal
  • Grade 3: Moderate
  • Grade 4: Severe

The following image shows the different levels from Knee Osteoarthritis Dataset with Severity Grading.

Knee Osteoarthritis Dataset with Severity Grading

Purpose

The purpose of this project is to correctly classify the severity of osteoarthritis based on X-ray.

Streamlit Web App

Methodology

The following methodology has been proposed to correctly classify the degree of osteoarthritis based on X-ray images:

  1. Data preparation
  2. Model training
  3. Model evaluation
  4. Web application development with the best model

1. Data preparation

The dataset consisting of 8000 X-ray images, approximately, of the knee obtained from the Knee Osteoarthritis Dataset.

Data distribution

In the bar chart we can see the image distribution of the 5 grades (classes), for each of the training, validation and test datasets, and in the pie chart we can see the average percentage of data we have for each class. So we have an unbalanced dataset.

Three strategies were implemented to reduce the impact that the unbalanced base can have on the models:

  • class weight
  • data augmentation (horizontal_flip, brightness_range, width_shift_range, zoom_range)
  • preprocessing features of pre-trained networks

See 01_data_preparation.ipynb notebook.

2. Model training

Pre-trained Networks

Three pre-trained networks were chosen: Xception, ResNet-50 e Inception Resnet v2.

The following table presents the summary of the partial results obtained in the implementation of the different pre-trained networks with fine tuning. Our metrics is Balanced Accuracy. Models were trained on Apple M1 Pro chip with 8-core CPU, 14-core GPU and 16-core Neural Engine.

Confusion Matrix Pre-trained Models

Highlighting, the highest success rate of each model by class, we have:

  • Inception ResNet classified the minimal class better
  • Xception classified the doubtful and severe classes better
  • ResNet50 classified the healthy and moderate classes better

See 02_model_inception_resnet_v2.ipynb

See 02_model_xception.ipynb

See 02_model_resnet50.ipynb

Ensemble

Three ensemble approaches were performed with the previous results:

  • One calculating the average, and two calculating the weighted average of the accuracy and f1 of each class.
Confusion Matrix Ensemble models

The three models had similar results, but we selected the ensemble with f1 model.

See 02_ensemble_models.ipynb

Model evaluation

We evaluated the best model in the test set, a balanced accuracy of 71% was obtained, and in the confusion matrix we can observe the highlight of the moderate and severe classes.

Confusion Matrix with Test data

Explainability

We implemented the Grad-CAM explainability technique to better understand how classes are classified. The Grad-CAM indicates the parts of the image that most impact the classification score.

We can see in the images that for the healthy, doubtful and minimal classes, the most prominent areas are located in the center of the knee, and the moderate and severe classes are most prominent on the right or left edges of the knee.

Grad-CAM results were obtained from the last convolutional layer of the Xception model.

Severe class

See 03_best_model_on_test_xception.ipynb

Web application development with the best model

The web application allows you to select and load an X-Ray image, to later predict and evaluate the loss in joint spacing, and indicate the probability of disease severity, as well as the area that most impacted the classification score.

Web App on Streamlit

Project Setup

--

--

fernanda rodríguez

hi, i’m maría fernanda rodríguez r. multimedia engineer. data scientist. front-end dev. phd candidate: augmented reality + machine learning.