OmniGen¶
OmniGen: Unified Image Generation from BAAI, by Shitao Xiao, Yueze Wang, Junjie Zhou, Huaying Yuan, Xingrun Xing, Ruiran Yan, Chaofan Li, Shuting Wang, Tiejun Huang, Zheng Liu.
The abstract from the paper is:
The emergence of Large Language Models (LLMs) has unified language generation tasks and revolutionized human-machine interaction. However, in the realm of image generation, a unified model capable of handling various tasks within a single framework remains largely unexplored. In this work, we introduce OmniGen, a new diffusion model for unified image generation. OmniGen is characterized by the following features: 1) Unification: OmniGen not only demonstrates text-to-image generation capabilities but also inherently supports various downstream tasks, such as image editing, subject-driven generation, and visual conditional generation. 2) Simplicity: The architecture of OmniGen is highly simplified, eliminating the need for additional plugins. Moreover, compared to existing diffusion models, it is more user-friendly and can complete complex tasks end-to-end through instructions without the need for extra intermediate steps, greatly simplifying the image generation workflow. 3) Knowledge Transfer: Benefit from learning in a unified format, OmniGen effectively transfers knowledge across different tasks, manages unseen tasks and domains, and exhibits novel capabilities. We also explore the model’s reasoning capabilities and potential applications of the chain-of-thought mechanism. This work represents the first attempt at a general-purpose image generation model, and we will release our resources at https://github.com/VectorSpaceLab/OmniGen to foster future advancements.
Tip
Make sure to check out the Schedulers guide to learn how to explore the tradeoff between scheduler speed and quality, and see the reuse components across pipelines section to learn how to efficiently load the same components into multiple pipelines.
Tip
This pipeline was contributed by staoxiao. The original codebase can be found here. The original weights can be found under hf.co/shitao.
Inference¶
First, load the pipeline:
import mindspore as ms
from diffusers import OmniGenPipeline
pipe = OmniGenPipeline.from_pretrained("Shitao/OmniGen-v1-diffusers", mindspore_dtype=ms.bfloat16)
For text-to-image, pass a text prompt. By default, OmniGen generates a 1024x1024 image.
You can try setting the height
and width
parameters to generate images with different size.
prompt = "Realistic photo. A young woman sits on a sofa, holding a book and facing the camera. She wears delicate silver hoop earrings adorned with tiny, sparkling diamonds that catch the light, with her long chestnut hair cascading over her shoulders. Her eyes are focused and gentle, framed by long, dark lashes. She is dressed in a cozy cream sweater, which complements her warm, inviting smile. Behind her, there is a table with a cup of water in a sleek, minimalist blue mug. The background is a serene indoor setting with soft natural light filtering through a window, adorned with tasteful art and flowers, creating a cozy and peaceful ambiance. 4K, HD."
image = pipe(
prompt=prompt,
height=1024,
width=1024,
guidance_scale=3,
generator=np.random.Generator(np.random.PCG64(111)),
).[0][0]
image.save("output.png")
OmniGen supports multimodal inputs.
When the input includes an image, you need to add a placeholder <img><|image_1|></img>
in the text prompt to represent the image.
It is recommended to enable use_input_image_size_as_output
to keep the edited image the same size as the original image.
prompt="<img><|image_1|></img> Remove the woman's earrings. Replace the mug with a clear glass filled with sparkling iced cola."
input_images=[load_image("https://raw.githubusercontent.com/VectorSpaceLab/OmniGen/main/imgs/docs_img/t2i_woman_with_book.png")]
image = pipe(
prompt=prompt,
input_images=input_images,
guidance_scale=2,
img_guidance_scale=1.6,
use_input_image_size_as_output=True,
generator=np.random.Generator(np.random.PCG64(222)),).[0][0]
image.save("output.png")
mindone.diffusers.OmniGenPipeline
¶
Bases: DiffusionPipeline
The OmniGen pipeline for multimodal-to-image generation.
Reference: https://arxiv.org/pdf/2409.11340
PARAMETER | DESCRIPTION |
---|---|
transformer |
Autoregressive Transformer architecture for OmniGen.
TYPE:
|
scheduler |
A scheduler to be used in combination with
TYPE:
|
vae |
Variational Auto-Encoder (VAE) Model to encode and decode images to and from latent representations.
TYPE:
|
tokenizer |
Text tokenizer of class. LlamaTokenizer.
TYPE:
|
Source code in mindone/diffusers/pipelines/omnigen/pipeline_omnigen.py
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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 |
|
mindone.diffusers.OmniGenPipeline.__call__(prompt, input_images=None, height=None, width=None, num_inference_steps=50, max_input_image_size=1024, timesteps=None, guidance_scale=2.5, img_guidance_scale=1.6, use_input_image_size_as_output=False, num_images_per_prompt=1, generator=None, latents=None, output_type='pil', return_dict=False, callback_on_step_end=None, callback_on_step_end_tensor_inputs=['latents'])
¶
Function invoked when calling the pipeline for generation.
PARAMETER | DESCRIPTION |
---|---|
prompt |
The prompt or prompts to guide the image generation. If the input includes images, need to add
placeholders
TYPE:
|
input_images |
The list of input images. We will replace the "<|image_i|>" in prompt with the i-th image in list.
TYPE:
|
height |
The height in pixels of the generated image. This is set to 1024 by default for the best results.
TYPE:
|
width |
The width in pixels of the generated image. This is set to 1024 by default for the best results.
TYPE:
|
num_inference_steps |
The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference.
TYPE:
|
max_input_image_size |
the maximum size of input image, which will be used to crop the input image to the maximum size
TYPE:
|
timesteps |
Custom timesteps to use for the denoising process with schedulers which support a
TYPE:
|
guidance_scale |
Guidance scale as defined in Classifier-Free Diffusion Guidance.
TYPE:
|
img_guidance_scale |
Defined as equation 3 in Instrucpix2pix.
TYPE:
|
use_input_image_size_as_output |
whether to use the input image size as the output image size, which can be used for single-image input, e.g., image editing task
TYPE:
|
num_images_per_prompt |
The number of images to generate per prompt.
TYPE:
|
generator |
One or a list of
TYPE:
|
latents |
Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image
generation. Can be used to tweak the same generation with different prompts. If not provided, a latents
tensor will ge generated by sampling using the supplied random
TYPE:
|
output_type |
The output format of the generate image. Choose between
PIL:
TYPE:
|
return_dict |
Whether or not to return a [
TYPE:
|
callback_on_step_end |
A function that calls at the end of each denoising steps during the inference. The function is called
with the following arguments:
TYPE:
|
callback_on_step_end_tensor_inputs |
The list of tensor inputs for the
TYPE:
|
[`~PIPELINES.IMAGEPIPELINEOUTPUT`] OR `TUPLE` | DESCRIPTION |
---|---|
If |
|
where the first element is a list with the generated images. |
Source code in mindone/diffusers/pipelines/omnigen/pipeline_omnigen.py
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 |
|
mindone.diffusers.OmniGenPipeline.disable_vae_slicing()
¶
Disable sliced VAE decoding. If enable_vae_slicing
was previously enabled, this method will go back to
computing decoding in one step.
Source code in mindone/diffusers/pipelines/omnigen/pipeline_omnigen.py
231 232 233 234 235 236 |
|
mindone.diffusers.OmniGenPipeline.disable_vae_tiling()
¶
Disable tiled VAE decoding. If enable_vae_tiling
was previously enabled, this method will go back to
computing decoding in one step.
Source code in mindone/diffusers/pipelines/omnigen/pipeline_omnigen.py
246 247 248 249 250 251 |
|
mindone.diffusers.OmniGenPipeline.enable_vae_slicing()
¶
Enable sliced VAE decoding. When this option is enabled, the VAE will split the input tensor in slices to compute decoding in several steps. This is useful to save some memory and allow larger batch sizes.
Source code in mindone/diffusers/pipelines/omnigen/pipeline_omnigen.py
224 225 226 227 228 229 |
|
mindone.diffusers.OmniGenPipeline.enable_vae_tiling()
¶
Enable tiled VAE decoding. When this option is enabled, the VAE will split the input tensor into tiles to compute decoding and encoding in several steps. This is useful for saving a large amount of memory and to allow processing larger images.
Source code in mindone/diffusers/pipelines/omnigen/pipeline_omnigen.py
238 239 240 241 242 243 244 |
|
mindone.diffusers.OmniGenPipeline.encode_input_images(input_pixel_values, dtype=None)
¶
get the continue embedding of input images by VAE
PARAMETER | DESCRIPTION |
---|---|
input_pixel_values |
normalized pixel of input images
TYPE:
|
Source code in mindone/diffusers/pipelines/omnigen/pipeline_omnigen.py
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
|