File size: 7,030 Bytes
82e7434
 
 
 
 
 
 
 
 
 
 
49b976a
82e7434
 
52527db
82e7434
 
 
 
 
 
 
 
 
f33b237
 
 
 
 
 
82e7434
41c8d05
 
82e7434
 
 
5bb7431
 
82e7434
8d686db
82e7434
 
 
ffc83f8
82e7434
 
 
8d686db
 
 
 
82e7434
 
 
 
 
 
 
 
 
fc56bea
82e7434
 
 
 
5bb7431
82e7434
 
 
 
 
 
 
7cbbca1
 
 
 
c6c298d
7cbbca1
 
 
c6c298d
 
 
 
7cbbca1
 
8100d9d
c6c298d
 
7cbbca1
 
 
c6c298d
 
7cbbca1
 
 
c6c298d
 
7cbbca1
 
 
c6c298d
 
7cbbca1
 
8100d9d
 
 
7cbbca1
 
 
 
 
 
 
 
 
 
 
 
36a7d63
7cbbca1
 
 
 
 
 
 
8100d9d
82e7434
 
 
 
 
7cbbca1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8100d9d
 
82e7434
 
 
 
 
 
 
 
 
7c1cd08
 
 
 
 
 
 
 
 
 
c64315f
 
 
82e7434
c64315f
82e7434
 
c64315f
82e7434
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
---
pipeline_tag: any-to-any
library_name: transformers
tags:
- text-to-image
- image-editing
- image-understanding
- vision-language
- multimodal
- autoregressive
- unified-model
license: mit
---

## 🌌 UniPic2-SD3.5M-Kontext-2B

<div align="center">   
  <img src="skywork-logo.png" alt="Skywork Logo" width="500"> 
</div>

<p align="center">
  <a href="https://github.com/SkyworkAI/UniPic">
    <img src="https://img.shields.io/badge/GitHub-UniPic-blue?logo=github" alt="GitHub Repo">
  </a>
  <a href="https://github.com/SkyworkAI/UniPic/stargazers">
    <img src="https://img.shields.io/github/stars/SkyworkAI/UniPic?style=social" alt="GitHub Stars">
  </a>
  <a href="https://github.com/SkyworkAI/UniPic/network/members">
    <img src="https://img.shields.io/github/forks/SkyworkAI/UniPic?style=social" alt="GitHub Forks">
  </a>
</p>



## 📖 Introduction

**UniPic2-SD3.5M-Kontext-2B** is a post-trained **T2I
** model built on the SD3.5-Medium. It focuses on text-to-image generation and image editing, delivering strong quality with a fast generation speed. It runs smoothly on a single 16 GB consumer GPU.

<div align="center"> <img src="teaser.png" alt="Model Teaser" width="720"> </div>

## 📊 Benchmarks

**UniPic2-SD3.5M-Kontext-2B** w/o GRPO achieves competitive results across a variety of vision-language tasks:

| Task               | Score  |
|--------------------|--------|
| 🧠 **GenEval**       | 0.83   |
| 🖼️ **DPG-Bench**     | 83.7   |
| ✂️ **GEditBench-EN** | 6.31   |
| 🧪 **ImgEdit-Bench** | 3.95   |



## 🧠 Usage

### 1. Clone the Repository

```bash
git clone https://github.com/SkyworkAI/UniPic
cd UniPic-2
```

### 2. Set Up the Environment
```bash
conda create -n unipic python=3.10
conda activate unipic
pip install -r requirements.txt
```


