Textual Inversion¶
Textual Inversion is a training method for personalizing models by learning new text embeddings from a few example images. The file produced from training is extremely small (a few KBs) and the new embeddings can be loaded into the text encoder.
TextualInversionLoaderMixin
provides a function for loading Textual Inversion embeddings from Diffusers and Automatic1111 into the text encoder and loading a special token to activate the embeddings.
Tip
To learn more about how to load Textual Inversion embeddings, see the Textual Inversion loading guide.
mindone.diffusers.loaders.textual_inversion.TextualInversionLoaderMixin
¶
Load Textual Inversion tokens and embeddings to the tokenizer and text encoder.
Source code in mindone/diffusers/loaders/textual_inversion.py
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 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 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 |
|
mindone.diffusers.loaders.textual_inversion.TextualInversionLoaderMixin.load_textual_inversion(pretrained_model_name_or_path, token=None, tokenizer=None, text_encoder=None, **kwargs)
¶
Load Textual Inversion embeddings into the text encoder of [StableDiffusionPipeline
]
PARAMETER | DESCRIPTION |
---|---|
pretrained_model_name_or_path |
Can be either one of the following or a list of them:
TYPE:
|
token |
Override the token to use for the textual inversion weights. If
TYPE:
|
text_encoder |
Frozen text-encoder (clip-vit-large-patch14). If not specified, function will take self.tokenizer.
TYPE:
|
tokenizer |
A
TYPE:
|
weight_name |
Name of a custom weight file. This should be used when:
TYPE:
|
cache_dir |
Path to a directory where a downloaded pretrained model configuration is cached if the standard cache is not used.
TYPE:
|
force_download |
Whether or not to force the (re-)download of the model weights and configuration files, overriding the cached versions if they exist.
TYPE:
|
proxies |
A dictionary of proxy servers to use by protocol or endpoint, for example,
TYPE:
|
local_files_only |
Whether to only load local model weights and configuration files or not. If set to
TYPE:
|
token |
The token to use as HTTP bearer authorization for remote files. If
TYPE:
|
revision |
The specific model version to use. It can be a branch name, a tag name, a commit id, or any identifier allowed by Git.
TYPE:
|
subfolder |
The subfolder location of a model file within a larger model repository on the Hub or locally.
TYPE:
|
mirror |
Mirror source to resolve accessibility issues if you're downloading a model in China. We do not guarantee the timeliness or safety of the source, and you should refer to the mirror site for more information.
TYPE:
|
To load a Textual Inversion embedding vector in ๐ค Diffusers format:
from mindone.diffusers import StableDiffusionPipeline
import mindspore
model_id = "runwayml/stable-diffusion-v1-5"
pipe = StableDiffusionPipeline.from_pretrained(model_id, mindspore_dtype=mindspore.float16)
pipe.load_textual_inversion("sd-concepts-library/cat-toy")
prompt = "A <cat-toy> backpack"
image = pipe(prompt, num_inference_steps=50)[0][0]
image.save("cat-backpack.png")
Source code in mindone/diffusers/loaders/textual_inversion.py
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 |
|
mindone.diffusers.loaders.textual_inversion.TextualInversionLoaderMixin.maybe_convert_prompt(prompt, tokenizer)
¶
Processes prompts that include a special token corresponding to a multi-vector textual inversion embedding to be replaced with multiple special tokens each corresponding to one of the vectors. If the prompt has no textual inversion token or if the textual inversion token is a single vector, the input prompt is returned.
PARAMETER | DESCRIPTION |
---|---|
prompt |
The prompt or prompts to guide the image generation.
TYPE:
|
tokenizer |
The tokenizer responsible for encoding the prompt into input tokens.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
|
Source code in mindone/diffusers/loaders/textual_inversion.py
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 |
|
mindone.diffusers.loaders.textual_inversion.TextualInversionLoaderMixin.unload_textual_inversion(tokens=None, tokenizer=None, text_encoder=None)
¶
Unload Textual Inversion embeddings from the text encoder of [StableDiffusionPipeline
]
Example:
from mindone.diffusers import AutoPipelineForText2Image
import mindspore as ms
pipeline = AutoPipelineForText2Image.from_pretrained("runwayml/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"],
token=["<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"],
token=["<s0>", "<s1>"],
text_encoder=pipeline.text_encoder_2,
tokenizer=pipeline.tokenizer_2,
)
# Unload explicitly from both text encoders abd 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
)
Source code in mindone/diffusers/loaders/textual_inversion.py
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 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 |
|