Views
No views yet

1import torch
2from transformers import Qwen2_5_VLForConditionalGeneration, AutoTokenizer, AutoProcessor
3from qwen_vl_utils import process_vision_info
4
5model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
6 "X-GenGroup/PaCo-Reward-7B", torch_dtype="bfloat16", device_map="auto"
7)
8
9# default processer
10processor = AutoProcessor.from_pretrained("X-GenGroup/PaCo-Reward-7B")
11
12image1 = 'https://huggingface.co/X-GenGroup/PaCo-Reward-7B/resolve/main/images/image_1.jpg'
13image2 = 'https://huggingface.co/X-GenGroup/PaCo-Reward-7B/resolve/main/images/image_2.jpg'
14
15main_prompt = 'Generate multiple images portraying a medical scene of a dentist in scrubs. The images should include activities such as explaining oral hygiene to a patient, taking X-rays of teeth, cleaning teeth in a dental office, and filling a cavity during an appointment. The setting should depict a realistic dental clinic.'
16text_prompt = (
17 f"Given two subfigures generated based on the theme: \"{main_prompt}\", "
18 f"do the two images maintain consistency in terms of style, logic and identity? "
19 f"Answer \"Yes\" and \"No\" first, and then provide detailed reasons."
20)
21
22# Example: Compare whether two images are visually consistent
23messages_1 = [
24 {
25 "role": "user",
26 "content": [
27 {"type": "image", "image": image1},
28 {"type": "image", "image": image2},
29 {"type": "text", "text": text_prompt},
30 ],
31 }
32]
33
34# Preparation for inference
35text = processor.apply_chat_template(
36 messages_1, tokenize=False, add_generation_prompt=True
37)
38image_inputs, video_inputs = process_vision_info(messages_1)
39inputs = processor(
40 text=[text],
41 images=image_inputs,
42 videos=video_inputs,
43 padding=True,
44 return_tensors="pt",
45)
46inputs = inputs.to("cuda")
47
48# Inference: Calculate consistency score
49# Get logits for first token
50with torch.no_grad():
51 outputs = model(**inputs)
52 first_token_logits = outputs.logits[0, -1, :] # Last position of prompt
53
54# Get token IDs for "Yes" and "No"
55yes_id = processor.tokenizer.encode("Yes", add_special_tokens=False)[0]
56no_id = processor.tokenizer.encode("No", add_special_tokens=False)[0]
57
58# Calculate probability
59yes_logit = first_token_logits[yes_id]
60no_logit = first_token_logits[no_id]
61yes_prob = torch.exp(yes_logit) / (torch.exp(yes_logit) + torch.exp(no_logit))
62
63print(f"Consistency Score (Yes Conditional Probability): {yes_prob.item():.4f}")
64
65# Inference: Generate detailed reasons
66generated_ids = model.generate(**inputs, max_new_tokens=512)
67generated_ids_trimmed = [
68 out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
69]
70output_text = processor.batch_decode(
71 generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
72)
73print(output_text[0])| Model | Type | HuggingFace |
|---|---|---|
| PaCo-Reward-7B | Reward Model | 🤗 Link |
| PaCo-Reward-7B-Lora | Reward Model (LoRA) | 🤗 Link |
| PaCo-FLUX.1-dev | T2I Model (LoRA) | 🤗 Link |
| PaCo-FLUX.1-Kontext-dev | Image Editing Model (LoRA) | 🤗 Link |
| PaCo-QwenImage-Edit | Image Editing Model (LoRA) | 🤗 Link |
1@misc{ping2025pacorladvancingreinforcementlearning,
2 title={PaCo-RL: Advancing Reinforcement Learning for Consistent Image Generation with Pairwise Reward Modeling},
3 author={Bowen Ping and Chengyou Jia and Minnan Luo and Changliang Xia and Xin Shen and Zhuohang Dang and Hangwei Qian},
4 year={2025},
5 eprint={2512.04784},
6 archivePrefix={arXiv},
7 primaryClass={cs.CV},
8 url={https://arxiv.org/abs/2512.04784},
9}