/brain-tumor-classifier

Brain tumor classifier for HKR Machine Learning course

Primary LanguageJupyter Notebook

Brain Tumor Classification with Convolutional Neural Networks

This repository contains code for classifying different types of brain tumors using a Convolutional Neural Network (CNN) architecture called ResNet50. It also includes a web service built with FastAPI for real-time inference.

Table of Contents

Datasets used

The data used for training and testing the model is a combination of the following two datasets:

Prerequisites

  • Python 3.8 or higher
  • pip
  • Virtual Environment (recommended)

Setup

  1. Clone the Repository: Clone this repository to a folder of your choice.

    git clone https://github.com/adisve/brain-tumor-classifier.git
  2. Navigate to the Project Folder: Move into the cloned project directory.

    cd brain-tumor-classifier
  3. Virtual Environment (Recommended): It's often best to create a virtual environment to isolate package dependencies. To create a virtual environment, run the following command:

    python3 -m venv .venv

To activate the virtual environment, run:

  • Linux/Mac:

    source .venv/bin/activate
  • Windows:

    .venv\Scripts\activate

Install Dependencies

Install the necessary packages by running the following command:

pip install .

This command reads the pyproject.toml file and installs all dependencies.

Web Service

The web service is built using FastAPI and provides real-time inferences from the trained model.

Running the Service

To run the web service on your local machine, navigate to the server/ directory and execute:

uvicorn api:app --reload

This will start the FastAPI server and you can access the API documentation at http://127.0.0.1:8000/docs.

API Endpoints

  • Predict: POST /predict/
    • Accepts an MRI image and returns the type of brain tumor.

For detailed documentation, refer to the FastAPI generated documentation at http://127.0.0.1:8000/docs.

Model Training and Testing

To run the model on your local machine, navigate to the model/ directory and open the Jupyter Notebook file.

Training

The model uses the following Keras callbacks during training:

  • EarlyStopping: To stop training early if no improvement in validation loss.
  • ReduceLROnPlateau: To reduce learning rate when a metric has stopped improving.
  • ModelCheckpoint: To save the model after every epoch.
  • LambdaCallback: Custom callback for additional functionalities (here, displaying the confusion matrix).

Evaluation

Run the model by executing the Jupyter Notebook. Metrics such as loss, accuracy, and AUC (Area Under the Curve) will be displayed at the end, along with interesting graphs.