forked from rosinality/stylegan2-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.py
executable file
·157 lines (135 loc) · 4.89 KB
/
generate.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
import argparse
import torch
from torchvision import utils
from tqdm import tqdm
def generate(args, g_ema, device, mean_latent):
with torch.no_grad():
g_ema.eval()
for i in tqdm(range(args.pics)):
sample_z = torch.randn(args.sample, args.latent, device=device)
if args.conditional:
if args.label == -1:
for label in range(0,7):
labels = torch.tensor(label).repeat(args.sample)
labels = torch.nn.functional.one_hot(labels, num_classes=args.num_classes).float().to(device)
sample, _ = g_ema(
[sample_z], labels, truncation=args.truncation, truncation_latent=mean_latent
)
utils.save_image(
sample,
f"generated_samples/conditional/{args.file_name}-{str(label)}_{str(i).zfill(6)}.png",
nrow=1,
normalize=True,
range=(-1, 1),
)
else:
labels = torch.tensor(args.label).repeat(args.sample)
labels = torch.nn.functional.one_hot(labels, num_classes=args.num_classes).float().to(device)
sample, _ = g_ema(
[sample_z], labels, truncation=args.truncation, truncation_latent=mean_latent
)
utils.save_image(
sample,
f"generated_samples/conditional/{args.file_name}-{str(args.label)}_{str(i).zfill(6)}.png",
nrow=1,
normalize=True,
range=(-1, 1),
)
else:
sample, _ = g_ema(
[sample_z], truncation=args.truncation, truncation_latent=mean_latent
)
utils.save_image(
sample,
f"generated_samples/{args.file_name}-{str(i).zfill(6)}.png",
nrow=1,
normalize=True,
range=(-1, 1),
)
if __name__ == "__main__":
device = "cuda"
parser = argparse.ArgumentParser(description="Generate samples from the generator")
parser.add_argument(
"--size", type=int, default=256, help="output image size of the generator"
)
parser.add_argument(
"--sample",
type=int,
default=1,
help="number of samples to be generated for each image",
)
parser.add_argument(
"--pics", type=int, default=20, help="number of images to be generated"
)
parser.add_argument("--truncation", type=float, default=1, help="truncation ratio")
parser.add_argument(
"--truncation_mean",
type=int,
default=4096,
help="number of vectors to calculate mean for the truncation",
)
parser.add_argument(
"--ckpt",
type=str,
default="stylegan2-ffhq-config-f.pt",
help="path to the model checkpoint",
)
parser.add_argument(
"--channel_multiplier",
type=int,
default=2,
help="channel multiplier of the generator. config-f = 2, else = 1",
)
parser.add_argument(
"--file_name",
type=str,
default="stylegan2-001",
help="file name for the generated images <name>-<num_image>.png",
)
parser.add_argument(
'--arch',
type=str,
default='stylegan2',
help='model architectures (stylegan2 | swagan)')
parser.add_argument(
"--conditional",
action="store_true",
help="conditional generation",
)
parser.add_argument(
"--num_classes",
type=int,
default=7,
)
parser.add_argument(
"--label",
type=int,
default=-1,
help='Class to generate. -1 if generate all labels.',
)
args = parser.parse_args()
args.latent = 512
args.n_mlp = 8
if args.arch == 'stylegan2':
if args.conditional:
from model_conditional import Generator
else:
from model import Generator
elif args.arch == 'swagan':
from swagan import Generator
if args.conditional:
g_ema = Generator(
args.size, args.latent, args.n_mlp, num_classes=args.num_classes, channel_multiplier=args.channel_multiplier
).to(device)
else:
g_ema = Generator(
args.size, args.latent, args.n_mlp, channel_multiplier=args.channel_multiplier
).to(device)
checkpoint = torch.load(args.ckpt)
g_ema.load_state_dict(checkpoint["g_ema"])
if args.truncation < 1:
with torch.no_grad():
mean_latent = g_ema.mean_latent(args.truncation_mean)
else:
mean_latent = None
generate(args, g_ema, device, mean_latent)