### 3.Text-to-Image Generation
```bash
import torch
from PIL import Image
from unipicv2.pipeline_stable_diffusion_3_kontext import StableDiffusion3KontextPipeline
from unipicv2.transformer_sd3_kontext import SD3Transformer2DKontextModel
from diffusers import FlowMatchEulerDiscreteScheduler, AutoencoderKL
from transformers import CLIPTextModelWithProjection, CLIPTokenizer, T5EncoderModel, T5TokenizerFast

# Load model components
pretrained_model_name_or_path = "Skywork/UniPic2-SD3.5M-Kontext-2B"

transformer = SD3Transformer2DKontextModel.from_pretrained(
        pretrained_model_name_or_path, subfolder="transformer", torch_dtype=torch.bfloat16).cuda()

vae = AutoencoderKL.from_pretrained(
    pretrained_model_name_or_path, subfolder="vae",
    torch_dtype=torch.bfloat16, device_map="auto", low_cpu_mem_usage=True
).cuda()

# Load text encoders
text_encoder = CLIPTextModelWithProjection.from_pretrained(
    pretrained_model_name_or_path, subfolder="text_encoder", torch_dtype=torch.bfloat16, device_map="auto", low_cpu_mem_usage=True
).cuda()
tokenizer = CLIPTokenizer.from_pretrained(pretrained_model_name_or_path, subfolder="tokenizer")

text_encoder_2 = CLIPTextModelWithProjection.from_pretrained(
    pretrained_model_name_or_path, subfolder="text_encoder_2", torch_dtype=torch.bfloat16, device_map="auto", low_cpu_mem_usage=True
).cuda()
tokenizer_2 = CLIPTokenizer.from_pretrained(pretrained_model_name_or_path, subfolder="tokenizer_2")

text_encoder_3 = T5EncoderModel.from_pretrained(
    pretrained_model_name_or_path, subfolder="text_encoder_3", torch_dtype=torch.bfloat16, device_map="auto", low_cpu_mem_usage=True
).cuda()
tokenizer_3 = T5TokenizerFast.from_pretrained(pretrained_model_name_or_path, subfolder="tokenizer_3")

scheduler = FlowMatchEulerDiscreteScheduler.from_pretrained(
    pretrained_model_name_or_path, subfolder="scheduler"
)

# Create pipeline
pipeline = StableDiffusion3KontextPipeline(
    transformer=transformer, vae=vae,
    text_encoder=text_encoder, tokenizer=tokenizer,
    text_encoder_2=text_encoder_2, tokenizer_2=tokenizer_2,
    text_encoder_3=text_encoder_3, tokenizer_3=tokenizer_3,
    scheduler=scheduler)

# Generate image
image = pipeline(
    prompt='a pig with wings and a top hat flying over a happy futuristic scifi city',
    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.',
    height=512, width=384,
    num_inference_steps=50,
    guidance_scale=3.5,
    generator=torch.Generator(device=transformer.device).manual_seed(42)
).images[0]

image.save("text2image.png")

```


### 4.  Image Editing
```bash
# Load and preprocess image
def fix_longer_edge(x, image_size, factor=32):
    w, h = x.size
    if w >= h:
        target_w = image_size
        target_h = h * (target_w / w)
        target_h = round(target_h / factor) * factor
    else:
        target_h = image_size
        target_w = w * (target_h / h)
        target_w = round(target_w / factor) * factor
    x = x.resize(size=(target_w, target_h))
    return x

image = Image.open("text2image.png")
image = fix_longer_edge(image, image_size=512)

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."

# Edit image
edited_image = pipeline(
    image=image,
    prompt="remove the pig's hat",
    negative_prompt=negative_prompt,
    height=image.height, width=image.width,
    num_inference_steps=50,
    guidance_scale=3.5,
    generator=torch.Generator(device=transformer.device).manual_seed(42)
).images[0]

edited_image.save("edited_img.png")

```

## 📄 License

This model is released under the MIT License.

## Citation
If you use Skywork-UniPic in your research, please cite:
```
@misc{wang2025skyworkunipicunifiedautoregressive,
      title={Skywork UniPic: Unified Autoregressive Modeling for Visual Understanding and Generation}, 
      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},
      year={2025},
      eprint={2508.03320},
      archivePrefix={arXiv},
      primaryClass={cs.CV},
      url={https://arxiv.org/abs/2508.03320}, 
}

@misc{wei2025skyworkunipic20building,
      title={Skywork UniPic 2.0: Building Kontext Model with Online RL for Unified Multimodal Model}, 
      author={Hongyang Wei and Baixin Xu and Hongbo Liu and Cyrus Wu and Jie Liu and Yi Peng and Peiyu Wang and Zexiang Liu and Jingwen He and Yidan Xietian and Chuanxin Tang and Zidong Wang and Yichen Wei and Liang Hu and Boyi Jiang and William Li and Ying He and Yang Liu and Xuchen Song and Eric Li and Yahui Zhou},
      year={2025},
      eprint={2509.04548},
      archivePrefix={arXiv},
      primaryClass={cs.CV},
      url={https://arxiv.org/abs/2509.04548}, 
}
```