-
Notifications
You must be signed in to change notification settings - Fork 1
/
sampler.py
executable file
·202 lines (183 loc) · 6.32 KB
/
sampler.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import numpy as np
import imageio
import torch
import matplotlib.pyplot as plt
import argparse
from PIL import Image
from model import CPPN
from mnist_data import MNIST
from torchvision.utils import save_image
parser = argparse.ArgumentParser()
parser.add_argument('--model', type=str, default='./model_final.pt',
help='The model to be loaded')
parser.add_argument('--x_dim', type=int, default=1080,
help='Number of pixels in the x dimension')
parser.add_argument('--y_dim', type=int, default=1080,
help='Number of pixels in the y dimension')
parser.add_argument('--scale', type=float, default=5.0,
help='The scale is a zoom-in/zoom-out parameter')
parser.add_argument('--cuda', action='store_true', help='use GPU computation')
parser.add_argument('--output', type=str, default='png',
help='Type of output: gif or png image')
parser.add_argument('--file_save', type=str, default='./images/test',
help='The file in which the output will be saved')
parser.add_argument('--frames', type=int, default=2,
help='number of frames in an animation for the gif')
opt = parser.parse_args()
print(opt)
mnist = MNIST()
mnist_train = mnist.train_loader
if opt.cuda:
cuda_gpu = torch.device('cuda:0')
cppn = CPPN(x_dim=opt.x_dim, y_dim=opt.y_dim, scale=opt.scale,
cuda_device=cuda_gpu)
cppn.cuda()
cppn.load_state_dict(torch.load(opt.model))
else:
cppn = CPPN(x_dim=opt.x_dim, y_dim=opt.y_dim, scale=opt.scale)
cppn.load_state_dict(torch.load(opt.model, map_location='cpu'))
cppn.eval()
enc = []
lab = []
for idx, (im, label) in enumerate(mnist_train):
with torch.no_grad():
if opt.cuda:
im = im.cuda()
mean, logvar = cppn.encoder(im)
encoding = cppn.reparametrize(mean, logvar)
enc.append(encoding)
lab.append(lab)
if idx > 5:
break
if opt.file_save == 'test':
print('WARNING - The name of the file in which the output will be stored \
is still the one by default (test.gif or test.png).')
if opt.output == 'png':
for i in range(len(enc)):
im = enc[i]
la = lab[i]
file_save = opt.file_save + '_' + str(i) + '.png'
im = cppn.generator.generate_image(im)
im = Image.fromarray(im)
im.save(file_save)
# save_image(im, file_save)
def save_anim_gif(filename=opt.file_save, n_frame=opt.frames,
duration1=0.5, duration2=1.0, duration=0.1,
number1=1, number2=8, number3=4,
scale1=4.0, scale2=17, reverse_gif=True):
images = []
enc = []
n1 = True
n2 = True
n3 = True
n4 = True
n5 = True
n6 = True
n7 = True
n8 = True
n9 = True
for idx, (im, label) in enumerate(mnist_train):
if label.item() == 1 and n1:
n1 = False
if opt.cuda:
im = im.cuda()
mean, logvar = cppn.encoder(im)
enc.append(cppn.reparametrize(mean, logvar))
if label.item() == 2 and n2:
n2 = False
if opt.cuda:
im = im.cuda()
mean, logvar = cppn.encoder(im)
enc.append(cppn.reparametrize(mean, logvar))
if label.item() == 3 and n3:
n3 = False
if opt.cuda:
im = im.cuda()
mean, logvar = cppn.encoder(im)
enc.append(cppn.reparametrize(mean, logvar))
if label.item() == 4 and n4:
n4 = False
if opt.cuda:
im = im.cuda()
mean, logvar = cppn.encoder(im)
enc.append(cppn.reparametrize(mean, logvar))
if label.item() == 5 and n5:
n5 = False
if opt.cuda:
im = im.cuda()
mean, logvar = cppn.encoder(im)
enc.append(cppn.reparametrize(mean, logvar))
if label.item() == 6 and n6:
n6 = False
if opt.cuda:
im = im.cuda()
mean, logvar = cppn.encoder(im)
enc.append(cppn.reparametrize(mean, logvar))
if label.item() == 7 and n7:
n7 = False
if opt.cuda:
im = im.cuda()
mean, logvar = cppn.encoder(im)
enc.append(cppn.reparametrize(mean, logvar))
if label.item() == 8 and n8:
n8 = False
if opt.cuda:
im = im.cuda()
mean, logvar = cppn.encoder(im)
enc.append(cppn.reparametrize(mean, logvar))
if label.item() == 9 and n9:
n9 = False
if opt.cuda:
im = im.cuda()
mean, logvar = cppn.encoder(im)
enc.append(cppn.reparametrize(mean, logvar))
delta = []
n = len(enc)
for i in range(n-1):
delta_z = (enc[i+1] - enc[i]) / (n_frame + 1)
delta.append(delta_z)
delta_s = (scale2 - scale1) / (n_frame + 1)
s = scale1
e = enc[0]
frames = 0
for i in range(n_frame):
cppn.generator.scale = s
im = cppn.generator.generate_image(e)
# im = Image.fromarray(im)
images.append(im)
s += delta_s
frames += 1
print(images)
durations = [duration1] + [duration]*(frames - 2) + [duration2]
revImages = list(images)
revImages.reverse()
revImages = revImages[1:]
images = images + revImages
durations = durations + [duration] * (frames - 2) + [duration1]
frames = 0
cppn.generator.scale = (scale1 + scale2) / 2.
images2 = []
for j in range(len(delta)):
z1 = enc[j]
delta_z = delta[j]
for i in range(n_frame):
z_gen = z1 + delta_z*float(i)
print("processing image ", i)
im = cppn.generator.generate_image(z_gen)
# im = Image.fromarray(im)
images2.append(im)
frames += 1
durations2 = [duration] + [duration]*(frames - 2) + [duration2]
if reverse_gif:
rev = list(images2)
rev.reverse()
rev = rev[1:]
images2 = images2 + rev
durations2 = durations2 + [duration] * (frames - 2) + [duration1]
images = images + images2
durations = durations + durations2
print("Writing a gif...")
filename = filename + ".gif"
imageio.mimsave(filename, images, duration=durations)
if opt.output == 'gif':
save_anim_gif()