Buckets:

hf-doc-build/doc / diffusers /main /ar /api /loaders /textual_inversion.md
mishig's picture
|
download
raw
11.2 kB
# Textual Inversion
تعد Textual Inversion طريقة تدريب لتخصيص النماذج من خلال تعلّم تمثيلات نصية جديدة من عدد قليل من الصور المثلثية. الملف الناتج عن التدريب صغير للغاية (بضعة كيلوبايتات) ويمكن تحميل التمثيلات الجديدة في مُشفِّر النص.
يوفر `TextualInversionLoaderMixin` دالةً لتحميل تمثيلات Textual Inversion من Diffusers وAutomatic1111 إلى مُشفِّر النص، وتحميل رمز خاص لتفعيل هذه التمثيلات.
> [!TIP]
> للتعرف على المزيد حول كيفية تحميل تضمينات Textual Inversion، راجد دليل التحميل الخاص بـ [Textual Inversion](../../using-diffusers/legacy_adapters#textual-inversion).
## TextualInversionLoaderMixin[[diffusers.loaders.TextualInversionLoaderMixin]]
#### diffusers.loaders.TextualInversionLoaderMixin[[diffusers.loaders.TextualInversionLoaderMixin]]
```python
diffusers.loaders.TextualInversionLoaderMixin()
```
[المصدر](https://github.com/huggingface/diffusers/blob/main/src/diffusers/loaders/textual_inversion.py#L118)
حمّل رموز ودمج Textual Inversion إلى مُرمّز النص ومُشفّر النص.
#### load_textual_inversion[[diffusers.loaders.TextualInversionLoaderMixin.load_textual_inversion]]
```python
load_textual_inversion(pretrained_model_name_or_path: str | list[str] | dict[str, torch.Tensor] | list[dict[str, torch.Tensor]], token: str | list[str] | None = None, tokenizer: 'PreTrainedTokenizer' | None = None, text_encoder: 'PreTrainedModel' | None = None, **kwargs)
```
[المصدر](https://github.com/huggingface/diffusers/blob/main/src/diffusers/loaders/textual_inversion.py#L271)
**المعاملات:**
pretrained_model_name_or_path (`str` أو `os.PathLike` أو `list[str or os.PathLike]` أو `Dict` أو `list[Dict]`) : يمكن أن يكون أيًا مما يلي أو قائمة به: - نص، وهو *معرف النموذج* (مثل `sd-concepts-library/low-poly-hd-logos-icons`) لنموذج مُدرَّب مسبقًا مستضاف على Hub. - مسار إلى *مجلد* (مثل `./my_text_inversion_directory/`) يحتوي على أوزان العكس النصي. - مسار إلى *ملف* (مثل `./my_text_inversions.pt`) يحتوي على أوزان العكس النصي. - [قاموس حالة torch](https://pytorch.org/tutorials/beginner/saving_loading_models.html#what-is-a-state-dict).
token (`str` أو `list[str]`، *اختياري*) : تجاوز الرمز المستخدم لأوزان الانعكاس النصي. إذا كان `pretrained_model_name_or_path` قائمة، فيجب أن يكون `token` أيضًا قائمة بطول متساوٍ.
text_encoder ([CLIPTextModel](https://huggingface.co/docs/transformers/main/en/model_doc/clip#transformers.CLIPTextModel)، *اختياري*) : مُشفِّر نص ثابت ([clip-vit-large-patch14](https://huggingface.co/openai/clip-vit-large-patch14)). إذا لم يُحدد، ستأخذ الدالة self.tokenizer.
tokenizer ([CLIPTokenizer](https://huggingface.co/docs/transformers/main/en/model_doc/clip#transformers.CLIPTokenizer)، *اختياري*) : `CLIPTokenizer` لتجزئة النص. إذا لم يُحدد، ستأخذ الدالة self.tokenizer.
weight_name (`str`، *اختياري*) : اسم ملف الأوزان المخصص. يجب استخدام هذا عند: - كان ملف الانعكاس النصي المحفوظ بصيغة 🤗 Diffusers، لكنه حُفظ باسم أوزان محدد مثل `text_inv.bin`. - كان ملف الانعكاس النصي المحفوظ بصيغة Automatic1111.
cache_dir (`str | os.PathLike`، *اختياري*) : مسار إلى مجلد حيث تُخزَّن مؤقتًا إعدادات النموذج المُسبق التحميل المُحمَّل إذا لم يُستخدم التخزين المؤقت القياسي.
force_download (`bool`، *اختياري*، القيمة الافتراضية `False`) : ما إذا كان يجب إجبار إعادة تنزيل أوزان النموذج وملفات الإعداد، مع تجاوز النسخ المخزنة مؤقتًا إذا كانت موجودة.
proxies (`dict[str, str]`، *اختياري*) : قاموس لخوادم الوكيل (proxy servers) للاستخدام حسب البروتوكول أو نقطة النهاية، على سبيل المثال، `{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}`. تُستخدم هذه الوكلاء في كل طلب.
local_files_only (`bool`، *اختياري*، القيمة الافتراضية `False`) : ما إذا كان سيتم تحميل أوزان النموذج المحلية وملفات الإعداد فقط أم لا. إذا تم ضبطه على `True`، فلن يتم تنزيل النموذج من Hub.
hf_token (`str` أو *bool*، *اختياري*) : الرمز الذي يُستخدم كتوكن مصادقة HTTP bearer للملفات البعيدة. إذا كان `True`، يُستخدم التوكن المولّد من `diffusers-cli login` (المخزَّن في `~/.huggingface`).
revision (`str`، *اختياري*، القيمة الافتراضية `"main"`) : إصدار النموذج المحدد الذي سيتم استخدامه. يمكن أن يكون اسم فرع، أو اسم وسم، أو معرف التزامن، أو أي معرّف مسموح به بواسطة Git.
subfolder (`str`، *اختياري*، القيمة الافتراضية `""`) : موقع المجلد الفرعي لملف النموذج داخل مستودع نموذج أكبر على Hub أو محليًا.
mirror (`str`، *اختياري*) : مصدر مرآة لحل مشاكل إمكانية الوصول إذا كنت تقوم بتنزيل نموذج في الصين. نحن لا نضمن حداثة المصدر أو أمانه، وينبغي لك الرجوع إلى موقع المرآة لمزيد من المعلومات.
حمّل ترميزات Textual Inversion في مُشفِّر النص الخاص بـ [StableDiffusionPipeline](/docs/diffusers/main/ar/api/pipelines/stable_diffusion/text2img#diffusers.StableDiffusionPipeline) (يُؤمَّن دعم كلا الصيغتين: 🤗 Diffusers وAutomatic1111).
مثال:
لتحميل متجه تضمين Textual Inversion بصيغة 🤗 Diffusers:
```py
from diffusers import StableDiffusionPipeline
import torch
model_id = "stable-diffusion-v1-5/stable-diffusion-v1-5"
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to("cuda")
pipe.load_textual_inversion("sd-concepts-library/cat-toy")
prompt = "A <cat-toy> backpack"
image = pipe(prompt, num_inference_steps=50).images[0]
image.save("cat-backpack.png")
```
لتحميل متجه تضمين Textual Inversion بصيغة Automatic1111، تأكد أولاً من تنزيل المتجه (على سبيل المثال من [civitAI](https://civitai.com/models/3036?modelVersionId=9857)) ثم قم بتحميل المتجه
محليًا:
```py
from diffusers import StableDiffusionPipeline
import torch
model_id = "stable-diffusion-v1-5/stable-diffusion-v1-5"
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to("cuda")
pipe.load_textual_inversion("./charturnerv2.pt", token="charturnerv2")
prompt = "charturnerv2, multiple views of the same character in the same outfit, a character turnaround of a woman wearing a black jacket and red shirt, best quality, intricate details."
image = pipe(prompt, num_inference_steps=50).images[0]
image.save("character.png")
```
#### maybe_convert_prompt[[diffusers.loaders.TextualInversionLoaderMixin.maybe_convert_prompt]]
```python
maybe_convert_prompt(prompt: str | list[str], tokenizer: 'PreTrainedTokenizer')
```
[المصدر](https://github.com/huggingface/diffusers/blob/main/src/diffusers/loaders/textual_inversion.py#L123)
**المعاملات:**
prompt (`str` أو قائمة من `str`) : الأمر أو الأوامر التي تُوجّه توليد الصورة.
tokenizer (`PreTrainedTokenizer`) : المُرمِّز المسؤول عن ترميز المُوجِّه إلى رموز الإدخال.
**يعيد:** `str` أو قائمة من `str`
الموجّه المحوّل
تعالج الأوامر النصية التي تتضمن رمزًا خاصًا يقابل تضمين انعكاس نصي متعدد المتجهات ليتم استبداله بعدة رموز خاصة، حيث يقابل كل رمز واحدًا من المتجهات. إذا لم يحتوي الأمر النصي على رمز انعكاس نصي، أو كان رمز الانعكاس النصي متجهًا واحدًا، يُعاد الأمر النصي المُدخل كما هو.
#### unload_textual_inversion[[diffusers.loaders.TextualInversionLoaderMixin.unload_textual_inversion]]
```python
unload_textual_inversion(tokens: str | list[str] | None = None, tokenizer: 'PreTrainedTokenizer' | None = None, text_encoder: 'PreTrainedModel' | None = None)
```
[المصدر](https://github.com/huggingface/diffusers/blob/main/src/diffusers/loaders/textual_inversion.py#L467)
إزالة تضمينات Textual Inversion من مُشفّر النص الخاص بـ [StableDiffusionPipeline](/docs/diffusers/main/ar/api/pipelines/stable_diffusion/text2img#diffusers.StableDiffusionPipeline)
مثال:
```py
from diffusers import AutoPipelineForText2Image
import torch
pipeline = AutoPipelineForText2Image.from_pretrained("stable-diffusion-v1-5/stable-diffusion-v1-5")
# Example 1
pipeline.load_textual_inversion("sd-concepts-library/gta5-artwork")
pipeline.load_textual_inversion("sd-concepts-library/moeb-style")
# Remove all token embeddings
pipeline.unload_textual_inversion()
# Example 2
pipeline.load_textual_inversion("sd-concepts-library/moeb-style")
pipeline.load_textual_inversion("sd-concepts-library/gta5-artwork")
# Remove just one token
pipeline.unload_textual_inversion("<moe-bius>")
# Example 3: unload from SDXL
pipeline = AutoPipelineForText2Image.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0")
embedding_path = hf_hub_download(
repo_id="linoyts/web_y2k", filename="web_y2k_emb.safetensors", repo_type="model"
)
# load embeddings to the text encoders
state_dict = load_file(embedding_path)
# load embeddings of text_encoder 1 (CLIP ViT-L/14)
pipeline.load_textual_inversion(
state_dict["clip_l"],
tokens=["<s0>", "<s1>"],
text_encoder=pipeline.text_encoder,
tokenizer=pipeline.tokenizer,
)
# load embeddings of text_encoder 2 (CLIP ViT-G/14)
pipeline.load_textual_inversion(
state_dict["clip_g"],
tokens=["<s0>", "<s1>"],
text_encoder=pipeline.text_encoder_2,
tokenizer=pipeline.tokenizer_2,
)
# Unload explicitly from both text encoders and tokenizers
pipeline.unload_textual_inversion(
tokens=["<s0>", "<s1>"], text_encoder=pipeline.text_encoder, tokenizer=pipeline.tokenizer
)
pipeline.unload_textual_inversion(
tokens=["<s0>", "<s1>"], text_encoder=pipeline.text_encoder_2, tokenizer=pipeline.tokenizer_2
)
```

Xet Storage Details

Size:
11.2 kB
·
Xet hash:
f5e77fcd39e72e37d314624334aee6f30b22cfe619c6a13035d2f8e6f2c7faa6

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.