-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsd_inpainting.py
174 lines (149 loc) · 4.04 KB
/
sd_inpainting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import warnings
warnings.filterwarnings("ignore")
import argparse, os, re, random
import inspect
from typing import List, Optional, Union
from pathlib import Path
import numpy as np
import torch as th
import torchvision
import torchvision.transforms as T
from PIL import Image
import gradio as gr
from diffusers import StableDiffusionInpaintPipeline
from rembg.rembg import remove
from Tools.upscaler import Upscaler
upscaler = Upscaler()
device = th.device('cuda' if th.cuda.is_available() else 'cpu')
parser = argparse.ArgumentParser()
parser.add_argument(
"--prompt",
type=str,
nargs="?",
help="the text prompt to render",
required=True,
)
parser.add_argument(
"--image",
type=str,
nargs="?",
help="the original image to paint in",
required=True,
)
parser.add_argument(
"--mask_x",
type=int,
help="the x of the center point of the mask",
required=True,
)
parser.add_argument(
"--mask_y",
type=int,
help="the y of the center point of the mask",
required=True,
)
parser.add_argument(
"--mask_size",
type=int,
help="the size of the mask, in pixels",
required=True,
)
parser.add_argument(
"--output_dir",
type=str,
nargs="?",
help="directory of the output image (will be created if doesn't exist)",
default="./outputs"
)
parser.add_argument(
"--save_name",
type=str,
nargs="?",
help="name of the output image (with .png or .jpg)",
default="0.png"
)
parser.add_argument(
"--H",
type=int,
default=512,
help="image height, in pixels",
)
parser.add_argument(
"--W",
type=int,
default=512,
help="image width, in pixels",
)
parser.add_argument(
"--model",
type=str,
help="path to the folder of stable-diffusion-inpainting",
default="./stable-diffusion-inpainting",
)
parser.add_argument(
"--seed",
type=int,
default=None,
help="the seed (for reproducible sampling)",
)
parser.add_argument(
"--device",
type=str,
default="cuda",
help="specify GPU (cuda/cuda:0/cuda:1/...)",
)
parser.add_argument(
"--scale",
type=float,
default=7.5,
help="unconditional guidance scale",
)
def main():
opt = parser.parse_args()
os.makedirs(opt.output_dir, exist_ok=True)
if not os.path.isdir(opt.model):
print(f"Model not found : ({opt.model}) No such file or directory ")
return
if not os.path.isfile(opt.image):
print(f"Image not found : ({opt.image}) No such file or directory ")
return
if not opt.prompt:
print(f"Choose the prompt")
return
if not opt.seed:
opt.seed = random.randint(0, 1000000)
device = th.device(opt.device if th.cuda.is_available() else 'cpu')
pipe = StableDiffusionInpaintPipeline.from_pretrained(
opt.model,
revision="fp16",
torch_dtype=th.float16,
).to(device)
image = Image.open(opt.image).resize((opt.H, opt.W))
to_image = T.ToPILImage()
to_tensor = T.PILToTensor()
posX, posY = opt.mask_x, opt.mask_y
size = opt.mask_size
x1, x2 = int(posX-(size/2)), int(posX+(size/2))
y1, y2 = int(posY-(size/2)), int(posY+(size/2))
source_mask_512 = th.zeros(size=(opt.H, opt.W))
source_mask_512[y1:y2, x1:x2] = 1
source_mask_512 = source_mask_512 * to_tensor(image)*255
mask_image = to_image(source_mask_512)
guidance_scale=opt.scale
num_samples = 1
generator = th.Generator(device=device).manual_seed(opt.seed) # change the seed to get different results
images = pipe(
prompt=opt.prompt,
image=image,
mask_image=mask_image,
guidance_scale=guidance_scale,
generator=generator,
num_images_per_prompt=num_samples,
).images
generated_parts = [to_image(to_tensor(img)[:, y1:y2, x1:x2]) for img in images]
generated_part = generated_parts[-1]
upscaled_part = upscaler.upscale(generated_part)
extracted_part = remove(upscaled_part)
extracted_part.save(f"{opt.output_dir}/{opt.save_name}")
if __name__ == "__main__":
main()