This project implements a deep learning solution for multi-label classification of chest X-ray images using the NIH ChestXray14 dataset. The implementation uses the ConvNext architecture through the timm library with options for transfer learning.
The NIH Chest X-ray Dataset consists of 112,120 X-ray images from 30,805 unique patients with 14 disease labels:
- Atelectasis
- Cardiomegaly
- Effusion
- Infiltration
- Mass
- Nodule
- Pneumonia
- Pneumothorax
- Consolidation
- Edema
- Emphysema
- Fibrosis
- Pleural Thickening
- Hernia
Inside your virtual environment, run the following:
pip install -r requirements.txt.
├── config.py # Configuration and argument parsing
├── dataset.py # Dataset class implementation
├── loss.py # Focal Loss implementation
├── metrics.py # Evaluation metrics
├── trainer.py # Training loop implementation
├── main.py # Main execution script
└── README.md
Basic usage:
python main.pyWith pretrained weights:
python main.py --pretrained| Argument | Default | Description |
|---|---|---|
--pretrained |
False | Use pretrained weights |
--batch_size |
64 | Batch size for training |
--num_workers |
4 | Number of data loading workers |
--max_train_samples |
80000 | Maximum training samples |
--max_test_samples |
20000 | Maximum test samples |
--num_epochs |
30 | Number of training epochs |
--learning_rate |
0.0001 | Learning rate |
--weight_decay |
0.01 | Weight decay for optimization |
Example with multiple arguments:
python main.py --pretrained --batch_size 32 --learning_rate 0.0005 --num_epochs 50The project uses ConvNext Base by default, but you can easily modify the architecture by changing the model in main.py:
model = timm.create_model('convnext_base', pretrained=args.pretrained, num_classes=14)You can replace 'convnext_base' with other architectures available in timm. Some options include:
convnext_tinyconvnext_smallconvnext_largeswin_tiny_patch4_window7_224vit_base_patch16_224efficientnet_b0
To see all available models:
import timm
print(timm.list_models('*'))For detailed model specifications, refer to the timm documentation.
- Loss Function: Focal Loss with α=0.25 and γ=2
- Optimizer: AdamW
- Learning Rate Scheduler: ReduceLROnPlateau
- Early Stopping: Implemented with patience=15
- Metrics: Mean AUC-ROC across all classes
- Image Size: 384x384 pixels
- Data Augmentation: Basic normalization (can be extended in transforms)
The best model based on validation AUC is automatically saved as 'best_model.pth'.
During training, the script outputs:
- Epoch progress with loss
- Current AUC score
- Best AUC score achieved
- Learning rate adjustments
- Early stopping notifications
Modify the transform in main.py:
transform = transforms.Compose([
transforms.Resize((384, 384)),
transforms.RandomHorizontalFlip(), # add augmentations here
transforms.RandomRotation(10),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])You can adjust training parameters either through command line arguments or by modifying the defaults in config.py.
- NIH ChestXray14 Dataset: Wang et al. 2017
- ConvNext Paper: Liu et al. 2022
- timm Library: Ross Wightman