xinsir commited on
Commit
5837e9b
·
verified ·
1 Parent(s): 621f5b5

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +122 -1
README.md CHANGED
@@ -57,4 +57,125 @@ prompt: fruit, food, no humans, food focus, cherry, simple background, english t
57
  ![image6](./000067_scribble_concat.webp)
58
 
59
  prompt: 1girl, solo, ball, swimsuit, bikini, mole, beachball, white bikini, breasts, hairclip, navel, looking at viewer, hair ornament, chromatic aberration, holding, holding ball, pool, cleavage, water, collarbone, mole on breast, blush, bangs, parted lips, bare shoulders, mole on thigh, bare arms, smile, large breasts, blonde hair, halterneck, hair between eyes, stomach
60
- ![image7](./000092_scribble_concat.webp)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  ![image6](./000067_scribble_concat.webp)
58
 
59
  prompt: 1girl, solo, ball, swimsuit, bikini, mole, beachball, white bikini, breasts, hairclip, navel, looking at viewer, hair ornament, chromatic aberration, holding, holding ball, pool, cleavage, water, collarbone, mole on breast, blush, bangs, parted lips, bare shoulders, mole on thigh, bare arms, smile, large breasts, blonde hair, halterneck, hair between eyes, stomach
60
+ ![image7](./000092_scribble_concat.webp)
61
+
62
+
63
+ ## How to Get Started with the Model
64
+
65
+ Use the code below to get started with the model.
66
+
67
+ ```python
68
+ from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, AutoencoderKL
69
+ from diffusers import DDIMScheduler, EulerAncestralDiscreteScheduler
70
+ from PIL import Image
71
+ import torch
72
+ import numpy as np
73
+ import cv2
74
+
75
+ def HWC3(x):
76
+ assert x.dtype == np.uint8
77
+ if x.ndim == 2:
78
+ x = x[:, :, None]
79
+ assert x.ndim == 3
80
+ H, W, C = x.shape
81
+ assert C == 1 or C == 3 or C == 4
82
+ if C == 3:
83
+ return x
84
+ if C == 1:
85
+ return np.concatenate([x, x, x], axis=2)
86
+ if C == 4:
87
+ color = x[:, :, 0:3].astype(np.float32)
88
+ alpha = x[:, :, 3:4].astype(np.float32) / 255.0
89
+ y = color * alpha + 255.0 * (1.0 - alpha)
90
+ y = y.clip(0, 255).astype(np.uint8)
91
+ return y
92
+
93
+ def nms(x, t, s):
94
+ x = cv2.GaussianBlur(x.astype(np.float32), (0, 0), s)
95
+
96
+ f1 = np.array([[0, 0, 0], [1, 1, 1], [0, 0, 0]], dtype=np.uint8)
97
+ f2 = np.array([[0, 1, 0], [0, 1, 0], [0, 1, 0]], dtype=np.uint8)
98
+ f3 = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype=np.uint8)
99
+ f4 = np.array([[0, 0, 1], [0, 1, 0], [1, 0, 0]], dtype=np.uint8)
100
+
101
+ y = np.zeros_like(x)
102
+
103
+ for f in [f1, f2, f3, f4]:
104
+ np.putmask(y, cv2.dilate(x, kernel=f) == x, x)
105
+
106
+ z = np.zeros_like(y, dtype=np.uint8)
107
+ z[y > t] = 255
108
+ return z
109
+
110
+
111
+ controlnet_conditioning_scale = 1.0
112
+ prompt = "your prompt, the longer the better, you can describe it as detail as possible"
113
+ negative_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality'
114
+
115
+
116
+ eulera_scheduler = EulerAncestralDiscreteScheduler.from_pretrained("gsdf/CounterfeitXL", subfolder="scheduler")
117
+
118
+
119
+ controlnet = ControlNetModel.from_pretrained(
120
+ "xinsir/anime-painter",
121
+ torch_dtype=torch.float16
122
+ )
123
+
124
+ # when test with other base model, you need to change the vae also.
125
+ vae = AutoencoderKL.from_pretrained("gsdf/CounterfeitXL", subfolder="vae", torch_dtype=torch.float16)
126
+
127
+ pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
128
+ "gsdf/CounterfeitXL",
129
+ controlnet=controlnet,
130
+ vae=vae,
131
+ safety_checker=None,
132
+ torch_dtype=torch.float16,
133
+ scheduler=eulera_scheduler,
134
+ )
135
+
136
+ # you can use either hed to generate a fake scribble given an image or a sketch image totally draw by yourself
137
+
138
+ if random.random() > 0.5:
139
+ # Method 1
140
+ # if you use hed, you should provide an image, the image can be real or anime, you extract its hed lines and use it as the scribbles
141
+ # The detail about hed detect you can refer to https://github.com/lllyasviel/ControlNet/blob/main/gradio_fake_scribble2image.py
142
+ # I provide a pseudo-code here
143
+ # img = cv2.imread(img_path)
144
+ # hed_img = apply_hed(img)
145
+ # cv2.imwrite("a hed detect path for an image", hed_img)
146
+
147
+ controlnet_img = Image.open("a hed detect path for an image")
148
+ controlnet_img = np.array(controlnet_img)
149
+ controlnet_img = nms(controlnet_img, 127, 3)
150
+ controlnet_img = cv2.GaussianBlur(controlnet_img, (0, 0), 3)
151
+
152
+ # different threshold for different lines
153
+ random_val = int(round(random.uniform(0.01, 0.10), 2) * 255)
154
+ controlnet_img[controlnet_img > random_val] = 255
155
+ controlnet_img[controlnet_img < 255] = 0
156
+ controlnet_img = Image.fromarray(controlnet_img)
157
+
158
+ else:
159
+ # Method 2
160
+ # if you use a sketch image total draw by yourself
161
+ control_path = "the sketch image you draw with some tools, like drawing board, the path you save it"
162
+ controlnet_img = Image.open(control_path) # Note that the image must be black-white(0 or 255), like the examples we list
163
+
164
+ # must resize to 1024*1024 or same resolution bucket to get the best performance
165
+ width, height = controlnet_img.size
166
+ ratio = np.sqrt(1024. * 1024. / (width * height))
167
+ new_width, new_height = int(width * ratio), int(height * ratio)
168
+ controlnet_img = controlnet_img.resize((new_width, new_height))
169
+
170
+ images = pipe(
171
+ prompt,
172
+ negative_prompt=negative_prompt,
173
+ image=controlnet_img,
174
+ controlnet_conditioning_scale=controlnet_conditioning_scale,
175
+ width=new_width,
176
+ height=new_height,
177
+ num_inference_steps=30,
178
+ ).images
179
+
180
+ images[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")
181
+ ```