From Noise to Images:
A Deep Dive into Flow Matching for Generative Modeling

Introduction

Diffusion models are stable but slow; GANs are fast but hard to train. Flow Matching aims to get the best of both: stable training and faster sampling.

Over the years, generative models like GANs, VAEs, and autoregressive models have made big strides. Most recently, diffusion models became popular because they’re reliable and produce great images. The catch? They’re slow at sampling—you often need dozens to hundreds of small steps to turn noise into a final image.

GANs flip that trade-off. They generate an image in one forward pass, so sampling is fast. But training is a delicate adversarial game that can be unstable and sometimes collapses to a few modes. Flow Matching tries to bridge these worlds. Instead of denoising in many tiny steps (diffusion) or playing an adversarial game (GANs), it learns a velocity field: a rule for how to move a point of noise toward a real image continuously over time. You train it with a simple regression loss (no adversary, no simulation of the ODE during training). At test time, you solve a deterministic ODE to get an image—often in far fewer steps than diffusion needs.

In this blog, we’ll explain the idea, compare it to GANs and diffusion, present the key equations, and walk through image generation from noise to image with a clean PyTorch implementation on CIFAR-10[2].

How Diffusion Models Work (and Why They’re Slow)

Big idea. Diffusion models learn to clean static off pictures. During training, we take real images and progressively add TV-like noise until they’re unrecognizable. The model sees these noisy versions and learns the small cleanup move that would make each one a bit clearer. Because it practices at many noise levels—from “slightly grainy” to “pure snow”—it becomes a reliable cleaner.

Training phase (why it’s stable).

• Each example is a straightforward cleanup task: “Here’s a noisy image → nudge it closer to a clean one.”

• There’s no adversarial game; it’s supervised and predictable, so training is usually steady.

• Learning across many noise levels teaches the model both big early cleanups and tiny late refinements.

Sampling phase (why it’s slow).

• To generate, you start from pure noise—there’s no picture underneath.

• You apply the learned cleanup move step by step, gradually revealing shapes, then edges, then texture.

• Each step is intentionally small to avoid smearing details, so you need dozens to hundreds of steps.

• If you add guidance (e.g., “make it look more like a cat”), many pipelines run the model multiple times per step, further increasing compute.

• High-resolution images make each step heavier, multiplying the total time.

Why people still use diffusion.

• Reliable training without adversarial instability.

• Excellent image quality: the coarse-to-fine cleanup naturally builds structure first and details later.

• Flexible control at sampling time (class labels, text prompts, style strength, etc.).

Figure 1. Diffusion sampling pipeline—starting from pure noise (t = T), the model iteratively denoises in reverse time to produce a clean generated image (t = 0), with intermediate refinements shown.

GANs (fast sampling, tricky training)

GANs in practice, short and simple: A GAN has two parts: a generator (G) that turns random noise (and optionally a class label) into an image, and a discriminator (D) that tries to tell real dataset images from G’s fakes. You feed in real images from your dataset (often normalized to [-1, 1]); the goal is to make G good enough that D can’t tell the difference.

Training: In each iteration, you (1) show D a mix of real and fake images and update D to spot fakes better, then (2) update G so its fakes fool D. Repeat for many epochs. Common, simple choices: Adam optimizer (lr≈1e-4–2e-4), batch size as large as your GPU allows, 1:1 update ratio for D:G, BatchNorm in G, SpectralNorm or a gradient penalty in D, light data augmentation, and EMA of G for cleaner samples. Watch out for mode collapse (all outputs look similar) and sensitivity to small hyperparameter changes.

Testing (inference): After training, freeze G and sample images by a single forward pass: draw noise (and a label if conditional) → G → image, then denormalize to view/save. It’s fast and easy to control variety by changing the noise (or the label). For overall quality, modern diffusion models usually beat GANs on fidelity/diversity; Flow Matching aims to reach diffusion-like quality with fewer steps, though results depend on the setup.

Figure 2: GAN training workflow. Random noise is mapped by the Generator into a synthetic image; the Discriminator sees both real images and generated ones and predicts “real or fake?”. Generator loss updates the generator to fool the discriminator, while discriminator loss updates the discriminator to better distinguish real from fake.

How Flow Matching Works

We want a generator that maps samples from an easy source distribution p0 (e.g., standard Gaussian noise) to a complex target distribution p1 (e.g., natural images). Rather than denoising in many tiny steps (diffusion) or training a generator against a discriminator (GANs), Flow Matching learns a time-dependent vector field f_theta(x, t). This vector field prescribes how any point x should move at time t so that, if we start from x(0) ~ p0 and follow the ordinary differential equation below, the distribution of states at t=1 matches the data distribution p1.

Figure.3 .Flow matching learns a smooth path from noise to data by modeling a time-dependent velocity field.[1]

Objective (conditional flow matching) :

Training recipe (per minibatch). We construct short, supervised problems by choosing endpoints and an intermediate time, then asking the network to predict the correct velocity at that point along the path:

• Sample endpoints x0 ~ p0 and x1 ~ p1 (optionally with class label y for conditional generation).

• Sample an interpolation time t ~ U(0,1) and form the intermediate point x_t = (1 - t) x0 + t x1 (a straight-line path; spherical/OT paths are also possible).

• The ground-truth velocity along this straight path is constant: v* = x1 - x0 (the direction and speed needed to continue from x_t toward x1).

• Train f_theta by minimizing a simple mean-squared error to this target velocity:

Where

Why this is attractive. The loss is supervised regression—no adversary and no need to simulate the ODE during training. Because (x0, x1, t) are sampled independently for each example, the model learns many local constraints that glue together into a coherent global flow. In practice, this produces diffusion-like training stability while avoiding multi-step noise schedules.

