forked from ptrblck/prog_gans_pytorch_inference
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pygame_interp_demo.py
134 lines (109 loc) · 3.35 KB
/
pygame_interp_demo.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
#!/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 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, scale_image_paper
import pygame
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(
'--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('--size', default=256, type=int, help='pygame window size')
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
# Init PYGame
pygame.init()
display = pygame.display.set_mode((args.size, args.size), 0)
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=1, # Since we want see it 'live'
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()
image = output.data.numpy()[0, ...].transpose(1, 2, 0)
image = np.rot90(scale_image_paper(image, [-1, 1], [0, 255]))
snapshot = pygame.surfarray.make_surface(image)
snapshot = pygame.transform.scale(snapshot, (args.size, args.size))
display.blit(snapshot, (0, 0))
pygame.display.flip()
def main():
global use_cuda
args = parser.parse_args()
if not args.weights:
print('No PyTorch state dict path privided. 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)
run(args)
if __name__ == '__main__':
main()