-
Notifications
You must be signed in to change notification settings - Fork 0
/
synthesize.py
148 lines (120 loc) · 3.87 KB
/
synthesize.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
import os
import random
import sys
from argparse import ArgumentParser
from pathlib import Path
from typing import List, Union
import numpy as np
import torch
import yaml
from PIL.Image import Image
from matplotlib import pyplot as plt
from configs.settings import CONFIGS, MODELS
def cli_main():
"""
Command-line interface for initializing and parsing arguments related to model configuration, text input,
and style settings.
Returns:
Namespace: A namespace object containing parsed arguments.
"""
parser = ArgumentParser()
parser.add_argument(
"-c",
"--config",
help="Type of model",
choices=["RNN", "Diffusion", "LatentDiffusion"],
type=str,
required=True,
)
parser.add_argument(
"-cf",
"--config-file",
help="Filename for configs",
type=str,
default="base_gpu.yaml",
)
parser.add_argument(
"-t",
"--text",
help="Text to generate",
type=str,
default="Handwriting Synthesis in Python",
)
parser.add_argument(
"-w",
"--writer",
help="Writer style. If not provided, the default writer is selected randomly",
type=int,
default=random.randint(0, 329),
)
parser.add_argument(
"--color",
help="Handwriting color. If not provided, the default color is black",
type=str,
default="black",
)
parser.add_argument(
"-s",
"--style_path",
help="Filename for style. If not provided, the default style is selected randomly",
type=str,
default=f"{os.listdir('assets')[np.random.randint(0, len(os.listdir('assets')))]}",
)
return parser.parse_args()
def generate_handwriting() -> Union[Image, plt.Figure, List[Image], List[plt.Figure]]:
"""
Generates handwriting based on the specified model and input parameters.
Depending on the model type (LatentDiffusion, RNN, or Diffusion), the function
loads the appropriate model checkpoint, configures it, and generates handwriting
images based on the provided text, style, and color.
Returns:
Union[Image, plt.Figure, List[Image], List[plt.Figure]]: Generated handwriting images or figures.
"""
model = MODELS[args.config].load_from_checkpoint(
checkpoint_path=f"{config.checkpoint_path}/model.ckpt",
map_location=torch.device(config.device),
)
model.eval()
# seed_everything(seed=42)
if args.config == "LatentDiffusion":
print(f"Selected writer id: {args.writer}")
save_path = Path(
f"{os.getcwd()}/images/{args.config}/{args.text.replace(' ', '-')}-w{args.writer}.jpeg"
)
return model.generate(
args.text,
vocab=config.vocab,
max_text_len=config.max_text_len,
writer_id=args.writer,
save_path=save_path,
color=args.color,
)
save_path = Path(
f"{os.getcwd()}/images/{args.config}/{args.text.replace(' ', '-')}.jpeg"
)
if args.config == "Diffusion":
return model.generate(
args.text,
save_path=save_path,
max_text_len=config.max_text_len,
vocab=config.vocab,
color=args.color,
style_path=args.style_path,
)
else:
return model.generate(
args.text,
save_path=save_path,
max_text_len=config.max_text_len,
vocab=config.vocab,
color=args.color,
)
if __name__ == "__main__":
args = cli_main()
if sys.version_info < (3, 8):
raise SystemExit("Only Python 3.8 and above is supported")
config_file = f"configs/{args.config}/{args.config_file}"
config = CONFIGS[args.config].from_yaml_file(
file=config_file, decoder=yaml.load, Loader=yaml.Loader
)
generate_handwriting()