-
Notifications
You must be signed in to change notification settings - Fork 2
/
latent_interp.py
144 lines (119 loc) · 3.42 KB
/
latent_interp.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Sample code for inference of Progressive Growing of GANs paper
(https://github.com/tkarras/progressive_growing_of_gans)
using a CelebA snapshot
"""
from __future__ import print_function
import argparse
import os
import numpy as np
import torch
from torch.autograd import Variable
from torch.utils.data.dataloader import DataLoader
from model import Generator
from utils import LatentDataset, save_images
interp_types = ['gauss', 'slerp']
use_cuda = False
parser = argparse.ArgumentParser(description='Interpolation demo')
parser.add_argument(
'--weights',
default='100_celeb_hq_network-snapshot-010403.pth',
type=str,
metavar='PATH',
help='path to PyTorch state dict')
parser.add_argument(
'--output',
type=str,
default='./interp',
help='Directory for storing interpolated imaged')
parser.add_argument(
'--batch_size',
default=1,
type=int,
help='batch size')
parser.add_argument(
'--num_workers',
default=1,
type=int,
help='number of workers for DataLoader')
parser.add_argument(
'--type',
default='gauss',
choices=interp_types,
help='interpolation types: ' +
' | '.join(interp_types) +
' (default: gauss)')
parser.add_argument(
'--nb_latents',
default=10,
type=int,
help='number of latent vectors to generate')
parser.add_argument(
'--filter',
default=2,
type=int,
help='gauss filter length for latent vector smoothing (\'gaus\' interp)')
parser.add_argument(
'--interp',
default=50,
type=int,
help='interpolation length between latents (\'slerp\' inter)')
parser.add_argument(
'--seed',
default=187,
type=int,
help='Random seed')
parser.add_argument(
'--cuda',
dest='cuda',
action='store_true',
help='Use GPU for processing')
def run(args):
global use_cuda
print('Loading Generator')
model = Generator()
model.load_state_dict(torch.load(args.weights))
if use_cuda:
model = model.cuda()
pin_memory = True
else:
pin_memory = False
# Generate latent data
latent_dataset = LatentDataset(interp_type=args.type,
nb_latents=args.nb_latents,
filter_latents=args.filter,
nb_interp=args.interp)
latent_loader = DataLoader(latent_dataset,
batch_size=args.batch_size,
num_workers=args.num_workers,
shuffle=False,
pin_memory=pin_memory)
print('Processing')
for i, data in enumerate(latent_loader):
if use_cuda:
data = data.cuda()
data = Variable(data, volatile=True)
output = model(data)
if use_cuda:
output = output.cpu()
images_np = output.data.numpy()
save_images(images_np, args.output, i*args.batch_size)
def main():
global use_cuda
args = parser.parse_args()
if not args.weights:
print('No PyTorch state dict path provided. Exiting...')
return
if args.cuda:
use_cuda = True
np.random.seed(args.seed)
torch.manual_seed(args.seed)
if use_cuda:
torch.cuda.manual_seed(args.seed)
if not os.path.exists(args.output):
os.mkdir(args.output)
run(args)
if __name__ == '__main__':
main()