The code below uses a trained Denoising Diffusion Probabilistic Models (DDPM) model to generate a large batch of 2D MR image samples that are saved as a numpy array. These are also referred to as Stochastic Object Models (SOMs) in the rest of the implementation.
conda activate ddpm
cd src/demo1
# Python file and its input and outputs -----------------------------------------------------------------------------
PY_FILE=scripts/image_sample_newdataset2_centercrop.py
MODEL_PATH=trained_DDPM_model/ema_0.9999_1100000.pt
OUTPUT_FLD="test_out_200k"
# Important ddpm parameters --------------------------
dSTEP=1000
nSAMPLES=1
bSIZE=1
# CMD_ARGUMENTS aligning with how we trained the DDPM model -----------------------------------------------------------------------------
MODEL_FLAGS="--image_size 384 --attention_resolutions 32,16,8 --num_channels 128 --num_head_channels 64 --num_res_blocks 2 --resblock_updown True --use_scale_shift_norm True --learn_sigma True"
DIFFUSION_FLAGS="--diffusion_steps ${dSTEP} --noise_schedule cosine "
sample_FLAGS="--save_dir ${OUTPUT_FLD}/HCP_brain_384x384_cropped_260x311_step1100k_ema_samples/ --num_samples ${nSAMPLES} --batch_size ${bSIZE}"
time python ${PY_FILE} --model_path ${MODEL_PATH} $MODEL_FLAGS ${DIFFUSION_FLAGS} ${sample_FLAGS}
The generated .npz file is the object input for demos 2 and 3. With the default example above, the handoff file is:
test_out_200k/HCP_brain_384x384_cropped_260x311_step1100k_ema_samples/samples_10000x260x311x1.npz
-
The DDPM1 was trained using the HCP's young adult dataset2 :
- This dataset consists of 1,113 subjects scanned on a customized Siemens 3T MRI system.
- From each patient, 10 axial slices within their Cerebrospinal fluid (CSF) regions were extracted to train the DDPM model.
-
The no. of diffusion steps is an important parameter when running this ddpm-based MRI data generation code.
-
Appropriately validate the generated data using metrics relevant to your clinical application. Below are the image statistics corresponding to the mean, standard deviation, white matter area-to-intracranial area ratio, and gray matter area-to-intracranial area ratio, computed using 1,000 real and 1,000 DDPM-generated MR images at each step.
-
Approximately 217,000 images were generated, including 168,000 for DLMO training, 40,000 for UNet training, 8,000 for image fidelity and AUC analysis, and 640 × 2 for the 2AFC validation study of the DLMO.
Footnotes
-
J. Ho, A. Jain, and P. Abbeel, “Denoising diffusion probabilistic models,” Advances in neural information processing systems, vol. 33, pp. 6840–6851, 2020. ↩
-
“HCP-Young Adult 2025,” https://www.humanconnectome.org/study/hcp-young-adult/document/hcp-young-adult-2025-release, 2025. ↩