-
Notifications
You must be signed in to change notification settings - Fork 59
/
predict.py
128 lines (116 loc) · 3.64 KB
/
predict.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
import argparse
import sys
import tempfile
from pathlib import Path
import cog
sys.path.insert(0, "base")
from utils.inverter import StyleGANInverter
from utils.visualizer import load_image, resize_image, save_image
class Predictor(cog.Predictor):
def setup(self):
# get default args first
self.args = parse_args()
@cog.input("image", type=Path, help="facial image for manipulation")
@cog.input(
"description",
type=str,
help="description of how to manipulate the image, e.g. 'he is old', 'she is smiling'",
)
def predict(self, image, description):
self.args.description = description
self.args.image_path = image
inverter = StyleGANInverter(
self.args.model_name,
mode=self.args.mode,
learning_rate=self.args.learning_rate,
iteration=self.args.num_iterations,
reconstruction_loss_weight=1.0,
perceptual_loss_weight=self.args.loss_weight_feat,
regularization_loss_weight=self.args.loss_weight_enc,
clip_loss_weight=self.args.loss_weight_clip,
description=self.args.description,
logger=None,
)
image_size = inverter.G.resolution
# Invert the given image.
image = resize_image(
load_image(str(self.args.image_path)), (image_size, image_size)
)
_, viz_results = inverter.easy_invert(image, num_viz=self.args.num_results)
out_path = Path(tempfile.mkdtemp()) / "out.png"
save_image(str(out_path), viz_results[-1])
return out_path
def parse_args():
"""Parses arguments."""
parser = argparse.ArgumentParser()
parser.add_argument(
"--model_name",
type=str,
default="styleganinv_ffhq256",
help="Name of the GAN model.",
)
parser.add_argument(
"--mode",
type=str,
default="man",
help="Mode (gen for generation, man for manipulation).",
)
parser.add_argument(
"--description", type=str, default="he is old", help="The description."
)
parser.add_argument(
"--image_path",
type=str,
default="examples/142.jpg",
help="Path of images to invert.",
)
parser.add_argument(
"-o",
"--output_dir",
type=str,
default="",
help="Directory to save the results. If not specified, "
"`./results/inversion/test` "
"will be used by default.",
)
parser.add_argument(
"--learning_rate",
type=float,
default=0.01,
help="Learning rate for optimization. (default: 0.01)",
)
parser.add_argument(
"--num_iterations",
type=int,
default=200,
help="Number of optimization iterations. (default: 200)",
)
parser.add_argument(
"--num_results",
type=int,
default=5,
help="Number of intermediate optimization results to "
"save for each sample. (default: 5)",
)
parser.add_argument(
"--loss_weight_feat",
type=float,
default=5e-5,
help="The perceptual loss scale for optimization. " "(default: 5e-5)",
)
parser.add_argument(
"--loss_weight_enc",
type=float,
default=2.0,
help="The encoder loss scale for optimization." "(default: 2.0)",
)
parser.add_argument(
"--loss_weight_clip",
type=float,
default=2.0,
help="The clip loss for optimization. (default: 2.0)",
)
parser.add_argument(
"--gpu_id", type=str, default="0", help="Which GPU(s) to use. (default: `0`)"
)
return parser.parse_args("")