-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinterpolate.py
More file actions
66 lines (54 loc) · 2.26 KB
/
Copy pathinterpolate.py
File metadata and controls
66 lines (54 loc) · 2.26 KB
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
import argparse, torch
from torchvision import utils
from wali_celeba import create_WALI
NLAT = 100
IMAGE_SIZE = 64
NUM_CHANNELS = 3
def interpolate(generator, z0, z1, nintp=10, path='linear', filepath=None):
""" Interpolate in the latent space.
Args:
generator: Generator network that takes z as input.
z0: Where interpolation starts.
z1: Where interpolation ends.
nintp: Number of intermediate steps.
path: Trajectory of interpolation. Default: linear
filepath: Where to save the images.
"""
assert path in ['linear', 'spherical']
assert z1.size() == z1.size()
z0, z1 = z0.view(z0.size(0), NLAT, 1, 1), z1.view(z1.size(0), NLAT, 1, 1)
alphas = torch.linspace(0, 1, nintp)
imgs = []
if path == 'linear':
for alpha in alphas:
z = z0 * alpha + z1 * (1 - alpha)
img = generator(z).detach_() * 0.5 + 0.5
imgs.append(img.cpu())
elif path == 'spherical':
nz0, nz1 = z0 / z0.norm(dim=1, keepdim=True), z1 / z1.norm(dim=1, keepdim=True)
theta = ((nz0 * nz1).sum(dim=1, keepdim=True)).acos()
for alpha in alphas:
z = torch.sin((1 - alpha) * theta) / torch.sin(theta) * z0 \
+ torch.sin(alpha * theta) / torch.sin(theta) * z1
img = generator(z).detach_() * 0.5 + 0.5
imgs.append(img.cpu())
imgs = torch.cat(imgs, dim=1).view(-1, NUM_CHANNELS, IMAGE_SIZE, IMAGE_SIZE)
grid = utils.make_grid(imgs, nrow=nintp)
utils.save_image(grid, filepath)
print('Interpolated images saved.')
def main(args):
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
wali = create_WALI()
ckpt = torch.load(args.ckpt)
wali.load_state_dict(ckpt)
generator = wali.G.to(device)
z0 = torch.randn(args.n, NLAT, 1, 1).to(device)
z1 = torch.randn(args.n, NLAT, 1, 1).to(device)
interpolate(generator, z0, z1, nintp=10, path='linear', filepath=args.save_path)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Plot interpolations for WALI.')
parser.add_argument("--ckpt", type=str, help='Path to the saved model checkpoint', default=None)
parser.add_argument("--n", type=int, help="number of interpolated paths", default=4)
parser.add_argument("--save-path", type=str, help="where to save the interpolations", default=None)
args = parser.parse_args()
main(args)