Skip to main content

3D and 2D VAE tools for medical and grayscale images

Project description

3D Variational Autoencoder Framework

This repository provides an easy-to-use framework for training 3D Variational Autoencoders (VAEs), with a focus on grayscale medical imaging data such as CT or MRI volumes. The codebase is fully compatible with 2D images as well, making it applicable beyond volumetric data.

The framework includes flexible data loaders, a modular VAE architecture, patch-based training utilities, mixed-precision support, optional distributed training (DDP), and seamless Weights & Biases logging.


Data Loaders

The provided dataloaders allow you to take any 3D image dataset and:

  • Extract patches from volumes to train your model on smaller subregions instead of full 3D images.
  • Generate overlapping or non-overlapping patches.
  • Randomly sample patches per volume each epoch.
  • Use the test loader to output any number of random patches of any chosen shape, useful for visualization or debugging.

This makes it straightforward to train large 3D models even on limited GPU memory.


Model

CustomVAE (defined in model.py) is a highly configurable 3D Variational Autoencoder. It supports:

  • User-defined downsampling and upsampling blocks.
  • Optional attention in the bottleneck:
    • Global attention
    • Patch attention
    • Relative position bias (optional)
  • Flexible latent dimensionality
  • Multiple normalization and activation options
  • Support for both 2D and 3D inputs

The architecture is modular, making it easy to extend or customize for research.


Training

Training utilities come with:

  • Automatic support for DistributedDataParallel (DDP)
    (DDP is initialized automatically when launched via torchrun).
  • Optional mixed precision (AMP) for memory-efficient training.
  • Gradient accumulation for large effective batch sizes.
  • W&B logging for losses, reconstructions, and sample visualizations.
  • Automatic checkpointing (periodic and best-model).

Full-Volume Training

To train directly on full 3D images, simply call the train_3d function with:

patching = None

The model receives entire volumes as input. This is the simplest way to train if your GPU memory allows it.

Patch-Based Training

Patch-based training is available by setting:

  • patching = "full"
    → each volume is fully decomposed into patches; the model trains on all patches.

  • patching = "random_parts"
    → a fixed number of random patches are sampled per volume every epoch, reducing computation and adding useful stochasticity.

Patch training enables high-resolution 3D models to run on modest hardware.


Evaluation

Evaluation supports both full-volume and patch-based reconstruction:

  • mode="full"
    → the entire 3D volume is fed through the model.

  • mode="patch"
    → the volume is broken into overlapping patches, reconstructed patch-by-patch, and stitched back together using blending or averaging.

The final reconstruction loss is computed on the fully reassembled volume.

W&B logging is also supported during evaluation, including sample reconstructions and GIF/MP4 visualizations.


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

vae3d2d-0.2.0.tar.gz (85.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

vae3d2d-0.2.0-py3-none-any.whl (101.2 kB view details)

Uploaded Python 3

File details

Details for the file vae3d2d-0.2.0.tar.gz.

File metadata

  • Download URL: vae3d2d-0.2.0.tar.gz
  • Upload date:
  • Size: 85.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.11

File hashes

Hashes for vae3d2d-0.2.0.tar.gz
Algorithm Hash digest
SHA256 fa56c30bb2db2b7b05a631d14f0953a1a25acf6049cc23dcb34f087cf5d4ff42
MD5 20005d2feb5267c83875019c1ac57fb0
BLAKE2b-256 70442a233ed48c507cc02821d85c5ae8b0db6e9624906872a6e72d75ec450afb

See more details on using hashes here.

File details

Details for the file vae3d2d-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: vae3d2d-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 101.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.11

File hashes

Hashes for vae3d2d-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 08d86f963fa1043aa2d9a2f8f7d74249863a2c917cc5415250515596b9ca4ebd
MD5 da23e586c6f0d2a210dda37f9db30562
BLAKE2b-256 788c175c68df5a6c572cbbffc45067a371b1b4365efa3da20901440134de2b42

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page