Skip to content

Instantly share code, notes, and snippets.

@Norod
Created December 9, 2022 17:15
Show Gist options
  • Select an option

  • Save Norod/f9c48e98a50d4ad5a81c8a985feaa0ff to your computer and use it in GitHub Desktop.

Select an option

Save Norod/f9c48e98a50d4ad5a81c8a985feaa0ff to your computer and use it in GitHub Desktop.
The Huggingface example code for stabilityai/stable-diffusion-2-depth (with GPU, CPU and multiple image results)
import torch
import requests
from PIL import Image
from diffusers import StableDiffusionDepth2ImgPipeline
def main():
device = "cuda" if torch.cuda.is_available() else "cpu"
dtype = torch.float16 if device == "cuda" else torch.float32
pipe = StableDiffusionDepth2ImgPipeline.from_pretrained(
"stabilityai/stable-diffusion-2-depth",
torch_dtype=dtype
).to(device)
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
init_image = Image.open(requests.get(url, stream=True).raw)
prompt = "two tigers"
n_propmt = "bad, deformed, ugly, bad anotomy"
prompt_to_print = prompt.replace(" ", "_")
init_image.save(f"init-{prompt_to_print}-Depth2Img.jpg")
num_of_samples = 2
result = pipe(prompt=[prompt] * num_of_samples, image=init_image, negative_prompt=[n_propmt] * num_of_samples, strength=0.7)
images = result["images"]
for i, image in enumerate(images):
prompt_to_print = str(i) + "-" + prompt
output_file = prompt_to_print.replace(" ", "_") + "-Depth2Img" + ".jpg"
image.save(output_file)
print("Saved: " + str(output_file))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment