Team Ai
Apppublic

multimodalart/Krea-2-Image-Reference

sourceHugging Faceupdated 3mo agoView on Hugging Face
1likes
krea2_image_reference_example.py64 linesDownload Raw Back to root
1"""2Krea 2 Image Reference — training-free style transfer via Untwisting RoPE.3 4Renders a text prompt's *content* in a reference image's *style*, with no5training and no LoRA. Port of https://github.com/BigStationW/ComfyUi-Untwisting-RoPE6("Untwisting RoPE: Frequency Control for Shared Attention in DiTs",7https://arxiv.org/abs/2602.05013) to the 🧨 diffusers `Krea2Pipeline`.8 9Live demo: https://huggingface.co/spaces/multimodalart/Krea-2-Image-Reference10 11Setup12-----13    pip install "transformers>=4.57.0" accelerate sentencepiece \14        git+https://github.com/huggingface/diffusers.git15    # download the module that lives next to this file (from the gist or the Space):16    #   https://huggingface.co/spaces/multimodalart/Krea-2-Image-Reference/raw/main/krea2_untwist.py17 18Needs a CUDA GPU with enough memory for Krea 2 Turbo in bf16 (~26 GB) — the19style transfer runs a [target, reference] cross-batch, so budget accordingly.20"""21 22import torch23from diffusers import Krea2Pipeline24from diffusers.utils import load_image25 26# `krea2_untwist.py` must be importable (same folder as this script).27from krea2_untwist import style_transfer28 29pipe = Krea2Pipeline.from_pretrained("krea/Krea-2-Turbo", torch_dtype=torch.bfloat16)30pipe.to("cuda")31 32# The reference supplies the STYLE (palette / texture / rendering); the prompt33# supplies the CONTENT (composition / subject).34reference = load_image(35    "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/input_image_vermeer.png"36)37 38image = style_transfer(39    pipe,40    prompt="a red fox sitting in a snowy forest, soft winter light",41    reference_image=reference,42    height=1024,43    width=1024,44    num_inference_steps=8,          # Krea 2 Turbo is few-step, guidance-free45    # --- style controls (these are the tuned defaults from the demo) ---46    beta=2.25,                      # sharpness of the frequency curve47    low_scale_start=1.0,            # low-freq (style) scale eases in ...48    low_scale_end=2.75,             # ... to `style_strength` at the last steps49    high_scale_start=1.0,           # high-freq (structure) scale decays ...50    high_scale_end=0.0,             # ... to zero, so composition follows the prompt51    adain_strength=0.75,            # match color/contrast statistics to the reference52    blocks=(7, 27),                 # skip early blocks so the prompt keeps its layout53    generator=torch.Generator("cuda").manual_seed(0),54)55image.save("krea2_image_reference.png")56print("saved krea2_image_reference.png")57 58# Tuning tips59# -----------60# * Weak effect?  Raise low_scale_end (style strength) toward 3.0, raise61#   adain_strength, or lower the first block toward 0.62# * Reference bleeding into composition?  Lower low_scale_end, keep63#   high_scale_end at 0.0, and keep the first block >= 7.64