forked from nashory/pggan-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_interpolated.py
59 lines (47 loc) · 1.65 KB
/
generate_interpolated.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
# generate interpolated images.
import os, sys
import torch
from config import config
from torch.autograd import Variable
import utils as utils
use_cuda = True
checkpoint_path = "repo/model/gen_R8_T55.pth.tar"
n_intp = 20
# load trained model.
import network as net
test_model = net.Generator(config)
if use_cuda:
torch.set_default_tensor_type("torch.cuda.FloatTensor")
test_model = torch.nn.DataParallel(test_model).cuda(device=0)
else:
torch.set_default_tensor_type("torch.FloatTensor")
for resl in range(3, config.max_resl + 1):
test_model.module.grow_network(resl)
test_model.module.flush_network()
print(test_model)
print("load checkpoint form ... {}".format(checkpoint_path))
checkpoint = torch.load(checkpoint_path)
test_model.module.load_state_dict(checkpoint["state_dict"])
# create folder.
for i in range(1000):
name = "repo/interpolation/try_{}".format(i)
if not os.path.exists(name):
os.system("mkdir -p {}".format(name))
break
# interpolate between twe noise(z1, z2).
z_intp = torch.FloatTensor(1, config.nz)
z1 = torch.FloatTensor(1, config.nz).normal_(0.0, 1.0)
z2 = torch.FloatTensor(1, config.nz).normal_(0.0, 1.0)
if use_cuda:
z_intp = z_intp.cuda()
z1 = z1.cuda()
z2 = z2.cuda()
test_model = test_model.cuda()
z_intp = Variable(z_intp)
for i in range(1, n_intp + 1):
alpha = 1.0 / float(n_intp + 1)
z_intp.data = z1.mul_(alpha) + z2.mul_(1.0 - alpha)
fake_im = test_model.module(z_intp)
fname = os.path.join(name, "_intp{}.jpg".format(i))
utils.save_image_single(fake_im.data, fname, imsize=pow(2, config.max_resl))
print("saved {}-th interpolated image ...".format(i))