KAIST CS492(D): Diffusion Models and Their Applications (Fall 2024)
Programming Assignment 7
Instructor: Minhyuk Sung (mhsung [at] kaist.ac.kr)
TA: Juil Koo (63days [at] kaist.ac.kr)
Flow Matching (FM) is a novel generative framework that shares similarities with diffusion models, particularly in how both tackle the Optimal Transport problem through an iterative process. Similar to diffusion models, FM also splits the sampling process into several time-dependent steps. At first glance, FM and diffusion models may seem almost identical due to their shared iterative sampling approach. However, the key differences lie in the objectve function and the choice of trajectories in FM.
Regading the objective function, diffusion models predict the injected noise during training. In contrast, Flow Matching predicts the displacement between the data distribution and the prior distribution.
Moreover, Flow Matching is developed from the perspective of flow, a time-dependent transformation function that corresponds to the forward pass in diffusion models. Unlike diffuison models, where the forward pass is fixed to ensure that every intermediate distribution also follows a Gaussian distribution, FM offers much greater flexibility in the choice of flow. This flexibility allows for the use of simpler trajectories, such as linear interpolation over time, between the data distribution and the prior distribution.
Experimental results have sohwn that the FM objective and its simpler trajectory are highly effective in modeling the data distribution, Making FM a compelling alternative to diffusion models.
Install the required package within the requirements.txt
pip install -r requirements.txt
Please note that this assignment is heavily dependent on Assignment 2. To begin, you should copy the functions you implemented in Assignment 2.
.
├── 2d_plot_fm_todo (Task 1)
│ ├── fm_tutorial.ipynb <--- Main code
│ ├── dataset.py <--- Define dataset (Swiss-roll, moon, gaussians, etc.)
│ ├── network.py <--- A vector field prediction network
│ └── fm.py <--- (TODO) Implement Flow Matching
│
└── image_fm_todo (Task 2)
├── dataset.py <--- Ready-to-use AFHQ dataset code
├── fm.py <--- (TODO) Implement Flow Matching
├── module.py <--- Basic modules of a vector field prediction network
├── network.py <--- U-Net
├── sampling.py <--- Image sampling code
└── train.py <--- Flow Matching training code
In this assignment, we explore Flow Matching (FM), a method that, like diffusion models, deals with time-dependent trajectories but does so from the perspective of vector fields that construct probability density paths. At a high level, FM shares similarities with diffusion models but offers more straight trajectories. Unlike the diffusion models, whose trajectories involve nonlinear terms that add unnecessary complexity, the trajectories in FM can be much simpler. They can be represented as a simple linear interpolation between the data point
- [Paper] Flow Matching for Generative Modeling (FM)
- [Paper] Flow Straight and Fast: Learning to Generate and Transfer Data with Rectified Flow (RF)
- [Blog] An Introduction to Flow Matching
Further material is listed at the bottom of the document.
In FM, we have a probability density path
However, since
The equations above can be derived from the continuity equation:
Given the conditional probabiliy paths
While the conditional probability paths and vector fields can be designed in various ways, we opt for the simplest vector field, which takes the form of a Gaussian kernel:
Specifically, we set
In this case, the CFM loss takes the following form:
As similar to previous Assignments 1 and 2, we will first implement Flow Matching (FM) and test it in a simple 2D plot toy experiment setup.
❗️❗️❗️ You are only allowed to edit the part marked by TODO. ❗️❗️❗️
In this assignment, you will implement all key functions of flow matching for training and sampling.
You can copy the 2d_plot_ddpm_todo/network.py and image_ddpm_todo/network.py that you've already implemented in Assignments 1 and 2.
complete the functions compute_psi_t() and step() of FMScheduler class in fm.py.
The step() function is a one step of ODESolver from
where
Complete get_loss() function of FlowMatching in fm.py that corresponds to the conditional flow matching objective written in Eq. 23 of the FM paper.
Complete sample() function of FlowMatching in fm.py.
Once you finish the implementation above, open and run fm_tutorial.ipynb via jupyter notebook. It will automatically train a FM and measure chamfer distance between 2D particles sampled by the FM and 2D particles sampled from the target distribution.
Take screenshots of:
- the training loss curve
- the Chamfer Distance reported after executing the Jupyter Notebook
- the visualization of the sampled particles.
Below are the xamples of (1) and (3).
Note that you need to run sampling with 50 inference steps, which is set as the default.
If you've completed Task 1, finish implementing the sample() function and get_loss() function to work with a classifier-free guidance setup.
After finishing the implementation for the CFG setup, train FM with the CFG setup: python train.py --use_cfg.
❗️❗️❗️ You are only allowed to edit the part marked by TODO. ❗️❗️❗️
It will sample images and save a checkpoint every args.log_interval. After training a model, sample and save images by
python sampling.py --use_cfg --ckpt_path ${CKPT_PATH} --save_dir ${SAVE_DIR_PATH}
We recommend starting the training as soon as possible as the training would take 14 hours.
As done in Assignments 1 and 2, measure FID score using the pre-trained classifier network provided previously.
python dataset.py
python fid/measure_fid.py $GT_IMG_DIR $GEN_IMG_DIR
Use the evaluation set of the AFHQ dataset, data/afhq/eval, not data/afhq/val as @GT_IMG_DIR.
Take a screenshot of a FID and include at least 8 sampled images.
Submission Item List
- Code without model checkpoints
Task 1
- Loss curve screenshot
- Chamfer distance results of FM sampling with 50 inference steps.
- Visualization of FM sampling.
Task 2
- FID score result obtained with the CFG scale of 7.5
- At least 8 images generated by Flow Matching
In a single PDF file, write your name and student ID, and include submission items listed above. Refer to more detailed instructions written in each task section about what to submit.
Name the document {NAME}_{STUDENT_ID}.pdf and submit both your code and the document as a ZIP file named {NAME}_{STUDENT_ID}.zip.
For this programming assignment, exclude any model checkpoints and the provided pre-trained classifier checkpoint when compressing the files.
Submit the zip file on GradeScope.
You will receive a zero score if:
- you do not submit,
- your code is not executable in the Python environment we provided, or
- you modify anycode outside of the section marked with
TODOor use different hyperparameters that are supposed to be fixed as given.
Plagiarism in any form will also result in a zero score and will be reported to the university.
Your score will incur a 10% deduction for each missing item in the submission item list.
Otherwise, you will receive up to 20 points from this assignment that count toward your final grade.
- Task 1
- 10 points: Achieve CD lower than 40.
- 5 points: Achieve greater, or equal to 40 and less than 60.
- 0 point: otherwise.
- Task 2
- 10 points: Achieve FID lower than 30 with CFG=7.5.
- 5 points: Achieve FID between 30 and 50 with CFG=7.5.
- 0 point: otherwise.
If you are interested in this topic, we encourage you to check ou the materials below.


