forked from nashory/pggan-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_image_grid.py
60 lines (44 loc) · 1.43 KB
/
generate_image_grid.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
# generate interpolated images.
import os,sys
import torch
from config import config
from torch.autograd import Variable
import utils as utils
from torchvision.utils import save_image
use_cuda = True
checkpoint_path = 'repo/model/gen_R8_T4200.pth.tar'
n_row = 1
n_col = 1
n_image = n_col * n_col
trials = 50
# 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/grid/{}'.format(i)
if not os.path.exists(name):
os.system('mkdir -p {}'.format(name))
break
# interpolate between twe noise(z1, z2).
for i in range(trials):
z1 = torch.FloatTensor(1, config.nz).normal_(0.0, 1.0)
if use_cuda:
z1 = z1.cuda()
test_model = test_model.cuda()
z = Variable(z1)
fake_im = test_model.module(z)
fname = os.path.join(name, '{}.jpg'.format(i))
utils.save_image_single(fake_im.data, fname, imsize=pow(2,config.max_resl))