Using Lora with StableDiffusion (from code)

MLBoy
2 min readFeb 25, 2024

Taste changes with one line

Stable Diffusion is an AI that generates images from text, while
Lora can add additional taste to the original model.

For example this

a machine learning boy

This is an image generated using Stable Diffusion from the text,

If you load Lora, who learned watercolor painting, into Stable Diffusion and generate it with the same text,

It will have a taste like this.

Lora is easy to use with WebUI (a library that allows you to use Stable Diffusion in a GUI), but
it is also easy to use from code.

how

Find your favorite style of Lora on Hugging Face , etc. and download it. It is a file with the extension .safetensor.
You can also make your own.

This is the watercolor painting of Lora from earlier.

pipe.load_lora_weights(".", weight_name="watercolor_v1_sdxl.safetensors")

Stable Diffusion is now loaded with Lora.
All that’s left to do is generate it.

from diffusers import StableDiffusionXLPipeline
pipe = StableDiffusionXLPipeline.from_pretrained('stabilityai/stable-diffusion-xl-base-1.0')
pipe.to( "cuda" )
pipe.load_lora_weights(".", weight_name="watercolor_v1_sdxl.safetensors")
image = pipe("delicious pizza",latents=latents).images[0]
image.save('pizza.png')

🐣

--

--