TorchXRayVision

raw JSON →
1.4.0 verified Sat May 09 auth: no python

A library for chest X-ray datasets and pretrained models in PyTorch. Version 1.4.0 released Sept 2024. Provides easy access to common datasets (ChestX-ray14, CheXpert, MIMIC-CXR, PadChest, etc.) and pre-trained models for classification, segmentation, and representation learning.

pip install torchxrayvision
error ModuleNotFoundError: No module named 'torchxrayvision'
cause Package not installed or installed in wrong environment.
fix
Run 'pip install torchxrayvision' in the correct Python environment.
error ValueError: unknown url: .../chexpert/CheXpert-v1.0-small.zip
cause The dataset URL has changed or the download link is broken. The remote server may be offline.
fix
Download the dataset manually from the original source and place it in the expected directory, then pass download=False.
error RuntimeError: Image must be a numpy array or PyTorch tensor
cause Input image is of an unsupported type (e.g., PIL Image).
fix
Convert input to a numpy array using np.array(img) or use torch.from_numpy().
breaking In version 1.3.2, automatic upsampling was changed to use interpolate instead of skimage's resize, which may affect reproducibility for models expecting the old preprocessing.
fix If you need the old behavior, manually apply skimage.transform.resize before passing images to models.
gotcha The dataset classes download large files (e.g., CheXpert is ~300GB). Ensure you have sufficient disk space and a stable internet connection.
fix Use the download=False flag and manually place data in the expected location.
gotcha Model weights are automatically downloaded to ~/.cache/torchxrayvision/ by default. This may fill up disk space over time.
fix Set the environment variable XRV_DATA_DIR or use the cache_dir parameter to redirect.

Load a pretrained DenseNet on CheXpert labels, create a PadChest dataset, and run inference on a single image.

import torchxrayvision as xrv

# Load a pretrained model
model = xrv.models.DenseNet(weights='densenet121-res224-chex')

dataset = xrv.datasets.PC_Dataset(
    imgpath='images',
    csvpath='labels.csv',
    views=['PA']
)

# Process an image
import skimage.io
img = skimage.io.imread('image.png')
img = xrv.datasets.normalize(img, 255)

# Run inference
with torch.no_grad():
    outputs = model(img.unsqueeze(0))