-
Notifications
You must be signed in to change notification settings - Fork 57
/
generate_multiscale_samples.py
189 lines (158 loc) · 5.61 KB
/
generate_multiscale_samples.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
188
189
""" Demo script for running Random-latent space interpolation on the trained MSG-StyleGAN OR
Show the effect of stochastic noise on a fixed image (for all multiple scales)"""
import argparse
import os
import pickle
from math import sqrt
from pathlib import Path
import dnnlib.tflib as tflib
import imageio
import numpy as np
import torch
import torchvision as tv
from tqdm import tqdm
from training.misc import dumb_upsample_nn
def parse_arguments():
parser = argparse.ArgumentParser("MSG-StyleGAN Multiscale image_generator")
parser.add_argument(
"--pickle_file",
type=str,
required=True,
action="store",
help="pickle file containing the trained styleGAN model",
)
parser.add_argument(
"--output_path",
action="store",
type=str,
default=None,
required=True,
help="Path to directory for saving the files",
)
parser.add_argument(
"--random_state",
action="store",
type=int,
default=3,
help="random_state (seed) for the script to run",
)
parser.add_argument(
"--num_cols",
action="store",
type=int,
default=None,
help="number of rows in the required grid",
)
parser.add_argument(
"--num_samples",
action="store",
type=int,
default=100,
help="Number of samples to be generated",
)
parser.add_argument(
"--truncation_psi",
action="store",
type=float,
default=0.6,
help="value of truncation psi for image generation",
)
parser.add_argument(
"--alternate_reso",
action="store",
type=bool,
default=False,
help="Whether to visualize only alternate resolutions in the multi-scale generation",
)
parser.add_argument(
"--only_noise",
action="store",
type=bool,
default=False,
help="to visualize the same point with only different realizations of noise",
)
args = parser.parse_args()
return args
def get_image(gen, point, num_cols, trunc_psi=0.6, alternate_reso=False):
"""
obtain an All-resolution grid of images from the given point
:param gen: the generator object
:param point: random latent point for generation
:param num_cols: depth of network from where the images are to be generated
:param trunc_psi: value of the truncation psi used for generating the images
:param alternate_reso: whether to visualize only alternate resolutions in the multi-scale generation
:return: img => generated image
"""
fmt = dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True)
point = np.expand_dims(point, axis=0)
images = gen.run(
point,
None,
truncation_psi=trunc_psi,
randomize_noise=True,
output_transform=fmt,
)
highest_res_log_2 = int(np.log2(images[-1].shape[1]))
images = [
dumb_upsample_nn(
np.transpose(image, (0, 3, 1, 2)),
int(2 ** (highest_res_log_2 - int(np.log2(image.shape[1])))),
)
for image in images
]
if alternate_reso:
new_images = []
for num, image in enumerate(reversed(images)):
if num % 2 == 0:
new_images.append(image)
images = list(reversed(new_images))
if num_cols is None:
num_cols = len(images) # default behaviour is to create a horizontal sequence
if num_cols == 1:
# for a vertical tower, reverse the order of the images
images = list(reversed(images))
images = np.concatenate(images, 0) # concatenate the images
multi_scale_image_grid = tv.utils.make_grid(
torch.tensor(images), nrow=num_cols
).numpy()
multi_scale_image_grid = np.transpose(multi_scale_image_grid, (1, 2, 0))
return multi_scale_image_grid
def main(args):
# Initialize TensorFlow.
tflib.init_tf()
# Load pre-trained network.
with open(args.pickle_file, "rb") as f:
_, _, Gs = pickle.load(f)
# _ = Instantaneous snapshot of the generator. Mainly useful for resuming a previous training run.
# _ = Instantaneous snapshot of the discriminator. Mainly useful for resuming a previous training run.
# Gs = Long-term average of the generator. Yields higher-quality results than the instantaneous snapshot.
# Print network details.
print("\n\nLoaded the Generator as:")
Gs.print_layers()
# Pick latent vector.
latent_size = Gs.input_shape[1]
rnd = np.random.RandomState(args.random_state)
# create the random latent_points for the interpolation
total_samples = args.num_samples
all_latents = rnd.randn(total_samples, latent_size)
all_latents = (
all_latents / np.linalg.norm(all_latents, axis=-1, keepdims=True)
) * sqrt(latent_size)
# animation mechanism
start_point = np.expand_dims(all_latents[0], axis=0)
points = all_latents[1:]
# all points are start_point, if we have only noise realization
if args.only_noise:
points = np.array([np.squeeze(start_point) for _ in points])
# make sure that the output path exists
output_path = Path(args.output_path)
output_path.mkdir(exist_ok=True)
print("Generating the requested number of samples ... ")
for count, point in tqdm(enumerate(points, 1)):
image = get_image(
Gs, point, args.num_cols, args.truncation_psi, args.alternate_reso
)
imageio.imwrite(os.path.join(output_path, str(count) + ".png"), image)
print(f"Requested images have been generated at: {output_path}")
if __name__ == "__main__":
main(parse_arguments())