Views
No views yet
mnist/ckpt.pth (24 MB)cifar10/ckpt.pth (35 MB)celeba64/ckpt.pth (332 MB)


1# train_flow_matching_on_images.py
2python train_flow_matching_on_images.py \
3 --do_train \
4 --dataset mnist \
5 --n_epochs 50 \
6 --batch_size 128 \
7 --learning_rate 1e-31python train_flow_matching_on_images.py \
2 --do_train \
3 --dataset cifar10 \
4 --n_epochs 50 \
5 --batch_size 128 \
6 --learning_rate 1e-3 \
7 --horizontal_flip1# train_celeba64.py
2python train_celeba64.py \
3 --do_train \
4 --n_epochs 50 \
5 --batch_size 512 \
6 --learning_rate 1e-4 \
7 --horizontal_fliptrain_flow_matching_on_images.py - For MNIST and CIFAR-10train_celeba64.py - For CelebA 64×641import torch
2from huggingface_hub import hf_hub_download
3
4# Download checkpoint
5ckpt_path = hf_hub_download(
6 repo_id="WayBob/FlowMatching-Unet-Celeb-64x64",
7 filename="celeba64/ckpt.pth"
8)
9
10# Load checkpoint
11checkpoint = torch.load(ckpt_path, map_location="cuda")1import torch
2from flow_matching.models import UNetModel
3from flow_matching.solver import ODESolver, ModelWrapper
4
5device = "cuda"
6
7# Create model (CelebA example)
8flow = UNetModel(
9 dim=(3, 64, 64),
10 num_channels=128,
11 num_res_blocks=2,
12 num_classes=0,
13 class_cond=False,
14).to(device)
15
16# Load weights
17flow.load_state_dict(checkpoint)
18flow.eval()
19
20# Create solver
21model_wrapper = ModelWrapper(flow)
22solver = ODESolver(model_wrapper)
23
24# Sample from Gaussian noise
25batch_size = 4
26x_init = torch.randn(batch_size, 3, 64, 64).to(device)
27time_grid = torch.linspace(0, 1, 21).to(device) # 20 steps
28
29with torch.no_grad():
30 samples = solver.sample(
31 x_init=x_init,
32 step_size=0.05,
33 method="euler",
34 time_grid=time_grid
35 )
36
37# Denormalize from [-1, 1] to [0, 1]
38samples = (samples + 1) / 2
39samples = samples.clamp(0, 1)
40
41# Save or visualize
42from torchvision.utils import save_image
43save_image(samples, "generated_faces.png", nrow=2)1# For class-conditional models
2flow = UNetModel(
3 dim=(3, 32, 32), # CIFAR-10
4 num_channels=64,
5 num_res_blocks=2,
6 num_classes=10,
7 class_cond=True,
8).to(device)
9
10# Load CIFAR-10 checkpoint
11ckpt = torch.load("cifar10/ckpt.pth")
12flow.load_state_dict(ckpt)
13
14# Generate specific class (e.g., class 3)
15y = torch.tensor([3, 3, 3, 3]).to(device) # Batch of 4, all class 3
16
17def ode_func(t, x):
18 return flow(x=x, t=t, y=y)
19
20# Then use solver as before1pip install torch torchvision
2pip install torchdiffeq einops1@misc{flowmatching-unet-2024,
2 title={UNet Flow Matching Models for Image Generation},
3 author={WayBob},
4 year={2024},
5 howpublished={\url{https://huggingface.co/WayBob/FlowMatching-Unet-Celeb-64x64}}
6}