Buckets:
مرخّص بموجب ترخيص Apache الإصدار 2.0 (المسمى "الترخيص")
لا يجوز لك استخدام هذا الملف إلا وفقاً لشروط الترخيص.
يمكنك الحصول على نسخة من الترخيص في
http://www.apache.org/licenses/LICENSE-2.0
ما لم يقتضِ ذلك القانون المعمول به أو ما اتُّفق عليه كتابياً، البرمجيات
تُوزَّع بموجب الرخصة على أساس "كما هي"،
بلا أي ضمانات أو شروط من أي نوع، سواء صريحة أو ضمنية.
راجع الترخيص لمعرفة اللغة المحددة التي تحكم الصلاحيات و
الشروط المحددة في الترخيص.
-->
OmniGen
OmniGen: توليد الصور الموحّد من BAAI، بقلم Shitao Xiao وYueze Wang وJunjie Zhou وHuaying Yuan وXingrun Xing وRuiran Yan وChaofan Li وShuting Wang وTiejun Huang وZheng Liu.
ملخص الورقة البحثية هو:
أدى ظهور نماذج اللغة الكبيرة (LLMs) إلى توحيد مهام توليد اللغة وقلب موازين التفاعل بين الإنسان والآلة. ومع ذلك، في مجال توليد الصور، لا يزال النموذج الموحّد القادر على التعامل مع مهام متنوعة ضمن إطار عمل واحد غير مستكشف إلى حد كبير. في هذا العمل، نعرّف OmniGen، وهو نموذج انتشار جديد لتوليد الصور الموحّد. يتسم OmniGen بالخصائص التالية: 1) التوحيد: لا يُظهر OmniGen قدرات توليد الصور من النص فحسب، بل يدعم بشكل جوهري مهامًا لاحقة متنوعة، مثل تحرير الصور، والتوليد الموجّه بالموضوع، والتوليد الشرطي البصري. 2) البساطة: البنية المعمارية لـ OmniGen مبسطة للغاية، مما يلغي الحاجة إلى إضافات خارجية. علاوة على ذلك، مقارنةً بنماذج الانتشار الحالية، فهو أكثر سهولة في الاستخدام، ويمكنه إكمال المهام المعقدة من البداية إلى النهاية عبر التعليمات دون الحاجة إلى خطوات وسيطة إضافية، مما يبسّط بشكل كبير سير عمل توليد الصور. 3) نقل المعرفة: بفضل التعلم في صيغة موحدة، ينقل OmniGen المعرفة بفعالية عبر مهام مختلفة، ويتعامل مع المهام والمجالات غير المرئية، ويعرض قدرات جديدة. كما نستكشف قدرات الاستدلال للنموذج والتطبيقات المحتملة لآلية سلسلة الأفكار. يمثل هذا العمل المحاولة الأولى لنموذج توليد صور عام، وسننشر مواردنا على https://github.com/VectorSpaceLab/OmniGen لتعزيز التطورات المستقبلية.
تأكد من الاطلاع على دليل المجدولين لمعرفة كيفية استكشاف التوازن بين سرعة المجدول وجودة النتائج، واطّلع على قسم إعادة استخدام المكونات عبر خطوط الأنابيب لمعرفة كيفية تحميل نفس المكونات بكفاءة في خطوط أنابيب متعددة.
تمت مساهمة خط المعالجة هذا من قِبل staoxiao. يمكن العثور على قاعدة الكود الأصلية هنا. يمكن العثور على الأوزان الأصلية تحت hf.co/shitao.
تحميل نقاط التفتيش للنماذج
قد تُخزَّن أوزان النموذج في مجلدات فرعية منفصلة على Hub أو محليًا، وفي هذه الحالة، يجب عليك استخدام طريقة from_pretrained().
import torch
from diffusers import OmniGenPipeline
pipe = OmniGenPipeline.from_pretrained("Shitao/OmniGen-v1-diffusers", dtype=torch.bfloat16)
النص إلى صورة
لتوليد الصور من النص، مرّر موجهًا نصيًا. افتراضيًا، يولّد OmniGen صورة بأبعاد 1024x1024. يمكنك تجربة ضبط المعاملين height وwidth لتوليد صور بأحجام مختلفة.
import torch
from diffusers import OmniGenPipeline
pipe = OmniGenPipeline.from_pretrained(
"Shitao/OmniGen-v1-diffusers",
dtype=torch.bfloat16
)
pipe.to("cuda") # or "mps", "xpu", "cpu"
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=torch.Generator(device="cpu").manual_seed(111),
).images[0]
image.save("output.png")
تعديل الصورة
يدعم OmniGen المدخلات متعددة الوسائط. عند تضمين صورة في المدخلات، يجب إضافة عنصر نائب <img><|image_1|></img> في نص الأمر لتمثيل الصورة. يُوصى بتفعيل use_input_image_size_as_output للحفاظ على حجم الصورة المعدلة مطابقًا لحجم الصورة الأصلية.
import torch
from diffusers import OmniGenPipeline
from diffusers.utils import load_image
pipe = OmniGenPipeline.from_pretrained(
"Shitao/OmniGen-v1-diffusers",
dtype=torch.bfloat16
)
pipe.to("cuda") # or "mps", "xpu", "cpu"
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=torch.Generator(device="cpu").manual_seed(222)
).images[0]
image.save("output.png")
الصورة الأصلية
صورة معدّلة
يتميز OmniGen ببعض الميزات المثيرة للاهتمام، مثل الاستدلال البصري، كما هو موضح في المثال أدناه.
prompt="If the woman is thirsty, what should she take? Find it in the image and highlight it in blue. <img><|image_1|></img>"
input_images=[load_image("https://raw.githubusercontent.com/VectorSpaceLab/OmniGen/main/imgs/docs_img/edit.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=torch.Generator(device="cpu").manual_seed(0)
).images[0]
image.save("output.png")
التوليد القابل للتحكم
يمكن لـ OmniGen التعامل مع عدة مهام كلاسيكية في مجال رؤية الحاسوب. كما هو موضح أدناه، يمكن لـ OmniGen اكتشاف هياكل العظام البشرية في الصور المدخلة، والتي يمكن استخدامها كحالات تحكم لتوليد صور جديدة.
import torch
from diffusers import OmniGenPipeline
from diffusers.utils import load_image
pipe = OmniGenPipeline.from_pretrained(
"Shitao/OmniGen-v1-diffusers",
dtype=torch.bfloat16
)
pipe.to("cuda") # or "mps", "xpu", "cpu"
prompt="Detect the skeleton of human in this image: <img><|image_1|></img>"
input_images=[load_image("https://raw.githubusercontent.com/VectorSpaceLab/OmniGen/main/imgs/docs_img/edit.png")]
image1 = pipe(
prompt=prompt,
input_images=input_images,
guidance_scale=2,
img_guidance_scale=1.6,
use_input_image_size_as_output=True,
generator=torch.Generator(device="cpu").manual_seed(333)
).images[0]
image1.save("image1.png")
prompt="Generate a new photo using the following picture and text as conditions: <img><|image_1|></img>\n A young boy is sitting on a sofa in the library, holding a book. His hair is neatly combed, and a faint smile plays on his lips, with a few freckles scattered across his cheeks. The library is quiet, with rows of shelves filled with books stretching out behind him."
input_images=[load_image("https://raw.githubusercontent.com/VectorSpaceLab/OmniGen/main/imgs/docs_img/skeletal.png")]
image2 = pipe(
prompt=prompt,
input_images=input_images,
guidance_scale=2,
img_guidance_scale=1.6,
use_input_image_size_as_output=True,
generator=torch.Generator(device="cpu").manual_seed(333)
).images[0]
image2.save("image2.png")
الصورة الأصلية
هيكل عظمي تم اكتشافه
من هيكل عظمي إلى صورة
يمكن لـ OmniGen أيضًا استخدام المعلومات ذات الصلة من الصور المدخلة مباشرةً لتوليد صور جديدة.
import torch
from diffusers import OmniGenPipeline
from diffusers.utils import load_image
pipe = OmniGenPipeline.from_pretrained(
"Shitao/OmniGen-v1-diffusers",
dtype=torch.bfloat16
)
pipe.to("cuda") # or "mps", "xpu", "cpu"
prompt="Following the pose of this image <img><|image_1|></img>, generate a new photo: A young boy is sitting on a sofa in the library, holding a book. His hair is neatly combed, and a faint smile plays on his lips, with a few freckles scattered across his cheeks. The library is quiet, with rows of shelves filled with books stretching out behind him."
input_images=[load_image("https://raw.githubusercontent.com/VectorSpaceLab/OmniGen/main/imgs/docs_img/edit.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=torch.Generator(device="cpu").manual_seed(0)
).images[0]
image.save("output.png")
صورة مولّدة
الحفاظ على المعرّف والكائن
يمكن لـ OmniGen توليد صور متعددة بناءً على الأشخاص والأشياء الموجودة في الصورة المدخلة، كما يدعم إدخال عدة صور في الوقت نفسه. بالإضافة إلى ذلك، يمكن لـ OmniGen استخراج الأشياء المطلوبة من صورة تحتوي على عدة أشياء بناءً على التعليمات.
import torch
from diffusers import OmniGenPipeline
from diffusers.utils import load_image
pipe = OmniGenPipeline.from_pretrained(
"Shitao/OmniGen-v1-diffusers",
dtype=torch.bfloat16
)
pipe.to("cuda") # or "mps", "xpu", "cpu"
prompt="A man and a woman are sitting at a classroom desk. The man is the man with yellow hair in <img><|image_1|></img>. The woman is the woman on the left of <img><|image_2|></img>"
input_image_1 = load_image("https://raw.githubusercontent.com/VectorSpaceLab/OmniGen/main/imgs/docs_img/3.png")
input_image_2 = load_image("https://raw.githubusercontent.com/VectorSpaceLab/OmniGen/main/imgs/docs_img/4.png")
input_images=[input_image_1, input_image_2]
image = pipe(
prompt=prompt,
input_images=input_images,
height=1024,
width=1024,
guidance_scale=2.5,
img_guidance_scale=1.6,
generator=torch.Generator(device="cpu").manual_seed(666)
).images[0]
image.save("output.png")
input_image_1
input_image_2
صورة مولّدة
import torch
from diffusers import OmniGenPipeline
from diffusers.utils import load_image
pipe = OmniGenPipeline.from_pretrained(
"Shitao/OmniGen-v1-diffusers",
dtype=torch.bfloat16
)
pipe.to("cuda") # or "mps", "xpu", "cpu"
prompt="A woman is walking down the street, wearing a white long-sleeve blouse with lace details on the sleeves, paired with a blue pleated skirt. The woman is <img><|image_1|></img>. The long-sleeve blouse and a pleated skirt are <img><|image_2|></img>."
input_image_1 = load_image("https://raw.githubusercontent.com/VectorSpaceLab/OmniGen/main/imgs/docs_img/emma.jpeg")
input_image_2 = load_image("https://raw.githubusercontent.com/VectorSpaceLab/OmniGen/main/imgs/docs_img/dress.jpg")
input_images=[input_image_1, input_image_2]
image = pipe(
prompt=prompt,
input_images=input_images,
height=1024,
width=1024,
guidance_scale=2.5,
img_guidance_scale=1.6,
generator=torch.Generator(device="cpu").manual_seed(666)
).images[0]
image.save("output.png")
صورة الشخص
clothe image
صورة مولّدة
التحسين عند استخدام صور متعددة
في مهمة النص إلى صورة، يتطلب OmniGen الحد الأدنى من تكاليف الذاكرة والوقت (9 جيجابايت من الذاكرة و31 ثانية لصورة بدقة 1024x1024 على معالج رسومي A800). ومع ذلك، عند استخدام صور كمدخلات، تزداد التكلفة الحسابية.
فيما يلي بعض الإرشادات لمساعدتك على تقليل التكاليف الحسابية عند استخدام صور متعددة. تم إجراء التجارب على معالج رسومي A800 مع صورتين مدخلتين.
كما هو الحال مع خطوط الأنابيب الأخرى، يمكنك تقليل استهلاك الذاكرة عن طريق تفريغ النموذج: pipe.enable_model_cpu_offload() أو pipe.enable_sequential_cpu_offload() . في OmniGen، يمكنك أيضًا تقليل العبء الحسابي عن طريق تقليل max_input_image_size. يُظهر الجدول أدناه استهلاك الذاكرة لمختلف أحجام الصور:
| الطريقة | استهلاك الذاكرة |
|---|---|
| max_input_image_size=1024 | 40GB |
| max_input_image_size=512 | 17GB |
| max_input_image_size=256 | 14GB |
OmniGenPipeline[[diffusers.OmniGenPipeline]]
diffusers.OmniGenPipeline[[diffusers.OmniGenPipeline]]
diffusers.OmniGenPipeline(transformer: OmniGenTransformer2DModel, scheduler: FlowMatchEulerDiscreteScheduler, vae: AutoencoderKL, tokenizer: LlamaTokenizer)
المعاملات:
transformer (OmniGenTransformer2DModel) : بنية Transformer ذاتية التوليد (Autoregressive) مخصصة لـ OmniGen.
scheduler (FlowMatchEulerDiscreteScheduler) : مُجدوِل يُستخدم بالاقتران مع transformer لإزالة الضوضاء من كامنات الصورة المُرمَّزة.
vae (AutoencoderKL) : نموذج الترميز التحويلي التبايني (VAE) لتشفير وفك تشفير الصور إلى تمثيلات كامنة ومنها.
tokenizer (LlamaTokenizer) : مُجزِّئ نصوص من الفئة LlamaTokenizer.
خط أنابيب OmniGen لتوليد الصور من الوسائط المتعددة.
مرجع: https://huggingface.co/papers/2409.11340
call[[diffusers.OmniGenPipeline.call]]
__call__(prompt: str | list[str], input_images: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, list[PIL.Image.Image], list[numpy.ndarray], list[torch.Tensor], list[typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, list[PIL.Image.Image], list[numpy.ndarray], list[torch.Tensor]]]] = None, height: int | None = None, width: int | None = None, num_inference_steps: int = 50, max_input_image_size: int = 1024, timesteps: list[int] = None, guidance_scale: float = 2.5, img_guidance_scale: float = 1.6, use_input_image_size_as_output: bool = False, num_images_per_prompt: int | None = 1, generator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = None, latents: typing.Optional[torch.Tensor] = None, output_type: str | None = 'pil', return_dict: bool = True, callback_on_step_end: typing.Optional[typing.Callable[[int, int], NoneType]] = None, callback_on_step_end_tensor_inputs: list[str] = ['latents'])
المعاملات:
prompt (str أو list[str]، اختياري) : الأمر أو الأوامر التي تُوجّه توليد الصورة. إذا تضمن المدخل صورًا، فيجب إضافة العلامات الموضعية <img><|image_i|></img> في الأمر للإشارة إلى موضع الصورة رقم i.
input_images (PipelineImageInput أو list[PipelineImageInput]، اختياري) : قائمة الصور المدخلة. سنستبدل "<|image_i|>" في الأمر بالصورة رقم i في القائمة.
height (int، اختياري، الافتراضي هو self.unet.config.sample_size * self.vae_scale_factor) : ارتفاع الصورة المولّدة بالبكسلات. يتم ضبطه على 1024 افتراضيًا للحصول على أفضل النتائج.
width (int، اختياري، القيمة الافتراضية هي self.unet.config.sample_size * self.vae_scale_factor) : عرض الصورة المولدة بالبكسلات. يتم ضبطه على 1024 افتراضيًا للحصول على أفضل النتائج.
num_inference_steps (int، اختياري، القيمة الافتراضية 50) : عدد خطوات إزالة الضوضاء. تؤدي زيادة عدد خطوات إزالة الضوضاء عادةً إلى صورة ذات جودة أعلى على حساب بطء الاستدلال.
max_input_image_size (int، اختياري، القيمة الافتراضية 1024) : الحد الأقصى لحجم الصورة المدخلة، والذي سيُستخدم لقص الصورة المدخلة إلى الحد الأقصى
timesteps (list[int]، اختياري) : خطوات زمنية مخصصة لاستخدامها في عملية إزالة الضوضاء مع المجدولين الذين يدعمون معامل timesteps في طريقة set_timesteps الخاصة بهم. إذا لم يُعرَّف، فسيتم استخدام السلوك الافتراضي عند تمرير num_inference_steps. يجب أن تكون بترتيب تنازلي.
guidance_scale (float، اختياري، القيمة الافتراضية 2.5) : مقياس التوجيه كما هو مُعرَّف في Classifier-Free Diffusion Guidance. يُعرَّف guidance_scale على أنه w من المعادلة 2. في Imagen Paper. يتم تفعيل مقياس التوجيه عن طريق تعيين guidance_scale > 1. يشجع مقياس التوجيه الأعلى على توليد صور مرتبطة ارتباطًا وثيقًا بنص prompt، وعادةً على حساب انخفاض جودة الصورة.
img_guidance_scale (float، اختياري، القيمة الافتراضية 1.6) : يُعرَّف كمعادلة 3 في Instrucpix2pix.
use_input_image_size_as_output (bool، القيمة الافتراضية False) : ما إذا كان سيتم استخدام حجم الصورة المدخلة كحجم للصورة المُخرجة، وهو ما يمكن استخدامه لإدخال صورة واحدة، على سبيل المثال في مهمة تحرير الصور
num_images_per_prompt (int، اختياري، القيمة الافتراضية 1) : عدد الصور التي سيتم توليدها لكل موجه.
generator (torch.Generator أو list[torch.Generator]، اختياري) : مولّد واحد أو قائمة من مولّدات torch لجعل التوليد حتميًا.
latents (torch.Tensor، اختياري) : كامنات ضجيج مُولّدة مسبقًا، مُستخرجة من توزيع غاوسي، تُستخدم كمدخلات لتوليد الصور. يمكن استخدامها لتعديل نفس التوليد باستخدام أوامر مختلفة. إذا لم تُقدَّم، سيتم توليد كامنات من خلال الاستخراج باستخدام generator العشوائي المُقدَّم.
output_type (str، اختياري، القيمة الافتراضية "pil") : صيغة الصورة الناتجة عن عملية التوليد. اختر بين PIL: PIL.Image.Image أو np.array.
return_dict (bool، اختياري، الافتراضي True) : ما إذا كان يجب إرجاع ~pipelines.flux.FluxPipelineOutput بدلاً من tuple عادي.
callback_on_step_end (Callable، اختياري) : دالة تُستدعى في نهاية كل خطوة إزالة ضوضاء أثناء الاستدلال. تُستدعى الدالة بالحجج التالية: callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict). سيتضمن callback_kwargs قائمة بجميع المصفوفات كما حددتها callback_on_step_end_tensor_inputs.
callback_on_step_end_tensor_inputs (list، اختياري) : قائمة مدخلات المصفوفات (tensors) لدالة callback_on_step_end. سيتم تمرير المصفوفات المحددة في القائمة كحجة callback_kwargs. لن تتمكن من تضمين سوى المتغيرات المدرجة في خاصية ._callback_tensor_inputs لفئة خط الأنابيب (pipeline) الخاص بك.
يعيد: ImagePipelineOutput أو tuple
ImagePipelineOutput إذا كان return_dict هو True، وإلا فسيكون tuple. عند إرجاع tuple، يكون العنصر الأول قائمة تحتوي على الصور المولّدة.
الدالة التي تُستدعى عند استدعاء خط الأنابيب للتوليد.
أمثلة:
>>> import torch
>>> from diffusers import OmniGenPipeline
>>> pipe = OmniGenPipeline.from_pretrained("Shitao/OmniGen-v1-diffusers", torch_dtype=torch.bfloat16)
>>> pipe.to("cuda")
>>> prompt = "A cat holding a sign that says hello world"
>>> # Depending on the variant being used, the pipeline call will slightly vary.
>>> # Refer to the pipeline documentation for more details.
>>> image = pipe(prompt, num_inference_steps=50, guidance_scale=2.5).images[0]
>>> image.save("output.png")
يعيد: ImagePipelineOutput أو tuple: إذا كان return_dict هو True، يُعاد ImagePipelineOutput، وإلا يُعاد tuple يكون عنصره الأول قائمة بالصور المولّدة.
encode_input_images[[diffusers.OmniGenPipeline.encode_input_images]]
encode_input_images(input_pixel_values: list[torch.Tensor], device: typing.Optional[torch.device] = None, dtype: typing.Optional[torch.dtype] = None)
المعاملات:
input_pixel_values : بكسل مُعايَر للصور المُدخَلة
device --
الحصول على التضمين المستمر للصور المدخلة بواسطة VAE
يعيد: torch.Tensor
Xet Storage Details
- Size:
- 25.2 kB
- Xet hash:
- 4a868e56035e4b27211af957f9ee5e8ec893c014ead4a4df349e3e0aed05debb
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.