Generating Synthetic Medical Data for Training and Augmentation
Module: Healthcare AI | Difficulty: Advanced
Generator Loss (GAN)
Discriminator Loss (GAN)
Diffusion Forward Process
Diffusion Reverse Process
Synthetic Data Quality Metrics
| Metric | Formula | Range | Purpose |
|---|---|---|---|
| FID | 0-infinity | Distribution match | |
| SSIM | 0-1 | Structural similarity | |
| Dice | 0-1 | Segmentation overlap | |
| Expert | Clinician rating | 1-5 | Clinical realism |
import torch
import torch.nn as nn
class MedicalGAN(nn.Module):
def __init__(self, latent_dim=100, img_channels=1):
super().__init__()
self.generator = nn.Sequential(
nn.Linear(latent_dim, 256), nn.LeakyReLU(0.2),
nn.Linear(256, 512), nn.BatchNorm1d(512), nn.LeakyReLU(0.2),
nn.Linear(512, 1024), nn.BatchNorm1d(1024), nn.LeakyReLU(0.2),
nn.Linear(1024, img_channels * 64 * 64), nn.Tanh())
def generate(self, batch_size, latent_dim=100):
z = torch.randn(batch_size, latent_dim)
return self.generator(z).reshape(-1, 1, 64, 64)
class ConditionalDiffusion(nn.Module):
def __init__(self, img_dim=64, num_classes=10, timesteps=100):
super().__init__()
self.timesteps = timesteps
self.class_embed = nn.Embedding(num_classes, 64)
self.time_embed = nn.Sequential(
nn.Linear(1, 64), nn.SiLU(), nn.Linear(64, 64))
self.denoise_net = nn.Sequential(
nn.Linear(64 * 64 * 64 + 64 + 64, 512),
nn.ReLU(), nn.Linear(512, 64 * 64 * 64))
def forward(self, x_t, t, class_label):
t_embed = self.time_embed(t.float().unsqueeze(1))
c_embed = self.class_embed(class_label)
x_flat = x_t.flatten(1)
combined = torch.cat([x_flat, t_embed, c_embed], dim=1)
noise_pred = self.denoise_net(combined)
return noise_pred.reshape(x_t.shape)
gan = MedicalGAN(latent_dim=100, img_channels=1)
synthetic = gan.generate(batch_size=4)
print(f'Synthetic images: {synthetic.shape}')
diffusion = ConditionalDiffusion(img_dim=64, num_classes=10)
x_t = torch.randn(1, 1, 64, 64)
t = torch.tensor([50])
label = torch.tensor([3])
noise_pred = diffusion(x_t, t, label)
print(f'Noise prediction: {noise_pred.shape}')
Research Insight: Synthetic medical data can augment small datasets and enable privacy-preserving data sharing. However, GANs can suffer from mode collapse, generating limited diversity. Diffusion models produce higher-quality synthetic images but are slower to generate. The most practical approach is using synthetic data as augmentation (20-30% of training data) rather than as the sole data source.