OrlandoHugBot commited on
Commit
191855e
·
verified ·
1 Parent(s): 31b4eeb

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +223 -0
README.md ADDED
@@ -0,0 +1,223 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ ---
13
+
14
+ ## 🌌 UniPic2-Metaquery-9B
15
+
16
+ <div align="center">
17
+ <img src="skywork-logo.png" alt="Skywork Logo" width="500">
18
+ </div>
19
+
20
+ <p align="center">
21
+ <a href="https://github.com/SkyworkAI/UniPic">
22
+ <img src="https://img.shields.io/badge/GitHub-UniPic-blue?logo=github" alt="GitHub Repo">
23
+ </a>
24
+ <a href="https://github.com/SkyworkAI/UniPic/stargazers">
25
+ <img src="https://img.shields.io/github/stars/SkyworkAI/UniPic?style=social" alt="GitHub Stars">
26
+ </a>
27
+ <a href="https://github.com/SkyworkAI/UniPic/network/members">
28
+ <img src="https://img.shields.io/github/forks/SkyworkAI/UniPic?style=social" alt="GitHub Forks">
29
+ </a>
30
+ </p>
31
+
32
+
33
+
34
+ ## 📖 Introduction
35
+
36
+ **UniPic2-Metaquery-9B** is a 9B-parameter unified multimodal model built on Qwen2.5-VL-Instruct and SD3.5-Medium. It supports end-to-end understanding /t2i generation /image editing. It runs smoothly on a single 16 GB consumer GPU.
37
+
38
+ <div align="center"> <img src="teaser.png" alt="Model Teaser" width="720"> </div>
39
+
40
+ ## 📊 Benchmarks
41
+
42
+ **UniPic2-Metaquery-9B** w/o GRPO achieves competitive results across a variety of vision-language tasks:
43
+
44
+ | Task | Score |
45
+ |--------------------|--------|
46
+ | 🧠 **GenEval** | 0.86 |
47
+ | 🖼️ **DPG-Bench** | 83.63 |
48
+ | ✂️ **GEditBench-EN** | 6.90 |
49
+ | 🧪 **ImgEdit-Bench** | 4.10 |
50
+
51
+
52
+
53
+ ## 🧠 Usage
54
+
55
+ ### 1. Clone the Repository
56
+
57
+ ```bash
58
+ git clone https://github.com/SkyworkAI/UniPic
59
+ cd UniPic
60
+ ```
61
+
62
+ ### 2. Set Up the Environment
63
+ ```bash
64
+ conda create -n unipic python=3.10.14
65
+ conda activate unipic
66
+ pip install -r requirements.txt
67
+ ```
68
+
69
+
70
+ ### 3.Text-to-Image Generation
71
+ ```bash
72
+ import torch
73
+ from PIL import Image
74
+ from unipicv2.pipeline_stable_diffusion_3_kontext import StableDiffusion3KontextPipeline
75
+ from unipicv2.transformer_sd3_kontext import SD3Transformer2DKontextModel
76
+ from unipicv2.stable_diffusion_3_conditioner import StableDiffusion3Conditioner
77
+ from transformers import Qwen2_5_VLForConditionalGeneration, Qwen2_5_VLProcessor
78
+ from diffusers import FlowMatchEulerDiscreteScheduler, AutoencoderKL
79
+
80
+ # Load model components
81
+ pretrained_model_name_or_path = "/path/to/unipicv2_qwen2_5_vl_7b_sd_3_5m_kontext"
82
+
83
+ transformer = SD3Transformer2DKontextModel.from_pretrained(
84
+ pretrained_model_name_or_path, subfolder="transformer", torch_dtype=torch.bfloat16).cuda()
85
+
86
+ vae = AutoencoderKL.from_pretrained(
87
+ pretrained_model_name_or_path, subfolder="vae", torch_dtype=torch.bfloat16).cuda()
88
+
89
+ # Load Qwen2.5-VL model
90
+ lmm = Qwen2_5_VLForConditionalGeneration.from_pretrained(
91
+ "Qwen/Qwen2.5-VL-7B-Instruct",
92
+ torch_dtype=torch.bfloat16,
93
+ attn_implementation="flash_attention_2").cuda()
94
+
95
+ processor = Qwen2_5_VLProcessor.from_pretrained("Qwen/Qwen2.5-VL-7B-Instruct")
96
+ processor.chat_template = processor.chat_template.replace(
97
+ "{% if loop.first and message['role'] != 'system' %}<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n{% endif %}",
98
+ "")
99
+
100
+ conditioner = StableDiffusion3Conditioner.from_pretrained(
101
+ pretrained_model_name_or_path, subfolder="conditioner", torch_dtype=torch.bfloat16).cuda()
102
+
103
+ scheduler = FlowMatchEulerDiscreteScheduler.from_pretrained(pretrained_model_name_or_path, subfolder="scheduler")
104
+
105
+ # Create pipeline (note: text encoders set to None)
106
+ pipeline = StableDiffusion3KontextPipeline(
107
+ transformer=transformer, vae=vae,
108
+ text_encoder=None, tokenizer=None,
109
+ text_encoder_2=None, tokenizer_2=None,
110
+ text_encoder_3=None, tokenizer_3=None,
111
+ scheduler=scheduler)
112
+
113
+ # Prepare prompts
114
+ prompt = 'a pig with wings and a top hat flying over a happy futuristic scifi city'
115
+ negative_prompt = ''
116
+
117
+ messages = [[{"role": "user", "content": [{"type": "text", "text": f'Generate an image: {txt}'}]}]
118
+ for txt in [prompt, negative_prompt]]
119
+
120
+ texts = [processor.apply_chat_template(msg, tokenize=False, add_generation_prompt=True) for msg in messages]
121
+ inputs = processor(text=texts, images=None, videos=None, padding=True, return_tensors="pt").to("cuda")
122
+
123
+ # Process with Qwen2.5-VL
124
+ input_ids, attention_mask = inputs.input_ids, inputs.attention_mask
125
+ input_ids = torch.cat([input_ids, input_ids.new_zeros(2, conditioner.config.num_queries)], dim=1)
126
+ attention_mask = torch.cat([attention_mask, attention_mask.new_ones(2, conditioner.config.num_queries)], dim=1)
127
+ inputs_embeds = lmm.get_input_embeddings()(input_ids)
128
+ inputs_embeds[:, -conditioner.config.num_queries:] = conditioner.meta_queries[None].expand(2, -1, -1)
129
+
130
+ outputs = lmm.model(inputs_embeds=inputs_embeds, attention_mask=attention_mask, use_cache=False)
131
+ hidden_states = outputs.last_hidden_state[:, -conditioner.config.num_queries:]
132
+ prompt_embeds, pooled_prompt_embeds = conditioner(hidden_states)
133
+
134
+ # Generate image
135
+ image = pipeline(
136
+ prompt_embeds=prompt_embeds[:1],
137
+ pooled_prompt_embeds=pooled_prompt_embeds[:1],
138
+ negative_prompt_embeds=prompt_embeds[1:],
139
+ negative_pooled_prompt_embeds=pooled_prompt_embeds[1:],
140
+ height=512, width=384,
141
+ num_inference_steps=50,
142
+ guidance_scale=3.5,
143
+ generator=torch.Generator(device=transformer.device).manual_seed(42)
144
+ ).images[0]
145
+
146
+ image.save("text2image.png")
147
+ ```
148
+
149
+
150
+ ### 4. Image Editing
151
+ ```bash
152
+ # Load image for editing
153
+ image = Image.open("text2image.png")
154
+ image = fix_longer_edge(image, image_size=512)
155
+
156
+ prompt = "remove the pig's hat"
157
+ 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."
158
+
159
+ # Prepare messages with image input
160
+ messages = [[{"role": "user", "content": [{"type": "image", "image": image}, {"type": "text", "text": txt}]}]
161
+ for txt in [prompt, negative_prompt]]
162
+
163
+ texts = [processor.apply_chat_template(msg, tokenize=False, add_generation_prompt=True) for msg in messages]
164
+
165
+ min_pixels = max_pixels = int(image.height * 28 / 32 * image.width * 28 / 32)
166
+ inputs = processor(
167
+ text=texts, images=[image]*2,
168
+ min_pixels=min_pixels, max_pixels=max_pixels,
169
+ videos=None, padding=True, return_tensors="pt").to("cuda")
170
+
171
+ # Process with vision understanding
172
+ input_ids, attention_mask, pixel_values, image_grid_thw = \
173
+ inputs.input_ids, inputs.attention_mask, inputs.pixel_values, inputs.image_grid_thw
174
+
175
+ input_ids = torch.cat([input_ids, input_ids.new_zeros(2, conditioner.config.num_queries)], dim=1)
176
+ attention_mask = torch.cat([attention_mask, attention_mask.new_ones(2, conditioner.config.num_queries)], dim=1)
177
+ inputs_embeds = lmm.get_input_embeddings()(input_ids)
178
+ inputs_embeds[:, -conditioner.config.num_queries:] = conditioner.meta_queries[None].expand(2, -1, -1)
179
+
180
+ image_embeds = lmm.visual(pixel_values, grid_thw=image_grid_thw)
181
+ image_token_id = processor.tokenizer.convert_tokens_to_ids('<|image_pad|>')
182
+ inputs_embeds[input_ids == image_token_id] = image_embeds
183
+
184
+ lmm.model.rope_deltas = None
185
+ outputs = lmm.model(inputs_embeds=inputs_embeds, attention_mask=attention_mask,
186
+ image_grid_thw=image_grid_thw, use_cache=False)
187
+
188
+ hidden_states = outputs.last_hidden_state[:, -conditioner.config.num_queries:]
189
+ prompt_embeds, pooled_prompt_embeds = conditioner(hidden_states)
190
+
191
+ # Generate edited image
192
+ edited_image = pipeline(
193
+ image=image,
194
+ prompt_embeds=prompt_embeds[:1],
195
+ pooled_prompt_embeds=pooled_prompt_embeds[:1],
196
+ negative_prompt_embeds=prompt_embeds[1:],
197
+ negative_pooled_prompt_embeds=pooled_prompt_embeds[1:],
198
+ height=image.height, width=image.width,
199
+ num_inference_steps=50,
200
+ guidance_scale=3.5,
201
+ generator=torch.Generator(device=transformer.device).manual_seed(42)
202
+ ).images[0]
203
+
204
+ edited_image.save("image_editing.png")
205
+ ```
206
+
207
+ ## 📄 License
208
+
209
+ This model is released under the MIT License.
210
+
211
+ ## Citation
212
+ If you use Skywork-UniPic in your research, please cite:
213
+ ```
214
+ @misc{wang2025skyworkunipicunifiedautoregressive,
215
+ title={Skywork UniPic: Unified Autoregressive Modeling for Visual Understanding and Generation},
216
+ 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},
217
+ year={2025},
218
+ eprint={2508.03320},
219
+ archivePrefix={arXiv},
220
+ primaryClass={cs.CV},
221
+ url={https://arxiv.org/abs/2508.03320},
222
+ }
223
+ ```