Implementation tips. Encode time t with a small MLP or Fourier features; include class embeddings if using labels. Use standard optimizers (AdamW), cosine LR schedules, and gentle gradient clipping. The target velocity v* and the interpolated point x_t must be computed for each sample; omitting x_t (or the interpolation step) leads to a mismatched objective.

Sampling with flow matching:

After training, generation reduces to solving an initial value problem from t = 0 to 1 using the learned velocity field. Sampling is deterministic given the random seed for x0 (and the label y, if any). You may use a small number of fixed steps for speed, or an adaptive ODE solver for higher fidelity.

Discrete Euler sampling step (most compact form):

• x_t: current sample at time t.

• Δt: step size (e.g., Δt = 1/N for N steps over [0,1]).

• f_theta(x_t, t[, y]): learned velocity—how to move x_t at time t (optional class label y).

• x_{t+Δt}: the next point after one forward step using the predicted velocity.

Integrator choices. Euler is fast and often sufficient (e.g., 20–50 steps). Higher-order solvers like Heun or Runge–Kutta (RK45) can improve quality with moderate extra cost. During sampling, lightweight clamping or normalization of x may improve numerical stability.

Position in the landscape. Sampling typically needs far fewer steps than diffusion (which removes noise in many tiny increments) and is slower than GANs (which produce an image in one forward pass). Flow Matching offers a pragmatic middle ground: stable supervised training and faster, ODE-based sampling.

Flow Matching on CIFAR-10: A Code Walkthrough

Flow Matching is a new generative modeling approach that sits between GANs and diffusion models. In this walkthrough, we’ll go through a PyTorch implementation step by step, showing how to set up the model, training, and sampling pipeline on CIFAR-10.

1. Setup and Imports

We start with the usual suspects: PyTorch, Torchvision, SciPy for ODE solvers, and Matplotlib for visualization.

2. Attention and Residual Blocks

The model uses residual blocks (like ResNet) with optional channel attention to emphasize important features.

3. Flow Matching Model

This is a U-Net style encoder–decoder that predicts a velocity field given an image, time, and class label.

4. Training Objective

The loss function compares predicted velocity with the true direction from noise → real image.

The training loop uses AdamW, gradient clipping, and cosine learning rate scheduling.

5. Sampling Images

Two sampling strategies are implemented:

Fast Euler Sampling

ODE Solver Sampling

6. Visualization

Finally, the code provides functions to visualize generated images, compare them with real samples, and generate grids for qualitative evaluation.

Training Progress: Loss Curve

The first figure shows the training loss curve over 100 epochs. We can see a steady decline, meaning the model is learning to better match the flow between random noise and real CIFAR-10 images. The curve stabilizes after about 60 epochs, suggesting that the model converges to a point where further training only brings marginal improvements. This steady decrease is a good indicator of stable training, especially compared to GANs where loss curves often oscillate unpredictably.

👉 You can reproduce this training and plot directly on Colab here:
Open Colab Notebook

Figure 4. Training Loss Curve for Flow Matching Model

Real vs Generated Ships

The second figure compares real ship images (top row) from CIFAR-10 with generated ship samples (bottom row) produced by our flow matching model. While the generated ships are still slightly blurry compared to the real samples, they clearly capture essential characteristics such as hull structure, decks, and the surrounding water. This demonstrates that the model has learned to transform random noise into meaningful visual patterns that resemble ships, showing the effectiveness of flow matching on a challenging dataset like CIFAR-10.

Figure 5. Real vs Generated Ship Images on CIFAR-10

Real vs Generated Airplanes

The third figure shows a similar comparison for the airplane class. Real airplanes (top row) are sharp and diverse, while the generated ones (bottom row) successfully capture the overall shape, wings, and orientation of airplanes. Some generated samples still lack fine detail, but the results highlight the model’s ability to capture class-specific features. With more training or larger architectures, the generated airplanes could become nearly indistinguishable from real CIFAR-10 samples.

Together, these results show how flow matching produces visually coherent images while maintaining stable and predictable training dynamics, bridging the gap between GANs’ speed and diffusion’s stability.

Figure 6. Real vs Generated Airplane Images on CIFAR-10

References

  1.  Lipman, Y., Havasi, M., Holderrieth, P., Shaul, N., Le, M., Karrer, B., Chen, R. T. Q., Lopez-Paz, D., Ben-Hamu, H., & Gat, I. (2024). Flow Matching Guide and Code. arXiv:2412.06264 [cs.LG]. Retrieved from https://arxiv.org/abs/2412.06264

  2.  Krizhevsky, A. (2009). Learning multiple layers of features from tiny images. Technical Report, University of Toronto. Available at https://www.cs.toronto.edu/~kriz/cifar.html

  3.  Zhang, Y., Li, K., Li, K., Wang, L., Zhong, B., & Fu, Y. (2018). Image Super-Resolution Using Very Deep Residual Channel Attention Networks. In Proceedings of the European Conference on Computer Vision (ECCV) (pp. 286–301). https://arxiv.org/abs/1807.02758

  4.  Ronneberger, O., Fischer, P., & Brox, T. (2015). U-Net: Convolutional Networks for Biomedical Image Segmentation. In Proceedings of MICCAI 2015 (pp. 234–241). Springer.https://arxiv.org/abs/1505.04597

We Are Hiring!

クーガーは自律的で大胆なチャレンジを支援し、それぞれの個性を生かした技術と創造性の追求ができる場を目指しています。

ぜひ採用ページからエントリーください。カジュアル面談も実施しています!


最新情報をメールで取得

登録

© Couger Inc. All rights reserved.