Update README.md
Browse files
README.md
CHANGED
@@ -30,36 +30,41 @@ Unlike the inpaint controlnets used for general scenarios, this model is fine-tu
|
|
30 |
```python
|
31 |
from diffusers import (
|
32 |
ControlNetModel,
|
33 |
-
|
|
|
34 |
)
|
35 |
from diffusers.utils import load_image
|
36 |
import torch
|
37 |
from PIL import Image
|
|
|
38 |
|
39 |
def make_inpaint_condition(init_image, mask_image):
|
40 |
init_image = np.array(init_image.convert("RGB")).astype(np.float32) / 255.0
|
41 |
mask_image = np.array(mask_image.convert("L")).astype(np.float32) / 255.0
|
42 |
-
|
43 |
assert init_image.shape[0:1] == mask_image.shape[0:1], "image and image_mask must have the same image size"
|
44 |
init_image[mask_image > 0.5] = -1.0 # set as masked pixel
|
45 |
init_image = np.expand_dims(init_image, 0).transpose(0, 3, 1, 2)
|
46 |
init_image = torch.from_numpy(init_image)
|
47 |
return init_image
|
48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
controlnet = ControlNetModel.from_pretrained(
|
51 |
-
"alimama-creative/EcomXL_controlnet_inpaint",
|
|
|
52 |
)
|
53 |
|
54 |
-
pipe =
|
55 |
"stabilityai/stable-diffusion-xl-base-1.0",
|
56 |
controlnet=controlnet,
|
57 |
-
torch_dtype=torch.float16
|
58 |
)
|
59 |
-
|
60 |
-
pipe.scheduler =
|
61 |
-
# pipe.enable_xformers_memory_efficient_attention()
|
62 |
-
pipe.enable_vae_slicing()
|
63 |
|
64 |
image = load_image(
|
65 |
"https://huggingface.co/alimama-creative/EcomXL_controlnet_inpaint/resolve/main/images/inp_0.png"
|
@@ -69,24 +74,25 @@ mask = load_image(
|
|
69 |
)
|
70 |
mask = Image.fromarray(255 - np.array(mask))
|
71 |
|
72 |
-
control_image = make_inpaint_condition(
|
73 |
|
74 |
prompt="a product on the table"
|
75 |
|
76 |
-
|
|
|
|
|
77 |
prompt,
|
78 |
-
image=
|
79 |
-
|
80 |
-
control_image=control_image,
|
81 |
-
controlnet_conditioning_scale=0.5,
|
82 |
guidance_scale=7,
|
83 |
-
strength=0.75,
|
84 |
width=1024,
|
85 |
height=1024,
|
|
|
|
|
86 |
).images[0]
|
87 |
|
88 |
-
image
|
89 |
-
|
90 |
```
|
91 |
The model exhibits good performance when the controlnet weight (controllet_condition_scale) is 0.5.
|
92 |
|
|
|
30 |
```python
|
31 |
from diffusers import (
|
32 |
ControlNetModel,
|
33 |
+
StableDiffusionXLControlNetPipeline,
|
34 |
+
DDPMScheduler
|
35 |
)
|
36 |
from diffusers.utils import load_image
|
37 |
import torch
|
38 |
from PIL import Image
|
39 |
+
import numpy as np
|
40 |
|
41 |
def make_inpaint_condition(init_image, mask_image):
|
42 |
init_image = np.array(init_image.convert("RGB")).astype(np.float32) / 255.0
|
43 |
mask_image = np.array(mask_image.convert("L")).astype(np.float32) / 255.0
|
|
|
44 |
assert init_image.shape[0:1] == mask_image.shape[0:1], "image and image_mask must have the same image size"
|
45 |
init_image[mask_image > 0.5] = -1.0 # set as masked pixel
|
46 |
init_image = np.expand_dims(init_image, 0).transpose(0, 3, 1, 2)
|
47 |
init_image = torch.from_numpy(init_image)
|
48 |
return init_image
|
49 |
|
50 |
+
def add_fg(full_img, fg_img, mask_img):
|
51 |
+
full_img = np.array(full_img).astype(np.float32)
|
52 |
+
fg_img = np.array(fg_img).astype(np.float32)
|
53 |
+
mask_img = np.array(mask_img).astype(np.float32) / 255.
|
54 |
+
full_img = full_img * mask_img + fg_img * (1-mask_img)
|
55 |
+
return Image.fromarray(np.clip(full_img, 0, 255).astype(np.uint8))
|
56 |
|
57 |
controlnet = ControlNetModel.from_pretrained(
|
58 |
+
"alimama-creative/EcomXL_controlnet_inpaint",
|
59 |
+
use_safetensors=True,
|
60 |
)
|
61 |
|
62 |
+
pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
|
63 |
"stabilityai/stable-diffusion-xl-base-1.0",
|
64 |
controlnet=controlnet,
|
|
|
65 |
)
|
66 |
+
pipe.to("cuda")
|
67 |
+
pipe.scheduler = DDPMScheduler.from_config(pipe.scheduler.config)
|
|
|
|
|
68 |
|
69 |
image = load_image(
|
70 |
"https://huggingface.co/alimama-creative/EcomXL_controlnet_inpaint/resolve/main/images/inp_0.png"
|
|
|
74 |
)
|
75 |
mask = Image.fromarray(255 - np.array(mask))
|
76 |
|
77 |
+
control_image = make_inpaint_condition(image, mask)
|
78 |
|
79 |
prompt="a product on the table"
|
80 |
|
81 |
+
generator = torch.Generator(device="cuda").manual_seed(1234)
|
82 |
+
|
83 |
+
res_image = pipe(
|
84 |
prompt,
|
85 |
+
image=control_image,
|
86 |
+
num_inference_steps=25,
|
|
|
|
|
87 |
guidance_scale=7,
|
|
|
88 |
width=1024,
|
89 |
height=1024,
|
90 |
+
controlnet_conditioning_scale=0.5,
|
91 |
+
generator=generator,
|
92 |
).images[0]
|
93 |
|
94 |
+
res_image = add_fg(res_image, image, mask)
|
95 |
+
res_image.save(f'res.png')
|
96 |
```
|
97 |
The model exhibits good performance when the controlnet weight (controllet_condition_scale) is 0.5.
|
98 |
|