Skip to content

Repository files navigation

Anatomical masking for chest X-ray classification

This repository contains the author-generated deep-learning workflow supporting the study “Anatomical masking modifies the trade-off between information retention and domain generalization.” The study compares three preprocessing strategies for binary chest X-ray classification:

  1. no masking;
  2. lung-field masking; and
  3. whole-thorax masking.

The same Xception-based classifier design was used for all three strategies. Performance was evaluated on internal NIH ChestX-ray14 data and externally on PadChest.

Study workflow

  1. Train a U-Net model for whole-thorax segmentation.
  2. Train a U-Net model for lung-field segmentation.
  3. Apply the thorax mask, lung mask, or no mask to the chest radiographs.
  4. Apply the remaining preprocessing steps used by the classification pipeline.
  5. Train 20 independent normal-versus-pathological classifiers for each masking strategy.
  6. Evaluate the three strategies in internal and external validation.

No pathology-specific classifiers were trained. Pathology-stratified results were obtained by evaluating the same binary classifier within each radiological subgroup.

Data

NIH ChestX-ray14

NIH ChestX-ray14 contains 112,120 frontal chest radiographs from 30,805 patients with 14 pathology labels and a No Finding category.

For whole-thorax segmentation, 500 NIH images were manually delineated. Four hundred images were used for training and 100 for testing. Training-set augmentation generated 800 additional samples, for a total of 1,200 training instances.

For classification, NIH ChestX-ray14 was used for model development and internal validation.

Lung segmentation dataset

The lung segmentation model was trained using 21,165 image-mask pairs from the public Chest X-ray Dataset:

  • 10,192 normal images;
  • 3,616 COVID-19 images;
  • 6,012 images with lung opacity; and
  • 1,345 pneumonia images.

Clinical validation of thorax segmentation

Thorax masks were clinically assessed using 585 images drawn from NIH ChestX-ray14, the JSRT dataset, and the Montgomery County dataset.

PadChest external validation

External validation used 110,633 eligible PadChest images with PA, AP, or AP_horizontal projections.

Because NIH ChestX-ray14 and PadChest use different labeling ontologies, clinically compatible pathology categories were harmonized. Of the 110,633 PadChest images:

  • 84,087 had at least one label compatible with the NIH ChestX-ray14 ontology;
  • 26,546 had no compatible label.

All 110,633 images were retained in the global external validation analysis. The 26,546 images without a compatible label were excluded only from pathology-specific analyses and the pathology-adjusted mixed-effects model.

The source datasets are not redistributed in this repository. Users must obtain them from their original providers and comply with the corresponding terms of use.

Models and preprocessing

Segmentation

Both anatomical masks were generated with U-Net-based segmentation models. Whole-thorax model development compared a simple U-Net, a custom-loss model, and an EfficientNet-based U-Net. The final thorax model used the simple U-Net implementation with Dice loss.

Classification

The binary classifier distinguishes normal from pathological radiographs. Images are converted to grayscale and resized to 512 × 512 pixels. Depending on the experimental condition, a lung mask, thorax mask, or no mask is applied, followed by contrast-limited adaptive histogram equalization and z-score normalization.

The classifier uses an ImageNet-pretrained Xception backbone with additional convolutional, pooling, and dense layers. Binary cross-entropy and the Adam optimizer were used for training.

Repository structure

  • execute_thorax_training.py: whole-thorax segmentation training.
  • execute_lung_training.py: lung segmentation training.
  • apply_mask_model.py: application of a trained segmentation model to an image directory.
  • classification_masks/: preprocessing, classifier training, and prediction scripts.
  • image_functions/: image loading, normalization, augmentation, and mask-processing functions.
  • model_functions/: segmentation architectures, losses, evaluation, and training utilities.
  • results/: stored result tables included with the repository.
  • requirements.txt: Python package versions recorded for the 2022 computational environment.

Python environment

The analyses were developed in 2022. The exact Python interpreter version was not recorded in the repository, but the package versions used for the computational workflow are pinned in requirements.txt. Key versions include:

  • TensorFlow and TensorFlow GPU 2.10.0;
  • Keras 2.10.0;
  • NumPy 1.23.3;
  • pandas 1.5.0;
  • scikit-learn 1.1.2;
  • scikit-image 0.19.3;
  • SciPy 1.9.1;
  • OpenCV 4.6.0.66; and
  • Albumentations 1.3.0.

Create an isolated environment compatible with TensorFlow 2.10 and install the recorded dependencies:

python -m pip install -r requirements.txt

GPU execution additionally requires a TensorFlow-compatible NVIDIA/CUDA environment.

Entry points

The main scripts expose command-line options. Inspect the available parameters before running them:

python execute_thorax_training.py --help
python execute_lung_training.py --help
python apply_mask_model.py --help
python classification_masks/execute_classification_training.py --help
python classification_masks/execute_comparation_prediction.py --help

Dataset paths and metadata CSV files must be prepared locally. The code retains the directory conventions used during the original study, so users may need to adapt local paths while preserving the preprocessing and model settings described above.

Code availability and license

The code is publicly available without account or access restrictions in this repository and is distributed under the MIT License.

For questions about the study or computational workflow, please use the repository issue tracker.

About

Segmentation of the thoracic space in CXR

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages