X-ray Bone Fracture Detection: Enhancing Medical Diagnosis with Convolutional Neural Networks (CNN)

Danish Javed
Towards AI
Published in
5 min readOct 10, 2023

--

CNN model to predict if bone is broken or not
GUI for model

Convolutional Neural Networks(CNNs):

Convolutional Neural Networks are a type of artificial neural network commonly used in deep learning. CNN architecture consists of multiple layers, which are the input layer, convolutional layers, pooling layers, fully-connected (FC) layers, and output layers. The input layer takes an image as input, the convolutional layer extracts features from the input images, the pooling layer reduces computation by downsizing the image, and finally, the fully connected layer makes predictions, which are shown by the output layer.

Fracture detection using CNN:

CNN model building for bone fracture detection consists of the following steps:

1. Data-set Collection:

Data-set could be chosen from different sources, link to data set that is being used in this method is given below.
https://mega.nz/file/zcdywLhI#fck4ufXy_o_Uiu0vGqh-cZiKHw5Xe_n4M2qWUWSheAI

2. Pre-Processing:

Pre-processing is one of the main parts before model building and training to make the dataset best fit for model preprocessing in this project includes the following steps.
1. ImageDataGenerator Configuration: Two instances of ImageDataGenerator are created, one for training data and another for testing data.
2. Training Data Augmentation: Augmentation includes the following step

  • rescale=1.0/255 normalizes pixel values to [0, 1]
  • hear_range=0.2 random shear transformations involve tilting the image along one axis, adding diversity to the dataset.
  • zoom_range=0.2 applies random zoom changes during training, introducing object scale variations in the images.
  • horizontal_flip=True: Images are horizontally flipped randomly.

3. Testing Data Normalization: rescale=1.0/255 Similar to the training data, pixel values of testing data are normalized to the range [0, 1].
4. Data Loading: The flow_from_directory function loads data from directories, setting image size to 150x150, batch size to 32, and class mode to binary, indicating two classes (e.g., fractured and non-fractured).

# Create an ImageDataGenerator for data augmentation and normalization
train_datagen = ImageDataGenerator(
rescale=1.0/255, # Normalize pixel values between 0 and 1
shear_range=0.2, # Apply random shear transformations
zoom_range=0.2, # Apply random zoom transformations
horizontal_flip=True) # Flip images horizontally

# Only normalize pixel values for testing
test_datagen = ImageDataGenerator(rescale=1.0/255)

# Load and augment training data
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')

# Load and normalize testing data
test_generator = test_datagen.flow_from_directory(
test_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')

3. CNN Model building:

CNN sequential model was built using TensorFlow keras. Model architecture consists of the following layers
1. Convolutional 2D layer is the first layer with 32 filters of size 32x32, ReLU (Rectified Linear Unit) activation function, and dimensions of the input image are 150x150 pixels with 3 channels.
2. Max-Pooling (2x2) layer follows the convolutional layer, downsizing feature maps and emphasizing vital features by extracting the most important details.
3. Flatten Layer maps 2D features into a 1D vector to connect convolutional layers to fully connected layers in the model.
4. Fully Connected Layers include the following:

  • The first fully connected layer has 128 neurons and ReLU activation. It processes the flattened feature vectors and extracts higher-level features.
  • The second and final fully connected layer has single neuron and sigmoid activation. this layer produced binary output to predict the class of image.
# Build the model
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(32, (3, 3), activation='relu',
input_shape=(img_width, img_height, 3)),
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])

4. Model Compilation:

model.compile configures the model for training. It specifies the optimizer (in this case, 'adam'), the loss function ('binary_crossentropy' for binary classification), and the evaluation metric ('accuracy').

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

5. Model Training:

  1. The model is trained using the fit method. It takes the train_generator (which contains the training data) and specifies the number of training epochs. The model will learn from the training data, and its performance is improved through backpropagation and gradient descent.
# Train the model
model.fit(train_generator, epochs=epochs, callbacks=[tensorboard_callback])

Overall, this code constructs a CNN model with convolutional, pooling, and fully connected layers. It compiles the model with appropriate settings for training, including the choice of optimizer and loss function. Finally, it initiates the training process, allowing the model to learn from the provided X-ray data to classify fractured and non-fractured bones.

6. Testing & Deployment:

  1. Define the Classification Function:
  • classify_image(img): This function takes an input image and performs the following steps:
  • Resize the input image to match the model’s input shape (150x150 pixels).
  • Converts the image to a numpy array and normalizes the pixel values.
  • Expands the dimensions to match the model’s expected input shape.
  • Uses the pre-trained model to predict whether the X-ray image is “fractured” or “NOT fractured” based on the model’s output.

2. Create a Gradio Interface:

  • gr.Interface: This creates a Gradio interface for model. The key parameters include:
  • fn=classify_image: The function responsible for making predictions.
  • inputs=gr.inputs.Image(type="pil", label="Upload an X-ray image"): The input component for uploading an X-ray image. It uses the PIL (Pillow) image type.
  • outputs="text": The output component, which displays the classification result as text.
  • title="Bone Fracture Classification": The title for your Gradio interface.
  • description="Upload an X-ray image, and this model will classify it as fractured or not.": A description explaining the interface's purpose.
  • iface.launch(): This line of code initiates the Gradio interface, making it accessible via a web browser. Users can upload X-ray images, and the pre-trained model will classify them as "fractured" or "NOT fractured" based on the predictions.
import numpy as np
from tensorflow.keras.preprocessing import image
import gradio as gr
import numpy as np
from tensorflow.keras.preprocessing import image
import gradio as gr
import keras

# Define the function for image classification
def classify_image(img):
# Set the input image dimensions
img_width, img_height = 150, 150

# Resize the image to match the model's input shape
img = img.resize((img_width, img_height))

# Convert the image to a numpy array
img = np.array(img)
img = img.astype('float32') / 255.0
img = np.expand_dims(img, axis=0)

# Get the prediction
prediction = model.predict(img)

return "NOT fractured" if prediction > 0.5 else "fractured"

# Create a Gradio interface
iface = gr.Interface(
fn=classify_image,
inputs=gr.inputs.Image(type="pil", label="Upload an X-ray image"),
outputs="text",
title="Bone Fracture Classification",
description="Upload an X-ray image, and this model will classify it as fractured or not.",
)

# Start the Gradio interface
iface.launch()

This is how the X-Ray basic Bone fracture detection CNN model can be built using Tensor flow Keras.
For complete code visit my github.

--

--