This repository contains the implementation of the constricting CBF-based constrained image generation (Section 5.2.1) from our paper.
We enforce hard pixel-level constraints on a pre-trained diffusion model (DDPM-bedroom-256) by solving a minimum-norm Quadratic Program (QP) at each sampling step. The QP synthesizes a feedback control input that maintains the sample within a constricting safety tube (Algorithm 1 in the paper), guaranteeing that the final generated image satisfies the specified constraints.
pip install torch diffusers cvxpy pillow numpyA CUDA-capable GPU is recommended. The code falls back to CPU if unavailable.
-
Place your reference image as
window.pngin the same directory as the script. This is the image patch that will be enforced at the target location. -
Run:
python cbf_constricting.py- The output image is saved to
cbf_constricting_window.png. - logger.py compares various constricting schedules, and saves the results in a new directory called 'out'
The script implements Algorithm 1 from the paper in a single file:
- Load pre-trained model: DDPM-bedroom-256 from Hugging Face Diffusers (no retraining or modification).
- Define the constraint: A reference image patch is placed at pixel coordinates
(40, 150)with a spatially-varying maskv(p)that smoothly decays at boundaries (Eq. 16). - Initialize the safety tube: Compute the initial relaxation
\epsilon_0based on the constraint violation of the noise samplex(T)(Theorem 4.1). - Sample with CBF guidance: At each of the 200 sampling steps:
- Predict noise with the pre-trained U-Net
- Compute the nominal (uncontrolled) update
- Solve the per-pixel minimum-norm QP to obtain the control input
u(Eq. 10) - Apply the control:
x_{k-1} = x_uncontrolled - u·\Delta t
- Save: The final image satisfies
h(x) \geq 0for all constrained pixels.
| Parameter | Value | Reference |
|---|---|---|
Error tolerance e |
0.005 | Eq. 16 |
Class-K parameter \alpha |
0.5 | Section 5 |
Safety margin c |
0.01 | Section 5 |
Sampling steps K |
200 | Section 5.2.1 |
| Edge decay width | 5% | Section 5.2.1 |