Views
No views yet
1import torch
2from diffusers import Flux2KleinPipeline, BitsAndBytesConfig, Flux2Transformer2DModel
3
4# Load base model with 4-bit quantization (fits in 16GB VRAM)
5nf4_config = BitsAndBytesConfig(
6 load_in_4bit=True,
7 bnb_4bit_quant_type="nf4",
8 bnb_4bit_compute_dtype=torch.bfloat16,
9)
10transformer = Flux2Transformer2DModel.from_pretrained(
11 "black-forest-labs/FLUX.2-klein-4B",
12 subfolder="transformer",
13 quantization_config=nf4_config,
14 torch_dtype=torch.bfloat16,
15)
16pipe = Flux2KleinPipeline.from_pretrained(
17 "black-forest-labs/FLUX.2-klein-4B",
18 transformer=transformer,
19 torch_dtype=torch.bfloat16,
20)
21
22# Load LoRA weights
23pipe.load_lora_weights("giannisan/light-fantasy-flux2-klein-lora")
24pipe.enable_model_cpu_offload()
25
26# Fix VAE dtype mismatch
27_orig_decode = pipe.vae._decode
28def _patched_decode(z, *args, **kwargs):
29 return _orig_decode(z.to(pipe.vae.dtype), *args, **kwargs)
30pipe.vae._decode = _patched_decode
31
32# Generate
33image = pipe(
34 prompt="light_fantasy, a detailed fantasy painting of a grand castle on a mountainside with waterfalls and wildflowers",
35 height=512,
36 width=512,
37 num_inference_steps=50,
38 guidance_scale=1.0,
39 generator=torch.Generator("cpu").manual_seed(42),
40).images[0]
41image.save("output.png")light_fantasy at the start of your prompt to activate the style. For best results, follow it with a descriptive scene:light_fantasy, a detailed fantasy painting of [your scene description]guidance_scale=1.0 — this is a distilled model, CFG guidance is ignored.| Prompt | Style |
|---|---|
light_fantasy, a detailed fantasy painting of a medieval harbor town at sunset with tall ships | In-distribution |
light_fantasy, a detailed fantasy painting of a dragon sleeping on gold in a crystal cavern | Out-of-distribution (new subject, trained style) |
light_fantasy, a detailed fantasy painting of a cozy wizard's library with floating books | Out-of-distribution |
| Parameter | Value |
|---|---|
| Base model | FLUX.2-klein-4B |
| Method | DreamBooth LoRA with QLoRA (NF4 quantization) |
| Dataset | 232 fantasy painting images with per-image BLIP captions |
| Resolution | 512×512 |
| LoRA rank | 16 |
| Learning rate | 1e-4 (constant, 100 warmup steps) |
| Training steps | 2000 |
| Batch size | 1 (gradient accumulation 4) |
| Optimizer | AdamW 8-bit |
| Mixed precision | bf16 |
| Final loss | 0.969 |
| Hardware | NVIDIA RTX 4060 Ti 16GB |
| Training time | ~3 hours |
Salesforce/blip-image-captioning-large). Each caption starts with the light_fantasy trigger word.