Spaces:
No application file
No application file
Create try.py
Browse files
try.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel, UniPCMultistepScheduler
|
| 2 |
+
from diffusers.utils import load_image
|
| 3 |
+
import numpy as np
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
import cv2
|
| 7 |
+
from PIL import Image
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 11 |
+
device
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# load control net and stable diffusion v1-5
|
| 15 |
+
base_model_path = "runwayml/stable-diffusion-v1-5"
|
| 16 |
+
controlnet_path = "LuyangZ/controlnet_Neufert4_64_100"
|
| 17 |
+
controlnet = ControlNetModel.from_pretrained(controlnet_path, use_safetensors=True)
|
| 18 |
+
pipe = StableDiffusionControlNetPipeline.from_pretrained(
|
| 19 |
+
base_model_path, controlnet=controlnet, use_safetensors=True)
|
| 20 |
+
|
| 21 |
+
# speed up diffusion process with faster scheduler and memory optimization
|
| 22 |
+
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
|
| 23 |
+
pipe = pipe.to(device)
|
| 24 |
+
# pipe.set_progress_bar_config(disable=True)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
# generate image
|
| 28 |
+
control_image = load_image("C:/Users/luyan/diffusers/examples/controlnet/Test/1030_4465_8e4734b920a2be9f0e7d85b734b7fa7e.png")
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
# speed up diffusion process with faster scheduler and memory optimization
|
| 33 |
+
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
|
| 34 |
+
|
| 35 |
+
# generate image
|
| 36 |
+
control_image = load_image("C:/Users/luyan/diffusers/examples/controlnet/Test/2179_9871_432b1fbf16d04cd8371cd9ece543cb28.png")
|
| 37 |
+
|
| 38 |
+
# pipe = pipe.to(device)
|
| 39 |
+
# generator = torch.manual_seed(0)
|
| 40 |
+
# generator = torch.Generator(device=device).manual_seed(999)
|
| 41 |
+
# generator = None
|
| 42 |
+
|
| 43 |
+
# images = []
|
| 44 |
+
# for i in range(5):
|
| 45 |
+
# image = pipe(
|
| 46 |
+
# "floor plan,2 bedrooms", num_inference_steps=100, image=control_image
|
| 47 |
+
# ).images[0]
|
| 48 |
+
# images.append(image)
|
| 49 |
+
|
| 50 |
+
generator = torch.Generator(device=device).manual_seed(333)
|
| 51 |
+
images = []
|
| 52 |
+
for i in range(5):
|
| 53 |
+
image = pipe(
|
| 54 |
+
"floor plan,2 bedrooms", num_inference_steps=20, generator=generator, image=control_image
|
| 55 |
+
).images[0]
|
| 56 |
+
images.append(image)
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def make_grid(images, size=512):
|
| 60 |
+
"""Given a list of PIL images, stack them together into a line for easy viewing"""
|
| 61 |
+
output_im = Image.new("RGB", (size * len(images), size))
|
| 62 |
+
for i, im in enumerate(images):
|
| 63 |
+
output_im.paste(im.resize((size, size)), (i * size, 0))
|
| 64 |
+
return output_im
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
make_grid(images, size=512)
|