-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalphabet_song.py
121 lines (97 loc) · 3.31 KB
/
alphabet_song.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import random
import sys
from lib import *
from util import *
out_model_dir = checked("./alphabet_model")
vertex_image_dir = checked("./alphabet_vertex")
gen_image_dir = checked("./alphabet_song")
logger = getFileLogger("alphabet_vertex")
logger.info("Start!")
using_gpu = False
xp = np
try:
cuda.check_cuda_available()
xp = cuda.cupy
cuda.get_device(0).use()
using_gpu = True
except:
print "I'm sorry. Using CPU."
nz = 100
# load model =======================================================================
gen = Generator(nz=nz)
o_gen = optimizers.Adam(alpha=0.0002, beta1=0.5)
o_gen.setup(gen)
o_gen.add_hook(chainer.optimizer.WeightDecay(0.00001))
if using_gpu:
gen.to_gpu()
serializers.load_hdf5("%s/dcgan_model_gen.h5" % out_model_dir, gen)
serializers.load_hdf5("%s/dcgan_state_gen.h5" % out_model_dir, o_gen)
# load gen params ================================================================
gen_params = {}
# letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
# letters = "abcdefghijklmnopqrstuvwxyz"
for letter in letters:
z = xp.load("%s/%s.npy" % (vertex_image_dir, letter))
gen_params[letter] = z
# Funcs =============================================================================
class Clip(chainer.Function):
def forward(self, x):
x = x[0]
ret = cuda.elementwise(
'T x', 'T ret',
'''
ret = x<-1?-1:(x>1?1:x);
''', 'clip')(x)
return ret
def clip(x):
return np.float32(-1 if x < -1 else (1 if x > 1 else x))
# gen =========================================================
print "Let's sing ABC song!"
# target_titles = []
#
# for i in range(27):
# choices = random.sample(letters, 2)
# target_titles += [choices[0], choices[1], "+".join(choices)]
#
# def conv_to_params(title):
# if title.count("+"):
# titles = title.split("+")
# # gen_params[t]は、cupyのndarrayになっているっぽい
# # np.mean, cupy.meanの方法がうまくいかないので、愚直に平均を出す
# res = xp.zeros(100, dtype=np.float32)
# for t in titles:
# res = res + gen_params[t]
# print res / len(titles)
# return res / len(titles)
# # return (gen_params[titles[0]] + gen_params[titles[1]]) / 2
# else:
# return gen_params[title]
#
# targets = map(conv_to_params, target_titles)
targets = []
for current, next in zip(letters, letters[1:]):
for ratio in np.arange(1, 0, -0.02).tolist():
targets.append(gen_params[current] * ratio + gen_params[next] * (1 - ratio))
assert len(targets) == 50 * (26 - 1)
gen_param = xp.array(targets, dtype=np.float32)
z = Variable(gen_param)
x = gen(z, test=True)
x = x.data.get() # 生成画像リスト
pylab.rcParams['figure.figsize'] = (16.0, 16.0)
pylab.clf()
y = xp.array(x)
print y.shape
image_arrays = []
for i_ in range(len(targets)):
_tmp = cuda.to_cpu(y[i_, 0, :, :])
# if using_gpu:
# tmp = Clip().forward(y[i_, 0, :, :]).get()
# else:
# tmp = np.vectorize(clip)(y[i_, 0, :, :])
tmp = np.vectorize(clip)(_tmp)
image_arrays.append(tmp)
animate(image_arrays, title="abc")
# ===============================================================