-
Notifications
You must be signed in to change notification settings - Fork 3
/
gen_samples.py
228 lines (166 loc) · 7.52 KB
/
gen_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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# SPDX-FileCopyrightText: Copyright (c) 2021-2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: LicenseRef-NvidiaProprietary
#
# NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
# property and proprietary rights in and to this material, related
# documentation and any modifications thereto. Any use, reproduction,
# disclosure or distribution of this material and related documentation
# without an express license agreement from NVIDIA CORPORATION or
# its affiliates is strictly prohibited.
"""Generate images and shapes using pretrained network pickle."""
import os
import re
from typing import List, Optional, Tuple, Union
import click
import dnnlib
import numpy as np
import PIL.Image
import torch
from tqdm import tqdm
import legacy
from torch_utils import misc
from training.networks import Generator
#----------------------------------------------------------------------------
def num_range(s: str) -> List[int]:
'''Accept either a comma separated list of numbers 'a,b,c' or a range 'a-c' and return as a list of ints.'''
range_re = re.compile(r'^(\d+)-(\d+)$')
m = range_re.match(s)
if m:
return list(range(int(m.group(1)), int(m.group(2))+1))
vals = s.split(',')
return [int(x) for x in vals]
#----------------------------------------------------------------------------
def parse_vec2(s: Union[str, Tuple[float, float]]) -> Tuple[float, float]:
'''Parse a floating point 2-vector of syntax 'a,b'.
Example:
'0,1' returns (0,1)
'''
if isinstance(s, tuple): return s
parts = s.split(',')
if len(parts) == 2:
return (float(parts[0]), float(parts[1]))
raise ValueError(f'cannot parse 2-vector {s}')
#----------------------------------------------------------------------------
def make_transform(translate: Tuple[float,float], angle: float):
m = np.eye(3)
s = np.sin(angle/360.0*np.pi*2)
c = np.cos(angle/360.0*np.pi*2)
m[0][0] = c
m[0][1] = s
m[0][2] = translate[0]
m[1][0] = -s
m[1][1] = c
m[1][2] = translate[1]
return m
#----------------------------------------------------------------------------
def create_samples(N=256, voxel_origin=[0, 0, 0], cube_length=2.0):
# NOTE: the voxel_origin is actually the (bottom, left, down) corner, not the middle
voxel_origin = np.array(voxel_origin) - cube_length/2
voxel_size = cube_length / (N - 1)
overall_index = torch.arange(0, N ** 3, 1, out=torch.LongTensor())
samples = torch.zeros(N ** 3, 3)
# transform first 3 columns
# to be the x, y, z index
samples[:, 2] = overall_index % N
samples[:, 1] = (overall_index.float() / N) % N
samples[:, 0] = ((overall_index.float() / N) / N) % N
# transform first 3 columns
# to be the x, y, z coordinate
samples[:, 0] = (samples[:, 0] * voxel_size) + voxel_origin[2]
samples[:, 1] = (samples[:, 1] * voxel_size) + voxel_origin[1]
samples[:, 2] = (samples[:, 2] * voxel_size) + voxel_origin[0]
num_samples = N ** 3
return samples.unsqueeze(0), voxel_origin, voxel_size
# syn ----------------------------------------------------------------------------
# def rand_light():
# # u_1 = np.abs(np.random.normal(0,0.2,(1))).clip(0,0.9)
# u_1 = np.array([0.3])
# u_2 = np.random.uniform(0,1,(1))
# theta = 2*np.pi*u_2
# r = np.sqrt(u_1)
# z = np.sqrt(1-r*r)
# x = r*np.cos(theta)
# y = r*np.sin(theta)
# light_pos = np.concatenate((x,y,z),axis=0) * 4.
# return light_pos
# real ----------------------------------------------------------------------------
def rand_light():
r = 0.5
tmp = np.random.uniform(-r,r,(2,))
x = np.array([tmp[0]])
y = np.array([tmp[1]])
# print("x: ", x)
# print("y: ", y)
light_pos = np.concatenate((x,y,np.array([1])),axis=0)*4
return light_pos
#----------------------------------------------------------------------------
@click.command()
@click.option('--network', 'network_pkl', help='Network pickle filename', required=True)
@click.option('--seeds', type=num_range, help='List of random seeds (e.g., \'0,1,4-6\')', required=True)
@click.option('--trunc', 'truncation_psi', type=float, help='Truncation psi', default=1, show_default=True)
@click.option('--trunc-cutoff', 'truncation_cutoff', type=int, help='Truncation cutoff', default=14, show_default=True)
@click.option('--class', 'class_idx', type=int, help='Class label (unconditional if not specified)')
@click.option('--outdir', help='Where to save the output images', type=str, required=True, metavar='DIR')
@click.option('--reload_modules', help='Overload persistent modules?', type=bool, required=False, metavar='BOOL', default=False, show_default=True)
def generate_images(
network_pkl: str,
seeds: List[int],
truncation_psi: float,
truncation_cutoff: int,
outdir: str,
class_idx: Optional[int],
reload_modules: bool,
):
"""Generate images using pretrained network pickle.
Examples:
\b
# Generate an image using pre-trained FFHQ model.
python gen_samples.py --outdir=output --trunc=0.7 --seeds=0-5 --shapes=True\\
--network=ffhq-rebalanced-128.pkl
"""
print('Loading networks from "%s"...' % network_pkl)
device = torch.device('cuda')
with dnnlib.util.open_url(network_pkl) as f:
G = legacy.load_network_pkl(f)['G_ema'].to(device) # type: ignore
# for key in G.init_kwargs:
# if key=="synthesis_kwargs":
# print(key)
# for key2 in G.init_kwargs[key]:
# print(key2)
init_kwargs_tmp = G.init_kwargs
try:
if 'mlp_fea' not in init_kwargs_tmp:
init_kwargs_tmp['mlp_fea']=32
if 'mlp_hidden' not in init_kwargs_tmp:
init_kwargs_tmp['mlp_hidden']=64
init_kwargs_tmp["synthesis_kwargs"].pop('high_res', None)
except KeyError:
print("""""""""""""""""""")
pass
# Specify reload_modules=True if you want code modifications to take effect; otherwise uses pickled code
if reload_modules:
print("Reloading Modules!")
# G_new = Generator(*G.init_args, **G.init_kwargs).eval().requires_grad_(False).to(device)
G_new = Generator(*G.init_args, **init_kwargs_tmp).eval().requires_grad_(False).to(device)
misc.copy_params_and_buffers(G, G_new, require_all=True)
G = G_new
os.makedirs(outdir, exist_ok=True)
# Generate images.
for seed_idx, seed in enumerate(seeds):
print('Generating image for seed %d (%d/%d) ...' % (seed, seed_idx, len(seeds)))
z = torch.from_numpy(np.random.RandomState(seed).randn(1, G.z_dim)).to(device)
# for i in range(5000):
# print('Generating image %d ...' % (i))
# z = torch.from_numpy(np.random.randn(1, G.z_dim)).to(device)
cond_li = torch.from_numpy(rand_light()).unsqueeze(0).to(device)
print('cond li: ', cond_li.shape)
ws = G.mapping(z, cond_li, truncation_psi=truncation_psi, truncation_cutoff=truncation_cutoff)
img = G.synthesis(ws, cond_li)
img = (img.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)
PIL.Image.fromarray(img[0].cpu().numpy(), 'RGB').save(f'{outdir}/seed{seed:04d}.png')
# PIL.Image.fromarray(img[0].cpu().numpy(), 'RGB').save(f'{outdir}/i_{seed_idx:04d}.png')
# for tiled
#----------------------------------------------------------------------------
if __name__ == "__main__":
generate_images() # pylint: disable=no-value-for-parameter
#----------------------------------------------------------------------------