P-Q-Image-2.1-DWD / scripts /generate.py
johnrachwanpruna's picture
Release P-Q-Image-2.1-Turbo-DWD
baa0a0e verified
Raw History Blame Contribute Delete
3.96 kB
#!/usr/bin/env python
"""Generate or edit an image with Qwen-Image-2.1-Turbo and the DWD head (no VAE decode).
python scripts/generate.py --prompt "a red fox in fresh snow at sunrise" --width 2048 --height 2048 --out fox.png
python scripts/generate.py --prompt "Add gently falling snow to the whole scene." --image in.jpg --out snow.png
--tail takes a tail .safetensors file, a directory holding dwd_tail.safetensors, or a Hugging Face repo id. Width and
height must be multiples of 32; for an edit they default to about --output-resolution^2 pixels at the input's aspect.
--compare also writes the stock pipeline's image for the same seed (VAE decode) next to the output.
"""
import argparse
import os
import sys
import time
try:
from pq_image_dwd.pipeline import DEFAULT_TAIL, DWDPipeline
except ImportError: # running from a source checkout without installing the package
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "src"))
from pq_image_dwd.pipeline import DEFAULT_TAIL, DWDPipeline
def main(argv=None) -> int:
ap = argparse.ArgumentParser(description="Qwen-Image-2.1-Turbo + DWD head: text-to-image and editing without the "
"VAE decode")
ap.add_argument("--prompt", required=True, action="append",
help="text prompt or edit instruction; repeat for several images (seeds seed, seed + 1, ...)")
ap.add_argument("--image", action="append", default=None,
help="condition image for editing; repeat for several condition images")
ap.add_argument("--tail", default=DEFAULT_TAIL, help="tail .safetensors, a directory holding it, or a repo id")
ap.add_argument("--base", default="Qwen/Qwen-Image-2.1-Turbo", help="base model repo id or local diffusers dir")
ap.add_argument("--width", type=int, default=None)
ap.add_argument("--height", type=int, default=None)
ap.add_argument("--output-resolution", type=int, default=1024,
help="condition images are resized to about this^2 pixels (and the default output size)")
ap.add_argument("--seed", type=int, default=0)
ap.add_argument("--out", default="dwd.png", help="output PNG; with several prompts, _<i> is added to the name")
ap.add_argument("--compare", action="store_true",
help="also write the stock pipeline's image (VAE decode) as <out>_vae.png")
ap.add_argument("--device", default=None, help="default: cuda if available, else cpu")
ap.add_argument("--compile", action="store_true", help="torch.compile the transformer blocks (slow first image)")
a = ap.parse_args(argv)
for name in ("width", "height"):
v = getattr(a, name)
if v is not None and (v <= 0 or v % 32):
ap.error(f"--{name} must be a positive multiple of 32")
images = None
if a.image:
from PIL import Image
images = [Image.open(p) for p in a.image]
t0 = time.perf_counter()
pipe = DWDPipeline.from_pretrained(a.tail, base=a.base, device=a.device, compile=a.compile)
print(f"loaded in {time.perf_counter() - t0:.1f} s", flush=True)
t1 = time.perf_counter()
res = pipe(a.prompt, image=images, width=a.width, height=a.height, seed=a.seed,
output_resolution=a.output_resolution, return_vae=a.compare)
dwd, vae = res if a.compare else (res, None)
print(f"generated {len(dwd)} image(s) in {time.perf_counter() - t1:.2f} s", flush=True)
root, ext = os.path.splitext(a.out)
ext = ext or ".png"
os.makedirs(os.path.dirname(os.path.abspath(a.out)), exist_ok=True)
for i, img in enumerate(dwd):
stem = root if len(dwd) == 1 else f"{root}_{i}"
img.save(stem + ext)
print(f"wrote {stem + ext}")
if vae is not None:
vae[i].save(f"{stem}_vae{ext}")
print(f"wrote {stem}_vae{ext}")
return 0
if __name__ == "__main__":
sys.exit(main())