diffusers/tools
1128
1#!/usr/bin/env python32from diffusers import StableDiffusionControlNetPipeline, ControlNetModel3import requests4import torch5from PIL import Image6from io import BytesIO7 8url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"9 10response = requests.get(url)11init_image = Image.open(BytesIO(response.content)).convert("RGB")12init_image = init_image.resize((512, 512))13 14path = "runwayml/stable-diffusion-v1-5"15 16run_compile = True # Set True / False17controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny", torch_dtype=torch.float16)18pipe = StableDiffusionControlNetPipeline.from_pretrained(19 path, controlnet=controlnet, torch_dtype=torch.float1620)21 22pipe = pipe.to("cuda:0")23pipe.unet.to(memory_format=torch.channels_last)24pipe.controlnet.to(memory_format=torch.channels_last)25 26if run_compile:27 print("Run torch compile")28 pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)29 pipe.controlnet = torch.compile(pipe.controlnet, mode="reduce-overhead", fullgraph=True)30 31prompt = "ghibli style, a fantasy landscape with castles"32 33for _ in range(3):34 image = pipe(prompt=prompt, image=init_image).images[0]35 