Views
No views yet















1from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, AutoencoderKL
2from diffusers import DDIMScheduler, EulerAncestralDiscreteScheduler
3from PIL import Image
4from guided_filter import FastGuidedFilter # I have upload this file in this repo
5import torch
6import numpy as np
7import cv2
8
9def resize_image_control(control_image, resolution):
10 HH, WW, _ = control_image.shape
11 crop_h = random.randint(0, HH - resolution[1])
12 crop_w = random.randint(0, WW - resolution[0])
13 crop_image = control_image[crop_h:crop_h+resolution[1], crop_w:crop_w+resolution[0], :]
14 return crop_image, crop_w, crop_h
15
16def apply_gaussian_blur(image_np, ksize=5, sigmaX=1.0):
17 if ksize % 2 == 0:
18 ksize += 1 # ksize must be odd
19 blurred_image = cv2.GaussianBlur(image_np, (ksize, ksize), sigmaX=sigmaX)
20 return blurred_image
21
22def apply_guided_filter(image_np, radius, eps, scale):
23 filter = FastGuidedFilter(image_np, radius, eps, scale)
24 return filter.filter(image_np)
25
26
27controlnet_conditioning_scale = 1.0
28prompt = "your prompt, the longer the better, you can describe it as detail as possible"
29negative_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality'
30
31eulera_scheduler = EulerAncestralDiscreteScheduler.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", subfolder="scheduler")
32
33
34controlnet = ControlNetModel.from_pretrained(
35 "xinsir/controlnet-tile-sdxl-1.0",
36 torch_dtype=torch.float16
37)
38
39# when test with other base model, you need to change the vae also.
40vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
41
42pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
43 "stabilityai/stable-diffusion-xl-base-1.0",
44 controlnet=controlnet,
45 vae=vae,
46 safety_checker=None,
47 torch_dtype=torch.float16,
48 scheduler=eulera_scheduler,
49)
50
51controlnet_img = cv2.imread("your original image path")
52height, width, _ = controlnet_img.shape
53ratio = np.sqrt(1024. * 1024. / (width * height))
54W, H = int(width * ratio), int(height * ratio)
55
56crop_w, crop_h = 0, 0
57controlnet_img = cv2.resize(controlnet_img, (W, H))
58
59
60blur_strength = random.sample([i / 10. for i in range(10, 201, 2)], k=1)[0]
61radius = random.sample([i for i in range(1, 40, 2)], k=1)[0]
62eps = random.sample([i / 1000. for i in range(1, 101, 2)], k=1)[0]
63scale_factor = random.sample([i / 10. for i in range(10, 181, 5)], k=1)[0]
64
65
66if random.random() > 0.5:
67 controlnet_img = apply_gaussian_blur(controlnet_img, ksize=int(blur_strength), sigmaX=blur_strength / 2)
68
69if random.random() > 0.5:
70 # Apply Guided Filter
71 controlnet_img = apply_guided_filter(controlnet_img, radius, eps, scale_factor)
72
73# Resize image
74controlnet_img = cv2.resize(controlnet_img, (int(W / scale_factor), int(H / scale_factor)), interpolation=cv2.INTER_AREA)
75controlnet_img = cv2.resize(controlnet_img, (W, H), interpolation=cv2.INTER_CUBIC)
76
77controlnet_img = cv2.cvtColor(controlnet_img, cv2.COLOR_BGR2RGB)
78controlnet_img = Image.fromarray(controlnet_img)
79
80# need to resize the image resolution to 1024 * 1024 or same bucket resolution to get the best performance
81
82images = pipe(
83 prompt,
84 negative_prompt=negative_prompt,
85 image=controlnet_img,
86 controlnet_conditioning_scale=controlnet_conditioning_scale,
87 width=new_width,
88 height=new_height,
89 num_inference_steps=30,
90 ).images
91
92images[0].save(f"your image save path, png format is usually better than jpg or webp in terms of image quality but got much bigger")
931from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, AutoencoderKL
2from diffusers import DDIMScheduler, EulerAncestralDiscreteScheduler
3from PIL import Image
4import torch
5import numpy as np
6import cv2
7
8controlnet_conditioning_scale = 1.0
9prompt = "your prompt, the longer the better, you can describe it as detail as possible"
10negative_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality'
11
12eulera_scheduler = EulerAncestralDiscreteScheduler.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", subfolder="scheduler")
13
14
15controlnet = ControlNetModel.from_pretrained(
16 "xinsir/controlnet-tile-sdxl-1.0",
17 torch_dtype=torch.float16
18)
19
20# when test with other base model, you need to change the vae also.
21vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
22
23pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
24 "stabilityai/stable-diffusion-xl-base-1.0",
25 controlnet=controlnet,
26 vae=vae,
27 safety_checker=None,
28 torch_dtype=torch.float16,
29 scheduler=eulera_scheduler,
30)
31
32controlnet_img = cv2.imread("your original image path")
33height, width, _ = controlnet_img.shape
34ratio = np.sqrt(1024. * 1024. / (width * height))
35W, H = int(width * ratio), int(height * ratio)
36
37crop_w, crop_h = 0, 0
38controlnet_img = cv2.resize(controlnet_img, (W, H))
39controlnet_img = cv2.cvtColor(controlnet_img, cv2.COLOR_BGR2RGB)
40controlnet_img = Image.fromarray(controlnet_img)
41
42# need to resize the image resolution to 1024 * 1024 or same bucket resolution to get the best performance
43images = pipe(
44 prompt,
45 negative_prompt=negative_prompt,
46 image=controlnet_img,
47 controlnet_conditioning_scale=controlnet_conditioning_scale,
48 width=new_width,
49 height=new_height,
50 num_inference_steps=30,
51 ).images
52
53images[0].save(f"your image save path, png format is usually better than jpg or webp in terms of image quality but got much bigger")
541from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, AutoencoderKL
2from diffusers import DDIMScheduler, EulerAncestralDiscreteScheduler
3from PIL import Image
4import torch
5import numpy as np
6import cv2
7
8controlnet_conditioning_scale = 1.0
9prompt = "your prompt, the longer the better, you can describe it as detail as possible"
10negative_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality'
11
12eulera_scheduler = EulerAncestralDiscreteScheduler.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", subfolder="scheduler")
13
14
15controlnet = ControlNetModel.from_pretrained(
16 "xinsir/controlnet-tile-sdxl-1.0",
17 torch_dtype=torch.float16
18)
19
20# when test with other base model, you need to change the vae also.
21vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
22
23pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
24 "stabilityai/stable-diffusion-xl-base-1.0",
25 controlnet=controlnet,
26 vae=vae,
27 safety_checker=None,
28 torch_dtype=torch.float16,
29 scheduler=eulera_scheduler,
30)
31
32controlnet_img = cv2.imread("your original image path")
33height, width, _ = controlnet_img.shape
34ratio = np.sqrt(1024. * 1024. / (width * height))
35W, H = int(width * ratio) // 48 * 48, int(height * ratio) // 48 * 48
36controlnet_img = cv2.resize(controlnet_img, (W, H))
37controlnet_img = cv2.cvtColor(controlnet_img, cv2.COLOR_BGR2RGB)
38controlnet_img = Image.fromarray(controlnet_img)
39
40# need to resize the image resolution to 1024 * 1024 or same bucket resolution to get the best performance
41target_width = W // 3
42target_height = H // 3
43
44for i in range(3): # 两行
45 for j in range(3): # 两列
46 left = j * target_width
47 top = i * target_height
48 right = left + target_width
49 bottom = top + target_height
50
51 # 根据计算的边界裁剪图像
52 cropped_image = controlnet_img.crop((left, top, right, bottom))
53 cropped_image = cropped_image.resize((W, H))
54
55 images.append(cropped_image)
56
57seed = random.randint(0, 2147483647)
58generator = torch.Generator('cuda').manual_seed(seed)
59
60result_images = []
61for sub_img in images:
62 new_width, new_height = W, H
63 out = pipe(prompt=[prompt]*1,
64 image=sub_img,
65 control_image=sub_img,
66 negative_prompt=[negative_prompt]*1,
67 generator=generator,
68 width=new_width,
69 height=new_height,
70 num_inference_steps=30,
71 crops_coords_top_left=(W, H),
72 target_size=(W, H),
73 original_size=(W * 2, H * 2),
74 )
75 result_images.append(out.images[0])
76
77new_im = Image.new('RGB', (new_width*3, new_height*3))
78# 拼接图片到新的图像上
79new_im.paste(result_images[0], (0, 0))
80new_im.paste(result_images[1], (new_width, 0))
81new_im.paste(result_images[2], (new_width * 2, 0))
82new_im.paste(result_images[3], (0, new_height))
83new_im.paste(result_images[4], (new_width, new_height))
84new_im.paste(result_images[5], (new_width * 2, new_height))
85new_im.paste(result_images[6], (0, new_height * 2))
86new_im.paste(result_images[7], (new_width, new_height * 2))
87new_im.paste(result_images[8], (new_width * 2, new_height * 2))
88
89new_im.save(f"your image save path, png format is usually better than jpg or webp in terms of image quality but got much bigger")
90