Views
No views yet
Qwen-Image base model first to use this adapter.1import os
2import torch
3from diffusers import DiffusionPipeline
4from safetensors.torch import load_file
5from peft import LoraConfig, get_peft_model
6
7os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python"
8os.environ["DIFFUSERS_DISABLE_NATIVE_ATTENTION"] = "1"
9
10def load_model(model_path, ckpt_path=None, use_lora=True):
11 torch_dtype = torch.get_default_dtype() if not torch.cuda.is_available() else torch.bfloat16
12 device = "cuda" if torch.cuda.is_available() else "cpu"
13
14 pipe = DiffusionPipeline.from_pretrained(
15 model_path,
16 torch_dtype=torch_dtype,
17 ).to(device)
18 pipe.safety_checker = None
19
20 if ckpt_path is not None and use_lora:
21 target_modules = [
22 "attn.to_k", "attn.to_q", "attn.to_v", "attn.to_out.0",
23 "attn.add_k_proj", "attn.add_q_proj", "attn.add_v_proj", "attn.to_add_out",
24 "img_mlp.net.0.proj", "img_mlp.net.2",
25 "txt_mlp.net.0.proj", "txt_mlp.net.2",
26 ]
27 transformer_lora_config = LoraConfig(
28 r=64,
29 lora_alpha=128,
30 init_lora_weights="gaussian",
31 target_modules=target_modules,
32 )
33
34 pipe.transformer = get_peft_model(pipe.transformer, transformer_lora_config)
35
36 model_state_dict = load_file(ckpt_path, device="cpu")
37 pipe.transformer.load_state_dict(model_state_dict, strict=False)
38 print(f"successfully load lora: {ckpt_path}")
39
40 return pipe
41
42model_id = "Qwen/Qwen-Image"
43lora_ckpt_path = "CIawevy/QwenImage-TextPecker-SQPA"
44device = "cuda" if torch.cuda.is_available() else "cpu"
45
46negative_prompt = " "
47aspect_ratios = {
48 "1:1": (1328, 1328),
49 "16:9": (1664, 928),
50 "9:16": (928, 1664),
51}
52width, height = aspect_ratios["1:1"]
53num_inference_steps = 50
54true_cfg_scale = 4.0
55
56pipe = load_model(model_id, lora_ckpt_path)
57
58prompt = 'a weathered cave explorers journal page, with the phrase "TextPecker" prominently written in faded ink, surrounded by sketches of ancient ruins and cryptic symbols, under a dim, mystical light.'
59image = pipe(
60 prompt=prompt,
61 negative_prompt=negative_prompt,
62 width=width,
63 height=height,
64 num_inference_steps=num_inference_steps,
65 true_cfg_scale=true_cfg_scale,
66 generator=torch.Generator(device=device).manual_seed(42)
67).images[0]
68
69image.save("TextPecker_qwen_demo.png")
70print("img has been saved to: TextPecker_qwen_demo.png")1@article{zhu2026TextPecker,
2 title = {TextPecker: Rewarding Structural Anomaly Quantification for Enhancing Visual Text Rendering},
3 author = {Zhu, Hanshen and Liu, Yuliang and Wu, Xuecheng and Wang, An-Lan and Feng, Hao and Yang, Dingkang and Feng, Chao and Huang, Can and Tang, Jingqun and Bai, Xiang},
4 journal = {arXiv preprint arXiv:2602.20903},
5 year = {2026}
6}