forked from SizheAn/PanoHead
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_interpolation.py
218 lines (163 loc) · 7.68 KB
/
gen_interpolation.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
''' Generate images and shapes using pretrained network pickle.
Code adapted from following paper
"Efficient Geometry-aware 3D Generative Adversarial Networks."
See LICENSES/LICENSE_EG3D for original license.
'''
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 camera_utils import LookAtPoseSampler, FOV_to_intrinsics
from torch_utils import misc
from training.triplane import TriPlaneGenerator
from torchvision.models.segmentation import deeplabv3_resnet101
from torchvision import transforms, utils
from torch.nn import functional as F
#----------------------------------------------------------------------------
def parse_range(s: Union[str, List]) -> List[int]:
'''Parse a comma separated list of numbers or ranges and return a list of ints.
Example: '1,2,5-10' returns [1, 2, 5, 6, 7]
'''
if isinstance(s, list): return s
ranges = []
range_re = re.compile(r'^(\d+)-(\d+)$')
for p in s.split(','):
m = range_re.match(p)
if m:
ranges.extend(range(int(m.group(1)), int(m.group(2))+1))
else:
ranges.append(int(p))
return ranges
#----------------------------------------------------------------------------
def get_mask(model, batch, cid):
normalized_batch = transforms.functional.normalize(
batch, mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225))
output = model(normalized_batch)['out']
# sem_classes = [
# '__background__', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus',
# 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike',
# 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor'
# ]
# sem_class_to_idx = {cls: idx for (idx, cls) in enumerate(sem_classes)}
# cid = sem_class_to_idx['car']
normalized_masks = torch.nn.functional.softmax(output, dim=1)
boolean_car_masks = (normalized_masks.argmax(1) == cid)
return boolean_car_masks.float()
def norm_ip(img, low, high):
img_ = img.clamp(min=low, max=high)
img_.sub_(low).div_(max(high - low, 1e-5))
return img_
def norm_range(t, value_range=(-1, 1)):
if value_range is not None:
return norm_ip(t, value_range[0], value_range[1])
else:
return norm_ip(t, float(t.min()), float(t.max()))
#----------------------------------------------------------------------------
@click.command()
@click.option('--network', 'network_pkl', help='Network pickle filename', required=True)
@click.option('--trunc', 'truncation_psi', type=float, help='Truncation psi', default=0.7, show_default=True)
@click.option('--trunc-cutoff', 'truncation_cutoff', type=int, help='Truncation cutoff', default=14, show_default=True)
@click.option('--fov-deg', help='Field of View of camera in degrees', type=int, required=False, metavar='float', default=18.837, show_default=True)
@click.option('--reload_modules', help='Overload persistent modules?', type=bool, required=False, metavar='BOOL', default=False, show_default=True)
@click.option('--pose_cond', type=int, help='pose_cond angle', default=90, show_default=True)
@click.option('--outdir', help='Where to save the output images', type=str, required=True, metavar='DIR')
def generate_images(
network_pkl: str,
outdir: str,
truncation_psi: float,
truncation_cutoff: int,
fov_deg: float,
reload_modules: bool,
pose_cond: int,
):
"""Generate interpolation images using pretrained network pickle.
Examples:
\b
python gen_interpolation.py --network models/easy-khair-180-gpc0.8-trans10-025000.pkl\
--trunc 0.7 --outdir interpolation_out
"""
print('Loading networks from "%s"...' % network_pkl)
device = torch.device('cuda:0')
with dnnlib.util.open_url(network_pkl) as f:
G = legacy.load_network_pkl(f)['G_ema'].to(device) # type: ignore
# Specify reload_modules=True if you want code modifications to take effect; otherwise uses pickled code
if reload_modules:
print("Reloading Modules!")
G_new = TriPlaneGenerator(*G.init_args, **G.init_kwargs).eval().requires_grad_(False).to(device)
misc.copy_params_and_buffers(G, G_new, require_all=True)
G_new.neural_rendering_resolution = G.neural_rendering_resolution
G_new.rendering_kwargs = G.rendering_kwargs
G = G_new
network_pkl = os.path.basename(network_pkl)
outdir = os.path.join(outdir, os.path.splitext(network_pkl)[0] + '_' + str(pose_cond))
os.makedirs(outdir, exist_ok=True)
pose_cond_rad = pose_cond/180*np.pi
intrinsics = FOV_to_intrinsics(fov_deg, device=device)
# n_sample = 64
# batch = 32
# n_sample = n_sample // batch * batch
# batch_li = n_sample // batch * [batch]
pose_cond_rad = pose_cond/180*np.pi
intrinsics = FOV_to_intrinsics(fov_deg, device=device)
# Generate images.
cam_pivot = torch.tensor([0, 0, 0], device=device)
cam_radius = G.rendering_kwargs.get('avg_camera_radius', 2.7)
conditioning_cam2world_pose = LookAtPoseSampler.sample(pose_cond_rad, np.pi/2, cam_pivot, radius=cam_radius, device=device)
conditioning_params = torch.cat([conditioning_cam2world_pose.reshape(-1, 16), intrinsics.reshape(-1, 9)], 1)
conditioning_cam2world_pose_back = LookAtPoseSampler.sample(-pose_cond_rad, np.pi/2, cam_pivot, radius=cam_radius, device=device)
conditioning_params_back = torch.cat([conditioning_cam2world_pose_back.reshape(-1, 16), intrinsics.reshape(-1, 9)], 1)
conditioning_cam2world_pose_side = LookAtPoseSampler.sample(45/180*np.pi, np.pi/2, cam_pivot, radius=cam_radius, device=device)
conditioning_params_side = torch.cat([conditioning_cam2world_pose_side.reshape(-1, 16), intrinsics.reshape(-1, 9)], 1)
# set two random seeds for interpolation
seed1, seed2 = 521, 329
z0 = torch.from_numpy(np.random.RandomState(seed1).randn(G.z_dim).reshape(1,G.z_dim)).to(device)
z1 = torch.from_numpy(np.random.RandomState(seed2).randn(G.z_dim).reshape(1,G.z_dim)).to(device)
c = conditioning_params
ws0 = G.mapping(z0, c, truncation_psi=0.7, truncation_cutoff=None)
ws1 = G.mapping(z1, c, truncation_psi=0.7, truncation_cutoff=None)
image_final = []
for c in [conditioning_params, conditioning_params_side, conditioning_params_back]:
img0 = G.synthesis(ws0, c)['image']
img0 = norm_range(img0)
img1 = G.synthesis(ws1, c)['image']
img1 = norm_range(img1)
img_list = []
for interpolation_idx in [0,2,3,4,6,8]:
# for interpolation_idx in range(0,14,1):
# interpolation_idx = 8
ws_new = ws0.clone()
ws_new[:, interpolation_idx:, :] = ws1[:, interpolation_idx:, :]
img_new = G.synthesis(ws_new, c)['image']
img_new = norm_range(img_new)
img_list.append(img_new)
img_list.append(img0)
img_new = torch.cat(img_list, dim=0)
image_final.append(img_new)
image_final = torch.cat(image_final, dim=2)
utils.save_image(
image_final,
os.path.join(outdir, f'img_interpolation_seed{seed1}_{seed2}.png'),
# nrow=8,
normalize=True,
range=(0, 1),
padding=0,
)
# utils.save_image(
# diff,
# f'alphamse/changebg/diff.png',
# nrow=8,
# normalize=True,
# range=(0, 1),
# padding=0,
# )
# sys.exit()
#----------------------------------------------------------------------------
if __name__ == "__main__":
generate_images() # pylint: disable=no-value-for-parameter
#----------------------------------------------------------------------------