Skip to content

Support loading original stable diffusion checkpoints #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,6 @@ cython_debug/
data/
ddata/
migrators/
data.*.tar
data.*.tar
sd-v1-5.safetensors
out/
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,24 @@ prompts = ["a photograph of an astronaut riding a horse"]
images = pipeline.generate(prompts, models=models, device='cuda', idle_device='cpu')
```

Load an original stable diffusion model in safetensors format:
```py
import safetensors.torch
from stable_diffusion_pytorch import pipeline, convert_from_sdmodel, model_loader

# load the checkpoint file
state_dict = safetensors.torch.load_file('/PATH/TO/sd-v1-5.safetensors')

# split the state_dict into the 4 state_dicts for our models
state_dicts = convert_from_sdmodel.split_state_dict(state_dict)

# create the model objects, and apply the weights in state_dicts
models = model_loader.load_models(state_dicts, 'cpu')

prompts = ["a photograph of an astronaut riding a horse"]
images = pipeline.generate(prompts, models=models, device='cuda', idle_device='cpu')
```

Image-to-image generation:
```py
from PIL import Image
Expand Down
28 changes: 28 additions & 0 deletions convert_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import argparse
import safetensors.torch
from stable_diffusion_pytorch import convert_from_sdmodel

parser = argparse.ArgumentParser(description='Convert an original stable diffusion checkpoint file in safetensor format to 4 separate safetensors for this library.')
parser.add_argument(
"--sd_model", type=str, required=True,
help="Stable Diffusion model to load in safetensors format"
)
parser.add_argument(
"--save_to", type=str, required=True,
help="The prefix of the path to save to, for example \"./mymodel_\" " \
"will save 4 files, mymodel_clip.safetensors, mymodel_decoder.safetensors, " \
"mymodel_encoder.safetensors, and mymodel_diffusion.safetensors"
)

args = parser.parse_args()

# load the checkpoint file
state_dict = safetensors.torch.load_file(args.sd_model)

# convert to the state_dicts format that our library expects
state_dicts = convert_from_sdmodel.split_state_dict(state_dict)

for key in state_dicts:
outPath = args.save_to + key + '.safetensors'
print('Writing', outPath)
safetensors.torch.save_file(state_dicts[key], outPath)
46 changes: 46 additions & 0 deletions demo_sdmodel_generate_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# python3
# A demo of loading a third party model (everythingV5) meant for the original stable diffusion code, and generating images.
import os
import safetensors.torch
import torch
from stable_diffusion_pytorch import pipeline, convert_from_sdmodel, model_loader

# Runs on intel gpu by default, to run on cuda remove the following import and change 'xpu' to 'cuda'.
import intel_extension_for_pytorch as ipex
device = 'xpu'


# where to store generated images
outDir = './out'

# path to SD 1.4/1.5 based model safetensors file
modelPath = './sd-v1-5.safetensors' # download from https://huggingface.co/tabtap/sd-v1-5.safetensor/tree/main

# either float16 or float32
dtype = torch.float16



os.makedirs(outDir, exist_ok=True)

# load the checkpoint file
state_dict = safetensors.torch.load_file(modelPath)

# convert to the state_dicts format that our library expects
state_dicts = convert_from_sdmodel.split_state_dict(state_dict)

# create the model objects, and apply the weights in state_dicts
models = model_loader.load_models(state_dicts, device, dtype)

steps = 40
seed = 12345
prompt = '1girl,cirno,anime style'
negativePrompt = 'bad anatomy,bad hands,missing fingers,extra fingers'

fileName = prompt.replace(' ', '_').replace('\\', '\').replace(':', '⦂').replace(',', '-') + '.' + str(seed) + '.png'

images = pipeline.generate([prompt], uncond_prompts=[negativePrompt],
models=models, n_inference_steps=steps, seed=seed, device=device,
height=768, width=512)

images[0].save(outDir + '/' + fileName)
Loading