MedLatent
Research Autoencoders for Brain MRI and Faces
2 min read
A set of convolutional autoencoders (models for image compression and reconstruction) trained with latent space dimensions ranging from 2 to 128 on two datasets: brain MRI scans (IXI) and faces from the CelebA dataset. These models were created as part of a controlled study to compare architectures with a fixed latent layer size, allowing researchers to explore how data compression affects image reconstruction and generation quality. The models are intended for computer vision and medical imaging researchers (not for clinical diagnostics).
Key features
- Trained on two contrasting datasets: healthy adult brain MRIs and CelebA facial images.
- Controlled experiment varying latent vector size (z) from 2 to 128 units.
- Fully self-contained PyTorch-based architecture using GroupNorm and the SiLU activation function.
- Detailed quality metrics on a held-out test set (PSNR, SSIM, FID).
What you can do
- Use pretrained model weights to reconstruct MRI scans or faces.
- Generate new samples via sampling by fitting a multivariate Gaussian distribution (latent_gaussian.npz).
- Explore the latent space using built-in dimensionality reduction tools.
- Run inference and image generation in Python by loading weights via safetensors.
Example
Example code for loading the model and generating samples based on the trained Gaussian distribution:
import json, numpy as np, torch
from huggingface_hub import hf_hub_download
from safetensors.torch import load_file
import importlib.util
repo, run = "ChandraPrakashBathula/medlatent-autoencoder", "ixi/z16"
spec = importlib.util.spec_from_file_location("m", hf_hub_download(repo, "models/ae.py"))
m = importlib.util.module_from_spec(spec); spec.loader.exec_module(m)
cfg = json.load(open(hf_hub_download(repo, f"{run}/config.json")))
model = m.Autoencoder(m.AEConfig(**cfg["model_config"])).eval()
model.load_state_dict(load_file(hf_hub_download(repo, f"{run}/model.safetensors")))
g = np.load(hf_hub_download(repo, f"{run}/latent_gaussian.npz"))
mu, L = torch.tensor(g["mean"]).float(), torch.tensor(g["chol"]).float()
z = cfg["model_config"]["latent_dim"]
with torch.no_grad():
codes = mu + torch.randn(16, z) @ L.T
samples = model.decode(codes)
Read more
Details• Architecture: Encoder with 5 convolutional stages (32–512 channels), decoder with a mirrored structure.
• IXI Metrics (z=128): PSNR 27.07 dB, SSIM 0.899, reconstruction FID 119.6.
• Limitations: Pixel-wise mean squared error (MSE) leads to blurring of fine details (cortical folds and hair). The models are not medical devices.










Comments