-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombine.py
executable file
·66 lines (58 loc) · 2.45 KB
/
combine.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
#!/usr/bin/env python3
import cure
cure.patch()
from sys import exit
from numpy import array
from chainer.functions import stack
from stylegan.networks import Generator
from interface.args import CustomArgumentParser
from interface.argtypes import uint, real
from interface.stdout import chainer_like_tqdm
from utilities.iter import range_batch
from utilities.image import save_image
from utilities.stdio import eprint
from utilities.filesys import mkdirs, open_filepath_write
from utilities.numpy import load, save
from utilities.chainer import to_variable, config_valid
def main(args):
config_valid()
print("Loading a model...")
generator = Generator.load(args.generator)
generator.to_device(args.device)
ws = array([load(s) for s in args.style])
w = sum(w * k for w, k in zip(ws, args.coefs))
mkdirs(args.dest)
with open_filepath_write(args.dest, "new-style", "npy", args.force) as fp:
save(fp, w)
with chainer_like_tqdm(desc="generation", total=args.number) as bar:
for i, n in range_batch(args.number, args.batch):
y = generator.synthesizer([stack([to_variable(w, device=args.device)] * n)] * generator.levels, noise=args.noisy, fixed=args.fixed)
y.to_cpu()
for j in range(n):
filename = f"{i + j + 1}"
with open_filepath_write(args.dest, filename, "png", args.force) as fp:
save_image(y.array[j], fp)
bar.update()
def check_args(args):
if len(args.coefs) != len(args.style):
eprint("The number of coefficients doesn't match the number of styles!")
raise RuntimeError("Input error")
return args
def preprocess_args(args):
if args.coefs is None:
n = len(args.style)
args.coefs = [1 / n for i in range(n)]
return args
def parse_args():
parser = CustomArgumentParser("Create a new style vector and its images by linear combination of style vectors")
parser.require_generator().add_output_args("combined")
parser.add_argument("style", metavar="STYLE_FILE", nargs="+", help="input style NPY file")
parser.add_argument("-c", "--coefs", dest="coefs", metavar="K", nargs="+", type=real, help="linear combination coefficients to multiply style vectors respectively (average all styles when this option is not used)")
parser.add_argument("-n", "--number", metavar="N", type=uint, default=3, help="the number of images to generate")
return parser.add_evaluation_args(default_batch=1).parse_args()
if __name__ == "__main__":
try:
main(check_args(preprocess_args(parse_args())))
except KeyboardInterrupt:
eprint("KeyboardInterrupt")
exit(130)