A comprehensive implementation for MRI reconstruction using pretrained deep learning models. This project supports XPDNet (TensorFlow/Keras) and VarNet (PyTorch/ATOMMIC) for brain multicoil data with 4× acceleration. No training required - uses inference-only with pretrained weights.
- ✅ XPDNet (TensorFlow/Keras) for brain multicoil 4× acceleration
- ✅ VarNet path with ATOMMIC + Hugging Face checkpoints (PyTorch)
- ✅ FastMRI dataset handling and preprocessing
- ✅ Automatic sensitivity map estimation
- ✅ Easy demo functionality with visualization
- ✅ Checkpoint swapping capabilities
- ✅ Comprehensive output formats (NIfTI, NumPy, PNG)
# Clone or download the project files
git clone <your-repo> # or download reconstructFastMRI.py + requirements.txt
# Install dependencies
pip install -r requirements.txt
# Optional: For VarNet path (PyTorch + ATOMMIC)
pip install torch torchvision transformers
pip install git+https://github.com/wdika/atommic.git# Run demo with XPDNet (downloads sample data automatically)
python reconstructFastMRI.py --demo
# Run demo with VarNet
python reconstructFastMRI.py --demo --method varnet# Reconstruct with XPDNet
python reconstructFastMRI.py --h5 path/to/your/brain_multicoil.h5
# Reconstruct with VarNet
python reconstructFastMRI.py --h5 path/to/your/brain_multicoil.h5 --method varnet
# Custom output directory and acceleration
python reconstructFastMRI.py --h5 data.h5 --output my_results --acceleration 8This project works with the public fastMRI dataset. To get access:
- Visit fastMRI website
- Request access (free for research)
- Download brain multicoil data
- Use with this reconstruction tool
Expected data format: .h5 files with kspace dataset containing multicoil k-space data [slices, coils, ky, kx].
from reconstructFastMRI import FastMRIReconstructor
# Initialize reconstructor
reconstructor = FastMRIReconstructor(method='xpdnet')
# Load pretrained weights
reconstructor.load_pretrained_weights()
# Run reconstruction
results = reconstructor.reconstruct_volume(
h5_path='brain_multicoil_sample.h5',
output_dir='reconstruction_output',
acceleration=4
)
print(f"Results saved to: {results['nifti_path']}")# Custom checkpoint
reconstructor = FastMRIReconstructor(method='xpdnet')
reconstructor.load_pretrained_weights('/path/to/custom/checkpoint.h5')
# Different acceleration factors
results = reconstructor.reconstruct_volume(
h5_path='data.h5',
output_dir='results_8x',
acceleration=8,
center_fraction=0.04 # Smaller center for higher acceleration
)# Using VarNet (requires PyTorch + ATOMMIC)
reconstructor = FastMRIReconstructor(method='varnet')
reconstructor.load_pretrained_weights()
results = reconstructor.reconstruct_volume(
h5_path='data.h5',
output_dir='varnet_results'
)# Minimal usage
python reconstructFastMRI.py --h5 data.h5
# Specify output directory
python reconstructFastMRI.py --h5 data.h5 --output my_results
# Choose reconstruction method
python reconstructFastMRI.py --h5 data.h5 --method xpdnet # or varnet
# Custom acceleration
python reconstructFastMRI.py --h5 data.h5 --acceleration 8 --center-fraction 0.04python reconstructFastMRI.py \
--h5 path/to/data.h5 \ # Input fastMRI h5 file
--output ./results \ # Output directory
--method xpdnet \ # xpdnet or varnet
--acceleration 4 \ # Acceleration factor
--center-fraction 0.08 \ # Center fraction for mask
--checkpoint path/to/custom/weights.h5 \ # Custom checkpoint
--demo # Run demo modeAfter reconstruction, you'll find:
output_directory/
├── reconstruction_xpdnet.nii.gz # NIfTI volume
├── reconstruction_xpdnet.npy # NumPy array
├── comparison_xpdnet.png # Comparison plot
└── slices/ # Individual slice images
├── slice_000_xpdnet.png
├── slice_010_xpdnet.png
└── ...
- Framework: TensorFlow/Keras
- Architecture: Primal-dual network with iterative reconstruction
- Domain: Alternates between image and k-space domains
- Iterations: 10 (configurable)
- Pretrained: Brain multicoil, 4× acceleration
- Framework: PyTorch via ATOMMIC
- Architecture: Variational network with cascaded refinement
- Domain: Primarily k-space with learned sensitivity encoding
- Cascades: 12 (typical)
- Pretrained: Available via Hugging Face
The system automatically downloads appropriate pretrained weights:
- XPDNet: Downloads from
facebook/xpdnet-brain-multicoilor fallback - VarNet: Downloads from
facebook/varnet-brain-multicoil
# Load custom checkpoint
reconstructor.load_pretrained_weights('/path/to/custom/model.h5')
# Via command line
python reconstructFastMRI.py --h5 data.h5 --checkpoint custom_model.h5# Runtime checkpoint swapping
reconstructor = FastMRIReconstructor(method='xpdnet')
# Load first checkpoint
reconstructor.load_pretrained_weights('checkpoint_1.h5')
results_1 = reconstructor.reconstruct_volume('data.h5', 'output_1')
# Swap to second checkpoint
reconstructor.load_pretrained_weights('checkpoint_2.h5')
results_2 = reconstructor.reconstruct_volume('data.h5', 'output_2')- Load: Read multicoil k-space from h5 file
- Undersample: Apply acceleration mask
- Sensitivity: Estimate coil sensitivity maps
- Reconstruct: Run through neural network
- Save: Export as NIfTI, NumPy, and visualizations
Uses ESPIRiT-like method:
- Extract calibration region (24×24 default)
- Convert to image domain
- Normalize by root-sum-of-squares
- Equispaced: Regular sampling with center calibration
- Center fraction: 8% default (0.08)
- Acceleration: 4× default (configurable)
- Python 3.7+
- 8GB RAM
- 2GB disk space
- Python 3.8+
- 16GB RAM
- GPU with CUDA support (for VarNet)
- 5GB disk space
numpy>=1.21.0tensorflow>=2.8.0matplotlib>=3.5.0h5py>=3.7.0nibabel>=3.2.0huggingface_hub>=0.16.0
Optional for VarNet:
torch>=1.12.0atommic(install from GitHub)
1. Missing Dependencies
pip install -r requirements.txt2. CUDA Out of Memory (VarNet)
# Use CPU instead
reconstructor = FastMRIReconstructor(method='xpdnet') # Falls back to CPU3. Download Errors
- Check internet connection
- Ensure Hugging Face Hub access
- Try with VPN if behind firewall
4. Data Format Issues
- Ensure h5 file has
kspacedataset - Check data dimensions:
[slices, coils, ky, kx] - Verify complex data format
For Large Datasets:
# Process slice by slice to save memory
# (automatically handled by the implementation)For Faster Processing:
# Use GPU if available (VarNet)
export CUDA_VISIBLE_DEVICES=0
python reconstructFastMRI.py --h5 data.h5 --method varnet- Fork the repository
- Create feature branch
- Add tests for new functionality
- Submit pull request
This project is provided for research and educational purposes. Please cite appropriate papers when using in publications:
- XPDNet: [Original Paper]
- VarNet: [Original Paper]
- FastMRI: [FastMRI Dataset Paper]
If you use this implementation, please cite:
@software{fastmri_reconstruction,
title={FastMRI Reconstruction with Pretrained Models},
author={Your Name},
year={2024},
url={https://github.com/your-repo}
}- FastMRI Dataset: https://fastmri.med.nyu.edu/
- XPDNet: [Link to paper]
- VarNet: [Link to paper]
- ATOMMIC: https://github.com/wdika/atommic
Need help? Open an issue or check the troubleshooting section above.