Spaces:
No application file
No application file
| from diffusers import StableDiffusionControlNetPipeline, ControlNetModel, UniPCMultistepScheduler | |
| from diffusers.utils import load_image | |
| import numpy as np | |
| import torch | |
| import cv2 | |
| from PIL import Image | |
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
| device | |
| # load control net and stable diffusion v1-5 | |
| base_model_path = "runwayml/stable-diffusion-v1-5" | |
| controlnet_path = "LuyangZ/controlnet_Neufert4_64_100" | |
| controlnet = ControlNetModel.from_pretrained(controlnet_path, use_safetensors=True) | |
| pipe = StableDiffusionControlNetPipeline.from_pretrained( | |
| base_model_path, controlnet=controlnet, use_safetensors=True) | |
| # speed up diffusion process with faster scheduler and memory optimization | |
| pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config) | |
| pipe = pipe.to(device) | |
| # pipe.set_progress_bar_config(disable=True) | |
| # generate image | |
| control_image = load_image("C:/Users/luyan/diffusers/examples/controlnet/Test/1030_4465_8e4734b920a2be9f0e7d85b734b7fa7e.png") | |
| # speed up diffusion process with faster scheduler and memory optimization | |
| pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config) | |
| # generate image | |
| control_image = load_image("C:/Users/luyan/diffusers/examples/controlnet/Test/2179_9871_432b1fbf16d04cd8371cd9ece543cb28.png") | |
| # pipe = pipe.to(device) | |
| # generator = torch.manual_seed(0) | |
| # generator = torch.Generator(device=device).manual_seed(999) | |
| # generator = None | |
| # images = [] | |
| # for i in range(5): | |
| # image = pipe( | |
| # "floor plan,2 bedrooms", num_inference_steps=100, image=control_image | |
| # ).images[0] | |
| # images.append(image) | |
| generator = torch.Generator(device=device).manual_seed(333) | |
| images = [] | |
| for i in range(5): | |
| image = pipe( | |
| "floor plan,2 bedrooms", num_inference_steps=20, generator=generator, image=control_image | |
| ).images[0] | |
| images.append(image) | |
| def make_grid(images, size=512): | |
| """Given a list of PIL images, stack them together into a line for easy viewing""" | |
| output_im = Image.new("RGB", (size * len(images), size)) | |
| for i, im in enumerate(images): | |
| output_im.paste(im.resize((size, size)), (i * size, 0)) | |
| return output_im | |
| make_grid(images, size=512) |