Skip to main content

Flow matching, mini-batch optimal transport coupling, and posterior sampling for inverse problems in PyTorch

Project description

DeltaFlow

Flow matching and anatomy-invariant guidance alignment for radiograph generative pretraining.

DeltaFlow provides composable PyTorch primitives for two things that are usually bundled together in ad-hoc research code:

  • Flow matching — a simulation-free generative objective that regresses a velocity field onto the conditional velocity of a probability path (Lipman et al., 2023), sampled with a simple Euler ODE solver.
  • Delta alignment — a multi-scale, anatomy-cancelling loss that aligns the guidance-difference feature Δh = h_cond - h_uncond across augmented views, instead of aligning raw (anatomy-entangled) features. This is the mechanism behind the name: Delta is the guidance-difference feature, Flow is the flow-matching engine that consumes it.

The library targets 2D radiography (chest X-ray, cephalometric, and hand radiographs), but the core primitives (interpolants, samplers, losses, models.projector) are domain-agnostic.

What's inside

Module Role
deltaflow.core BaseVelocityField — the interface every backbone implements
deltaflow.interpolants Probability paths, e.g. LinearInterpolant (rectified flow)
deltaflow.samplers FlowSampler — Euler ODE integration
deltaflow.losses FlowMatchingLoss, DeltaAlignmentLoss
deltaflow.models MultiScaleProjector (per-level projection heads), EMA
deltaflow.datasets Generic radiograph dataset wrappers (chest / cephalo / hand)

Install

pip install -e .
# or, for docs/dev tooling:
pip install -e ".[dev]"

Usage

Flow matching

import torch
from deltaflow.interpolants import LinearInterpolant
from deltaflow.losses import FlowMatchingLoss
from deltaflow.samplers import FlowSampler

loss_fn = FlowMatchingLoss(interpolant=LinearInterpolant())
loss = loss_fn(model, x1)               # model(x_t, t) -> predicted velocity
loss.backward()

samples = FlowSampler(model).sample(torch.randn(1000, 2), n_steps=50)

Delta alignment

from deltaflow.losses.delta_alignment import DeltaAlignmentLoss
from deltaflow.models import MultiScaleProjector

projector = MultiScaleProjector(feature_dims={"enc_1_4": 256, "bottleneck": 1024})
loss_fn = DeltaAlignmentLoss(projector, lambda_flow=1.0, lambda_align=5.0)

total, loss_dict = loss_fn(
    v_c1, v_u1, target_v1,
    v_c2, v_u2, target_v2,
    feats_u1, feats_c1, feats_u2, feats_c2,
)

See examples/ for runnable, tiered walkthroughs (00-foundations/, 10-sampling/, 20-training/, 90-showcase/).

Development

pip install -e ".[dev]"
pytest

Contributing

See CONTRIBUTING.md.

License

MIT. See LICENSE.

Citation

If you use DeltaFlow in your research, please cite it (see CITATION.cff).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torchdeltaflow-0.1.0.tar.gz (46.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

torchdeltaflow-0.1.0-py3-none-any.whl (41.3 kB view details)

Uploaded Python 3

File details

Details for the file torchdeltaflow-0.1.0.tar.gz.

File metadata

  • Download URL: torchdeltaflow-0.1.0.tar.gz
  • Upload date:
  • Size: 46.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for torchdeltaflow-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f1437c7356fe6e5170e52ae1445d2440c5601a47380fa79df9685e3e8427eb41
MD5 d8751c6ff205313054bdc1fa546b6374
BLAKE2b-256 00dfb001d648094d6032da0b8ad15a706e82e24d766ee1d9b2e3c4671194aa7b

See more details on using hashes here.

File details

Details for the file torchdeltaflow-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: torchdeltaflow-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 41.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for torchdeltaflow-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8ebd1a16526f70b3cd82da9c42a92c1dab6a461f6894ad3adc581c4e4da93e56
MD5 3007b10942226e4baf250158d5b86280
BLAKE2b-256 8c8974f543485adc7836e0afffa1d8d2f92327a291f235ee81b8a6103e50ede4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page