This project looks at comparing the machine learning methods Residual Neural Network (ResNet50) and K-Nearest Neighbours (KNN) for classifying real patient X-rays of pneumonia. Some information about our project as well as the finished model (which can be tested by uploading an image of an X-ray) can be found on the website: ml.andersf.no.
Structure
To get started, download this repo either by cloning it from your IDE, or download it as a zip. The majority of methods can be found in ML.py and KNN_classifier.py.
The images used for training, should be put in the respective folders described in the readme-files found in the data folders.
Both of the main py-files contain a main method at the bottom of the files, and can be run as is.
methods from ML.py and KNN_classifier.py
ML.py:
Make predictions on a single image.
Creates the data used for the model. Then creates the model for the ResNet50, and trains it for 2-4-20-10 epochs. To save the model, uncomment the learn.save() line.
creates a confusion matrix for the learner.
takes in the saved model from the folder specified in the train method, and loads it back, to be used without re-training.
KNN_classifier.py:
Takes the images from the x-ray folder, and creates test, train and validation sets to be used for the model.
saves the model to be used later.
loads the model saved by the save_model method to be used for classifyinfg
plots a graph showing what the best K-value is for the model.
from fastai.vision import * import numpy as np import pickle import torch import torch.nn as nn from torch.utils.data import Dataset from torchvision import transforms, datasets import torchvision import numpy as np from sklearn.neighbors import KNeighborsClassifier from sklearn.metrics import * import matplotlib.pyplot as plt
- Anders Fredriksen
- Marianne Pettersen
- Elise Bø