Negative | Typical |
---|---|
- Create conda environment
conda create --name env-name python=3.6.13
*Python 3.6.13 is needed since GDCM is not supported on versions above 3.6
- Clone Github
git clone -v https://github.com/ihamdi/Covid-xRay-Classification.git /your/directory/
or download and extract a copy of the files.
- Install PyTorch according to your machine. For example:
conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch
- Install dependencies from
requirements.txt
file:
pip install -r requirements.txt
- Download Data
Run python /scripts/download_data.py
to download the data using the Kaggle API and extract it automatically. If you haven't used Kaggle API before, please take a look at the instructions at the bottom on how to get your API key.
Otherwise, extract the contents of the "train" directory from the official SIIM-FISABIO-RSNA COVID-19 Detection page to the train
folder inside the data
directory.
bash
directory containsschedule.sh
bash file used to run multiple experiments in sequence.configs
directory contains configuration files used for different experiments, callbacks, datamodules, sweeps, etc.data
directory containstrain
folder where the data is expected to be.scripts
directory containsdownload_data.py
used to download the dataset directly from Kaggle.src
directory contains folders wherecallbacks
,datamodules
,models
, andutils
functions, as well as training, are defined.
Using Pytorch Lightning with the Hydra-Lightning-Template enables us to have experiments with different configurations/hyperparameters on the same dataset or doing a sweep with Optuna:
The experiment
folder inside configs
directory contains a template for configuring an experiment. The easiest way is to make a copy of template.yaml
and edit the parameters accordingly.
If num_classes is set to 4, then the data will be a random mix from all labels. Otherwise, the code will default to binary classification, and the data will be a balanced mix of negative and non-negative labeled images (randomly chosen from the other 3 classes). The program also rejects any data folders with more than 1 xray files to avoid training on lateral chest xrays.
To run the default experiment, run the following command
python train.py
or
python train.py experiment=template
This will run an experiment based on the template using the following configuation:
- 20 epochs (unless early stopping is triggered)
- Torchxrayvision's "ALL" (pretrained Densenet121) model with no Dropout
- Adam optimizer with learning rate of 0.003 and AMSGrad enabled.
- Batch size of 32
- Number of workers of 10
- 640 images only
- 60 : 20 : 20 split
- Image size of 128x128
- IMG-MIN/(MAX-MIN)x255 normalization
- No augmentations
*Torchxrayvision models expect 224 so the code defaults to that automatically if one of them is chosen
As part of the Hydra template, Optuna can be used to find the best hyperparameters within a defined range. A template configuration file can be found within hparams_search
folder inside the configs
directory. The template hyperparameter search can be initiated using
python run.py -m hparams_search=template_optuna experiment=template
or
python run.py -m hparams_search=template_optuna experiment=template hydra.sweeper.n_trials=30
When in binary classification mode, the code is able to produce a model with just over 80% accuracy on the validation data before it starts overfitting:
This was done using the "All" model from torchxrayvision, Adam optimizer, and the following hyperparameters:
- drop_rate (Dropout) = 0
- lr (Learning Rate) = 0.0003
- amsgrad (for Adam) = True
- normal (Normalization Method) = 0 (img-min/(max-min)*255)
- rotation = 11.355
- scaling = 0.2789
- shear = 1
- translation = 0.07357
- horizontal_flip = True
- vertical_flip = True
- dataset_size (Sample Size) = 3350 (maximum possible to keep subset balanced)
- train_val_test_split = [70,20,10]
- batch_size = 156
- num_workers = 20
Although the code accepts setting num_classes=4, it is currently unable to achieve a validation accuracy higher than 60-62% regardless of hyperparameter tuning:
F1 Heatmap & Confusion Matrix below show that it is especially not doing well at classifying xrays with Atypical appearance (label 3):
I am currently investigating whether it is possible to achieve significantly improvement in the performance of the model. Dropout and Augmentations seem to have an adverse effect on the accuracy so I might need to switch to a different architecture altogether.
Initially, this code was based on my Dogs vs Cats code. I eventually adopted the Lightning-Hydra-Template to make it easier to use and log. No submission is made to the Kaggle competition and only the training data is used.
For any questions or feedback, please feel free to post comments or contact me at ibraheem.hamdi@mbzuai.ac.ae
Torchxravision's page on Github (used for Densenet121 models pretrained on xrays).
Pytorch Lightning's page on Github
Lightning Hydra Template's page on Github.
Weights & Biases's website.
Densenet paper by Gao Huang, Zhuang Liu, Laurens van der Maaten, Kilian Q. Weinberger.