OrlandoHugBot commited on
Commit
fd8fb50
·
verified ·
1 Parent(s): 99c0263

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +218 -0
README.md ADDED
@@ -0,0 +1,218 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ pipeline_tag: any-to-any
3
+ library_name: transformers
4
+ tags:
5
+ - text-to-image
6
+ - image-editing
7
+ - image-understanding
8
+ - vision-language
9
+ - multimodal
10
+ - autoregressive
11
+ - unified-model
12
+ license: mit
13
+ ---
14
+
15
+ ## 🌌 UniPic2-Metaquery-GRPO-9B
16
+
17
+ <div align="center">
18
+ <img src="skywork-logo.png" alt="Skywork Logo" width="500">
19
+ </div>
20
+
21
+ <p align="center">
22
+ <a href="https://github.com/SkyworkAI/UniPic">
23
+ <img src="https://img.shields.io/badge/GitHub-UniPic-blue?logo=github" alt="GitHub Repo">
24
+ </a>
25
+ <a href="https://github.com/SkyworkAI/UniPic/stargazers">
26
+ <img src="https://img.shields.io/github/stars/SkyworkAI/UniPic?style=social" alt="GitHub Stars">
27
+ </a>
28
+ <a href="https://github.com/SkyworkAI/UniPic/network/members">
29
+ <img src="https://img.shields.io/github/forks/SkyworkAI/UniPic?style=social" alt="GitHub Forks">
30
+ </a>
31
+ </p>
32
+
33
+
34
+ ## 📖 Introduction
35
+
36
+ **UniPic2-Metaquery-GRPO-9B** is an unified multimodal model trained on UniPic2-Metaquery-9B with enhanced text rendering. It delivers end-to-end image understanding, text-to-image (T2I) generation, and image editing. Requires approximately 40 GB VRAM. For NVIDIA RTX 40-series GPUs, we recommend using the [Skywork/UniPic2-Metaquery-Flash](https://huggingface.co/Skywork/UniPic2-Metaquery-Flash)
37
+
38
+ <div align="center"> <img src="teaser.png" alt="Model Teaser" width="720"> </div>
39
+ <div align="center"> <img src="understanding.png" alt="Model Teaser" width="720"> </div>
40
+
41
+ ## 📊 Benchmarks
42
+
43
+ <div align="center"> <img src="eval.png" alt="Model eval" width="720"> </div>
44
+
45
+
46
+ ## 🧠 Usage
47
+
48
+ ### 1. Clone the Repository
49
+
50
+ ```bash
51
+ git clone https://github.com/SkyworkAI/UniPic
52
+ cd UniPic-2
53
+ ```
54
+
55
+ ### 2. Set Up the Environment
56
+ ```bash
57
+ # Requires ~40GB VRAM; for NVIDIA RTX 40-series GPUs, please use the Flash version
58
+ conda create -n unipic python=3.10
59
+ conda activate unipic
60
+ pip install -r requirements.txt
61
+ ```
62
+
63
+
64
+ ### 3.Text-to-Image Generation
65
+ ```bash
66
+ import torch
67
+ from PIL import Image
68
+ from unipicv2.pipeline_stable_diffusion_3_kontext import StableDiffusion3KontextPipeline
69
+ from unipicv2.transformer_sd3_kontext import SD3Transformer2DKontextModel
70
+ from unipicv2.stable_diffusion_3_conditioner import StableDiffusion3Conditioner
71
+ from transformers import Qwen2_5_VLForConditionalGeneration, Qwen2_5_VLProcessor
72
+ from diffusers import FlowMatchEulerDiscreteScheduler, AutoencoderKL
73
+
74
+ # Load model components
75
+ pretrained_model_name_or_path = "/path/to/Skywork/UniPic2-Metaquery-GRPO-9B"
76
+
77
+ transformer = SD3Transformer2DKontextModel.from_pretrained(
78
+ pretrained_model_name_or_path, subfolder="transformer", torch_dtype=torch.bfloat16).cuda()
79
+
80
+ vae = AutoencoderKL.from_pretrained(
81
+ pretrained_model_name_or_path, subfolder="vae", torch_dtype=torch.bfloat16).cuda()
82
+
83
+ # Load Qwen2.5-VL model
84
+ lmm = Qwen2_5_VLForConditionalGeneration.from_pretrained(
85
+ "Qwen/Qwen2.5-VL-7B-Instruct",
86
+ torch_dtype=torch.bfloat16,
87
+ attn_implementation="flash_attention_2").cuda()
88
+
89
+ processor = Qwen2_5_VLProcessor.from_pretrained("Qwen/Qwen2.5-VL-7B-Instruct")
90
+ processor.chat_template = processor.chat_template.replace(
91
+ "{% if loop.first and message['role'] != 'system' %}<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n{% endif %}",
92
+ "")
93
+
94
+ conditioner = StableDiffusion3Conditioner.from_pretrained(
95
+ pretrained_model_name_or_path, subfolder="conditioner", torch_dtype=torch.bfloat16).cuda()
96
+
97
+ scheduler = FlowMatchEulerDiscreteScheduler.from_pretrained(pretrained_model_name_or_path, subfolder="scheduler")
98
+
99
+ # Create pipeline (note: text encoders set to None)
100
+ pipeline = StableDiffusion3KontextPipeline(
101
+ transformer=transformer, vae=vae,
102
+ text_encoder=None, tokenizer=None,
103
+ text_encoder_2=None, tokenizer_2=None,
104
+ text_encoder_3=None, tokenizer_3=None,
105
+ scheduler=scheduler)
106
+
107
+ # Prepare prompts
108
+ prompt = 'a pig with wings and a top hat flying over a happy futuristic scifi city'
109
+ negative_prompt = ''
110
+
111
+ messages = [[{"role": "user", "content": [{"type": "text", "text": f'Generate an image: {txt}'}]}]
112
+ for txt in [prompt, negative_prompt]]
113
+
114
+ texts = [processor.apply_chat_template(msg, tokenize=False, add_generation_prompt=True) for msg in messages]
115
+ inputs = processor(text=texts, images=None, videos=None, padding=True, return_tensors="pt").to("cuda")
116
+
117
+ # Process with Qwen2.5-VL
118
+ input_ids, attention_mask = inputs.input_ids, inputs.attention_mask
119
+ input_ids = torch.cat([input_ids, input_ids.new_zeros(2, conditioner.config.num_queries)], dim=1)
120
+ attention_mask = torch.cat([attention_mask, attention_mask.new_ones(2, conditioner.config.num_queries)], dim=1)
121
+ inputs_embeds = lmm.get_input_embeddings()(input_ids)
122
+ inputs_embeds[:, -conditioner.config.num_queries:] = conditioner.meta_queries[None].expand(2, -1, -1)
123
+
124
+ outputs = lmm.model(inputs_embeds=inputs_embeds, attention_mask=attention_mask, use_cache=False)
125
+ hidden_states = outputs.last_hidden_state[:, -conditioner.config.num_queries:]
126
+ prompt_embeds, pooled_prompt_embeds = conditioner(hidden_states)
127
+
128
+ # Generate image
129
+ image = pipeline(
130
+ prompt_embeds=prompt_embeds[:1],
131
+ pooled_prompt_embeds=pooled_prompt_embeds[:1],
132
+ negative_prompt_embeds=prompt_embeds[1:],
133
+ negative_pooled_prompt_embeds=pooled_prompt_embeds[1:],
134
+ height=512, width=384,
135
+ num_inference_steps=50,
136
+ guidance_scale=3.5,
137
+ generator=torch.Generator(device=transformer.device).manual_seed(42)
138
+ ).images[0]
139
+
140
+ image.save("text2image.png")
141
+ ```
142
+
143
+
144
+ ### 4. Image Editing
145
+ ```bash
146
+ # Load image for editing
147
+ image = Image.open("text2image.png")
148
+ image = fix_longer_edge(image, image_size=512)
149
+
150
+ prompt = "remove the pig's hat"
151
+ negative_prompt = "blurry, low quality, low resolution, distorted, deformed, broken content, missing parts, damaged details, artifacts, glitch, noise, pixelated, grainy, compression artifacts, bad composition, wrong proportion, incomplete editing, unfinished, unedited areas."
152
+
153
+ # Prepare messages with image input
154
+ messages = [[{"role": "user", "content": [{"type": "image", "image": image}, {"type": "text", "text": txt}]}]
155
+ for txt in [prompt, negative_prompt]]
156
+
157
+ texts = [processor.apply_chat_template(msg, tokenize=False, add_generation_prompt=True) for msg in messages]
158
+
159
+ min_pixels = max_pixels = int(image.height * 28 / 32 * image.width * 28 / 32)
160
+ inputs = processor(
161
+ text=texts, images=[image]*2,
162
+ min_pixels=min_pixels, max_pixels=max_pixels,
163
+ videos=None, padding=True, return_tensors="pt").to("cuda")
164
+
165
+ # Process with vision understanding
166
+ input_ids, attention_mask, pixel_values, image_grid_thw = \
167
+ inputs.input_ids, inputs.attention_mask, inputs.pixel_values, inputs.image_grid_thw
168
+
169
+ input_ids = torch.cat([input_ids, input_ids.new_zeros(2, conditioner.config.num_queries)], dim=1)
170
+ attention_mask = torch.cat([attention_mask, attention_mask.new_ones(2, conditioner.config.num_queries)], dim=1)
171
+ inputs_embeds = lmm.get_input_embeddings()(input_ids)
172
+ inputs_embeds[:, -conditioner.config.num_queries:] = conditioner.meta_queries[None].expand(2, -1, -1)
173
+
174
+ image_embeds = lmm.visual(pixel_values, grid_thw=image_grid_thw)
175
+ image_token_id = processor.tokenizer.convert_tokens_to_ids('<|image_pad|>')
176
+ inputs_embeds[input_ids == image_token_id] = image_embeds
177
+
178
+ lmm.model.rope_deltas = None
179
+ outputs = lmm.model(inputs_embeds=inputs_embeds, attention_mask=attention_mask,
180
+ image_grid_thw=image_grid_thw, use_cache=False)
181
+
182
+ hidden_states = outputs.last_hidden_state[:, -conditioner.config.num_queries:]
183
+ prompt_embeds, pooled_prompt_embeds = conditioner(hidden_states)
184
+
185
+ # Generate edited image
186
+ edited_image = pipeline(
187
+ image=image,
188
+ prompt_embeds=prompt_embeds[:1],
189
+ pooled_prompt_embeds=pooled_prompt_embeds[:1],
190
+ negative_prompt_embeds=prompt_embeds[1:],
191
+ negative_pooled_prompt_embeds=pooled_prompt_embeds[1:],
192
+ height=image.height, width=image.width,
193
+ num_inference_steps=50,
194
+ guidance_scale=3.5,
195
+ generator=torch.Generator(device=transformer.device).manual_seed(42)
196
+ ).images[0]
197
+
198
+ edited_image.save("image_editing.png")
199
+ ```
200
+
201
+
202
+ ## 📄 License
203
+ This model is released under the MIT License.
204
+
205
+
206
+ ## Citation
207
+ If you use Skywork-UniPic in your research, please cite:
208
+ ```
209
+ @misc{wang2025skyworkunipicunifiedautoregressive,
210
+ title={Skywork UniPic: Unified Autoregressive Modeling for Visual Understanding and Generation},
211
+ author={Peiyu Wang and Yi Peng and Yimeng Gan and Liang Hu and Tianyidan Xie and Xiaokun Wang and Yichen Wei and Chuanxin Tang and Bo Zhu and Changshi Li and Hongyang Wei and Eric Li and Xuchen Song and Yang Liu and Yahui Zhou},
212
+ year={2025},
213
+ eprint={2508.03320},
214
+ archivePrefix={arXiv},
215
+ primaryClass={cs.CV},
216
+ url={https://arxiv.org/abs/2508.03320},
217
+ }
218
+ ```