This project uses a Variational Autoencoder (VAE) with Structural Similarity Index Measure (MSSIM) loss to generate realistic chest X-ray images based on the PyTorch-VAE implementation.
The code assumes the chest X-ray dataset is structured as follows:
chest_xray/
- chest_xray/
- test/
- NORMAL/
- PNEUMONIA/
- train/
- NORMAL/
- PNEUMONIA/
- val/
- NORMAL/
- PNEUMONIA/
Install all requirements from the original PyTorch-VAE repository:
pip install -r requirements.txtAdditional requirements for evaluation with PyTorch-Ignite:
pip install -r ignite_requirements.txtWe provide shell scripts that automatically set up a virtual environment with all required packages:
To train the model in a virtual environment:
./train_with_venv.shThis script will:
- Create a Python virtual environment in the
venvdirectory (if it doesn't exist) - Install all required packages
- Run the training script
- Deactivate the virtual environment when complete
To evaluate the trained model in a virtual environment:
./run_with_venv.shThis script will:
- Create a Python virtual environment in the
venvdirectory (if it doesn't exist) - Install all required packages
- Run the evaluation script
- Deactivate the virtual environment when complete
To train the model using the custom Python script:
python chest_xray_vae.pyThis will train the MSSIM-VAE model with the default parameters defined in the script.
To train the model using the original PyTorch-VAE training script with our custom config:
python run.py -c configs/chest_xray_mssim_vae.yamlAfter training, use the evaluation script to generate new chest X-ray images and calculate FID and Inception scores using PyTorch-Ignite:
python evaluate_vae.py --checkpoint logs/ChestXray_MSSIM_VAE/version_X/checkpoints/last.ckpt --output_dir generated_imagesReplace version_X with the appropriate version number from your training run, and adjust other parameters as needed:
--checkpoint: Path to the trained model checkpoint--output_dir: Directory to save generated images (default: 'generated_images')--num_samples: Number of samples to generate (default: 1000)--device: Device to use (default: 'cuda' if available, otherwise 'cpu')--seed: Random seed for reproducibility (default: 42)
The evaluation script uses PyTorch-Ignite's implementation of FID (Fréchet Inception Distance) and Inception Score metrics to assess the quality of the generated images. These metrics are based on features extracted by a pre-trained Inception v3 model:
-
FID: Measures the distance between feature distributions of real and generated images. Lower values indicate better quality and more similarity to the real data distribution.
-
Inception Score: Measures both the quality and diversity of generated images. Higher values indicate better performance.
The evaluation process includes:
- Resizing images to 299x299 (required by Inception v3)
- Using PIL's BILINEAR interpolation for better quality
- Running the metrics on batches of real and generated images
For the assignment, include the following in your report:
- Hyperparameter settings: Include the values from
configs/chest_xray_mssim_vae.yaml - Training loss curves: Get these from TensorBoard logs at
logs/ChestXray_MSSIM_VAE/version_X - Inception score and FID score: Get these from the output of the evaluation script
- Generated image examples: Include images from the
generated_imagesdirectory
To view training progress and results:
tensorboard --logdir logs/ChestXray_MSSIM_VAEThe MSSIM-VAE uses a structural similarity index measure loss function, which is particularly well-suited for medical images. Unlike pixel-wise loss functions, SSIM focuses on preserving structural information, which is critical for maintaining diagnostically relevant features in medical imaging.