forked from tmaham/DS-Fusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script_basic.py
188 lines (152 loc) · 4.85 KB
/
script_basic.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
#generate the stylized images
import os
import pdb
import random
import argparse
import sys
from ldm.data.list_fonts import font_list
def get_parser(**parser_kwargs):
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ("yes", "true", "t", "y", "1"):
return True
elif v.lower() in ("no", "false", "f", "n", "0"):
return False
else:
raise argparse.ArgumentTypeError("Boolean value expected.")
parser = argparse.ArgumentParser(**parser_kwargs)
parser.add_argument(
"-s",
"--style",
type=str,
const=True,
default="",
nargs="?",
required=True,
help="style word for generation, preferably nouns",
)
parser.add_argument(
"-t",
"--text",
type=str,
default="",
nargs="?",
required=True,
help="text to stylize",
)
parser.add_argument(
"--one_font",
type=str2bool,
default=True,
help="determines whether a single font or multiple fonts will be used",
)
parser.add_argument(
"--font_name",
type=str,
default="None",
help="explicitly give a font to stylize, only relevant for one font font-mode",
)
parser.add_argument(
"--custom_font",
type=str,
default="None",
help="user needs to ensure the font name is valid, and available on their system",
)
parser.add_argument(
"--attribute",
type=str,
default="None",
help="additional attribute for style images",
)
parser.add_argument(
"--white_bg",
type=str2bool,
default=True,
help="style images generated with 'white background'",
)
parser.add_argument(
"--cartoon",
type=bool,
default=True,
help="style images generated with cartoon attribute",
)
parser.add_argument(
"--data_folder",
type=str,
default="data_style/",
help="where to store style images",
)
parser.add_argument(
"--data_make",
type=str2bool,
default=False,
help="this will enforce style data to be re-generated, even if it already exists",
)
parser.add_argument(
"--gpus",
type=str,
default= "0,",
help="which gpus to use. write as 0,1, etc",
)
parser.add_argument(
"--ckpt_path",
type=str,
default= "ckpt/model.ckpt",
help="path to checkpoint of base",
)
return parser
if __name__ == "__main__":
sys.path.append(os.getcwd())
parser = get_parser()
opt, unknown = parser.parse_known_args()
path_data = os.path.join(opt.data_folder)
command = "mkdir "+ path_data
os.system(command)
prompt_cond = ""
if opt.cartoon:
prompt_cond = prompt_cond + "cartoon"
if opt.white_bg:
prompt_cond = prompt_cond + " with white background"
if opt.attribute != "None":
prompt = f"'{opt.style} illustration {prompt_cond} in style of {opt.attribute}'"
else:
prompt = f"'{opt.style} illustration {prompt_cond}'"
if opt.custom_font == "None":
if opt.font_name == "None":
font_name = random.choice(font_list)
else:
font_name = opt.font_name
else:
font_name = opt.custom_font
log_dir = opt.style +"-"+opt.text
data_exists = os.path.exists("data_style/"+opt.style)
if opt.data_make or not data_exists:
print("Making style images")
command = "rm -r " + "data_style/"+opt.style
os.system(command)
command = "mkdir -p " + "data_style/"+opt.style
os.system(command)
output_path = f"data_style/{opt.style}"
li = opt.ckpt_path
command = "python txt2img.py --ddim_eta 1.0 \
--n_samples 10 \
--n_iter 2\
--ddim_steps 50 \
--scale 5.0\
--H 256\
--W 256\
--outdir " + output_path + " --ckpt " +li +" --prompt " + prompt
os.system(command)
else:
print("Style images exist")
command = "mkdir out_cur"
os.system(command)
config_name = "finetune"
command = "python main.py --base configs/"+config_name+".yaml \
-t -n test --gpus 0, --ckpt_resume " + opt.ckpt_path +"\
--logname " +log_dir+ " --letter "\
+ opt.text + " --style_word " + opt.style + " --font_name " +font_name +\
(" --one_font True" if opt.one_font else " ") +\
" --images " +path_data +" --extra_style_word "+opt.attribute + " --custom_font " + opt.custom_font
os.system(command)