forked from yeungchenwa/FontDiffuser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_one_by_one.py
249 lines (217 loc) · 10.4 KB
/
gen_one_by_one.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import os
import cv2
import time
import random
import numpy as np
from PIL import Image
import torch
import torchvision.transforms as transforms
from accelerate.utils import set_seed
from src import (FontDiffuserDPMPipeline,
FontDiffuserModelDPM,
build_ddpm_scheduler,
build_unet,
build_content_encoder,
build_style_encoder)
from utils import (ttf2im,
load_ttf,
is_char_in_font,
save_args_to_yaml,
)
def save_single_image(save_dir, image, name=None):
if name is None:
save_path = f"{save_dir}/out_single.png"
else:
save_path = f"{save_dir}/{name}.png"
image.save(save_path)
def save_image_with_content_style(save_dir, image, content_image_pil, content_image_path, style_image_path, resolution):
new_image = Image.new('RGB', (resolution * 3, resolution))
if content_image_pil is not None:
content_image = content_image_pil
else:
content_image = Image.open(content_image_path).convert("RGB").resize((resolution, resolution), Image.BILINEAR)
style_image = Image.open(style_image_path).convert("RGB").resize((resolution, resolution), Image.BILINEAR)
new_image.paste(content_image, (0, 0))
new_image.paste(style_image, (resolution, 0))
new_image.paste(image, (resolution * 2, 0))
save_path = f"{save_dir}/out_with_cs.jpg"
# new_image.save(save_path)
def arg_parse():
from configs.fontdiffuser import get_parser
parser = get_parser()
parser.add_argument("--ckpt_dir", type=str, default=None)
parser.add_argument("--demo", action="store_true")
parser.add_argument("--controlnet", type=bool, default=False, help="If in demo mode, the controlnet can be added.")
parser.add_argument("--character_input", action="store_true")
parser.add_argument("--content_character", type=str, default=None)
parser.add_argument("--content_image_path", type=str, default=None)
parser.add_argument("--content_image_dir", type=str, default=None, help="Directory containing content images.")
parser.add_argument("--style_image_path", type=str, default=None)
parser.add_argument("--save_image", action="store_true")
parser.add_argument("--save_image_dir", type=str, default=None, help="The saving directory.")
parser.add_argument("--device", type=str, default="cuda:0")
parser.add_argument("--ttf_path", type=str, default="ttf/KaiXinSongA.ttf")
args = parser.parse_args()
style_image_size = args.style_image_size
content_image_size = args.content_image_size
args.style_image_size = (style_image_size, style_image_size)
args.content_image_size = (content_image_size, content_image_size)
return args
def image_process(args, content_image=None, style_image=None):
if not args.demo:
# Read content image and style image
if args.character_input:
assert args.content_character is not None, "The content_character should not be None."
if not is_char_in_font(font_path=args.ttf_path, char=args.content_character):
return None, None
font = load_ttf(ttf_path=args.ttf_path)
content_image = ttf2im(font=font, char=args.content_character)
content_image_pil = content_image.copy()
else:
content_image = Image.open(args.content_image_path).convert('RGB')
content_image_pil = None
style_image = Image.open(args.style_image_path).convert('RGB')
else:
assert style_image is not None, "The style image should not be None."
if args.character_input:
assert args.content_character is not None, "The content_character should not be None."
if not is_char_in_font(font_path=args.ttf_path, char=args.content_character):
return None, None
font = load_ttf(ttf_path=args.ttf_path)
content_image = ttf2im(font=font, char=args.content_character)
else:
assert content_image is not None, "The content image should not be None."
content_image_pil = None
## Dataset transform
content_inference_transforms = transforms.Compose(
[transforms.Resize(args.content_image_size, interpolation=transforms.InterpolationMode.BILINEAR),
transforms.ToTensor(),
transforms.Normalize([0.5], [0.5])])
style_inference_transforms = transforms.Compose(
[transforms.Resize(args.style_image_size, interpolation=transforms.InterpolationMode.BILINEAR),
transforms.ToTensor(),
transforms.Normalize([0.5], [0.5])])
# [None, :] 的作用是对张量(tensor)进行扩展维度操作。从 [C, H, W] 变为 [1, C, H, W]
content_image = content_inference_transforms(content_image)[None, :]
style_image = style_inference_transforms(style_image)[None, :]
return content_image, style_image, content_image_pil
def load_fontdiffuer_pipeline(args):
# Load the model state_dict
unet = build_unet(args=args)
unet.load_state_dict(torch.load(f"{args.ckpt_dir}/unet.pth"))
style_encoder = build_style_encoder(args=args)
style_encoder.load_state_dict(torch.load(f"{args.ckpt_dir}/style_encoder.pth"))
content_encoder = build_content_encoder(args=args)
content_encoder.load_state_dict(torch.load(f"{args.ckpt_dir}/content_encoder.pth"))
model = FontDiffuserModelDPM(unet=unet, style_encoder=style_encoder, content_encoder=content_encoder)
model.to(args.device)
print("Loaded the model state_dict successfully!")
# Load the training ddpm_scheduler.
train_scheduler = build_ddpm_scheduler(args=args)
print("Loaded training DDPM scheduler sucessfully!")
# Load the DPM_Solver to generate the sample.
pipe = FontDiffuserDPMPipeline(
model=model,
ddpm_train_scheduler=train_scheduler,
model_type=args.model_type,
guidance_type=args.guidance_type,
guidance_scale=args.guidance_scale,
)
print("Loaded dpm_solver pipeline sucessfully!")
return pipe
def sampling(args, pipe, content_image=None, style_image=None):
if not args.demo:
os.makedirs(args.save_image_dir, exist_ok=True)
# saving sampling config
# save_args_to_yaml(args=args, output_file=f"{args.save_image_dir}/sampling_config.yaml")
if args.seed:
set_seed(seed=args.seed)
content_image, style_image, content_image_pil = image_process(args=args, content_image=content_image,
style_image=style_image)
if content_image == None:
print(
f"The content_character you provided is not in the ttf. Please change the content_character or you can change the ttf.")
return None
with torch.no_grad():
content_image = content_image.to(args.device)
style_image = style_image.to(args.device)
print(f"Sampling by DPM-Solver++ ......")
start = time.time()
images = pipe.generate(
content_images=content_image,
style_images=style_image,
batch_size=1,
order=args.order,
num_inference_step=args.num_inference_steps,
content_encoder_downsample_size=args.content_encoder_downsample_size,
t_start=args.t_start,
t_end=args.t_end,
dm_size=args.content_image_size,
algorithm_type=args.algorithm_type,
skip_type=args.skip_type,
method=args.method,
correcting_x0_fn=args.correcting_x0_fn)
end = time.time()
if args.save_image:
print(f"Saving the image ......")
# save_single_image(save_dir=args.save_image_dir, image=images[0])
if args.character_input:
save_image_with_content_style(save_dir=args.save_image_dir,
image=images[0],
content_image_pil=content_image_pil,
content_image_path=None,
style_image_path=args.style_image_path,
resolution=args.resolution)
else:
save_image_with_content_style(save_dir=args.save_image_dir,
image=images[0],
content_image_pil=None,
content_image_path=args.content_image_path,
style_image_path=args.style_image_path,
resolution=args.resolution)
print(f"Finish the sampling process, costing time {end - start}s")
return images[0]
def process_directory(args, pipe):
content_images = [f for f in os.listdir(args.content_image_dir) if f.endswith(('.png', '.jpg', '.jpeg'))]
for image_name in content_images:
args.content_image_path = os.path.join(args.content_image_dir, image_name)
print(f"Processing {args.content_image_path}...")
out_image = sampling(args=args, pipe=pipe)
if out_image is not None:
save_path = os.path.join(args.save_image_dir, image_name)
save_single_image(save_dir=args.save_image_dir, image=out_image,
name=image_name.replace(".png", "").replace(".jpg", ""))
print(f"Saved {save_path}")
if __name__ == "__main__":
"""
python gen_one_by_one.py \
--ckpt_dir="ckpt/" \
--content_image_dir="data_examples/basic/LXGWWenKaiGB-Light/" \
--style_image_path="data_examples/sampling/依.png" \
--save_image \
--save_image_dir="outputs/cpp" \
--device="cuda:0" \
--algorithm_type="dpmsolver++" \
--guidance_type="classifier-free" \
--guidance_scale=7.5 \
--num_inference_steps=20 \
--method="multistep"
python gen_one_by_one.py \
--ckpt_dir="ckpt/" \
--content_image_dir="data_examples/basic/LXGWWenKaiGB-Light/" \
--style_image_path="data_examples/sampling/crh.png" \
--save_image \
--save_image_dir="outputs/crh3" \
--device="cuda:0" \
--algorithm_type="dpmsolver++" \
--guidance_type="classifier-free" \
--guidance_scale=7.5 \
--num_inference_steps=20 \
--method="multistep"
"""
args = arg_parse()
pipe = load_fontdiffuer_pipeline(args=args)
if args.content_image_dir:
process_directory(args, pipe)
else:
out_image = sampling(args=args, pipe=pipe)