-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
184 lines (140 loc) · 4.74 KB
/
train.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
from models import Generator, Discriminator
from torch_dataset import TrainSet
import torch
import copy
import numpy as np
import tqdm
import os
import shutil
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
matplotlib.use('Agg')
sns.set_style('whitegrid')
device = 'cuda'
# * Create dir for store models
try:
shutil.rmtree('saved_models')
except:
print('Folder does not exist')
os.makedirs('saved_models', exist_ok=True)
# --- Data params
noise_size = 10
prosody_size = 6
hidden_size = 128
pose_size = 33
d_out_size = 1
# --- Training params
n_epochs = 5
sample_size = 32
d_lr = 1e-4
g_lr = 1e-4
unroll_steps = 10
log_interval = 100
# --- Init dataset
dataset = TrainSet()
dataset.scaling(True)
# --- Init net i_size, n_size, o_size
G = Generator(i_size=prosody_size, n_size=noise_size,
o_size=pose_size, h_size=hidden_size).to(device)
D = Discriminator(i_size=pose_size, c_size=prosody_size,
h_size=hidden_size).to(device)
g_opt = torch.optim.Adam(G.parameters(), lr=g_lr)
d_opt = torch.optim.Adam(D.parameters(), lr=d_lr)
bce_loss = torch.nn.BCELoss()
d_real_loss = []
d_fake_loss = []
g_loss = []
def sample_noise(batch_size, dim):
return np.random.normal(0, 1, (batch_size, dim))
for epoch in tqdm.tqdm(range(n_epochs)):
# for i in range(n_epochs):
torch.cuda.empty_cache()
# * Random sample
idxs = np.random.randint(low=0, high=len(dataset), size=sample_size)
# * Configure real data
xs = [dataset[i][0] for i in idxs]
real_ys = [dataset[i][1] for i in idxs]
# * Generate fake data
fake_ys = []
for x in xs:
x = torch.Tensor(x).unsqueeze(1).to(device)
noise = torch.Tensor(sample_noise(1, noise_size)
).unsqueeze(1).to(device)
with torch.no_grad():
fake_y = G(x, noise)
fake_ys.append(fake_y)
# * Train D
d_opt.zero_grad()
d_real_error = 0
d_fake_error = 0
for x, real_y, fake_y in zip(xs, real_ys, fake_ys):
x = torch.Tensor(x).unsqueeze(1).to(device)
real_y = torch.Tensor(real_y).unsqueeze(1).to(device)
real_logit = D(real_y, x)
real_label = torch.ones_like(real_logit)
real_error = bce_loss(real_logit, real_label)
d_real_error += real_error
fake_logit = D(fake_y, x)
fake_label = torch.zeros_like(fake_logit)
fake_error = bce_loss(fake_logit, fake_label)
d_fake_error += fake_error
d_real_error = d_real_error / sample_size
d_fake_error = d_fake_error / sample_size
d_loss = d_real_error + d_fake_error
d_loss.backward()
d_opt.step()
d_real_loss.append(d_real_error.cpu().detach().numpy())
d_fake_loss.append(d_fake_error.cpu().detach().numpy())
if unroll_steps:
# * Unroll D
d_backup = D.state_dict()
for k in range(unroll_steps):
# * Train D
d_opt.zero_grad()
d_real_error = 0
d_fake_error = 0
for x, real_y, fake_y in zip(xs, real_ys, fake_ys):
x = torch.Tensor(x).unsqueeze(1).to(device)
real_y = torch.Tensor(real_y).unsqueeze(1).to(device)
real_logit = D(real_y, x)
real_label = torch.ones_like(real_logit)
real_error = bce_loss(real_logit, real_label)
d_real_error += real_error
fake_logit = D(fake_y, x)
fake_label = torch.zeros_like(fake_logit)
fake_error = bce_loss(fake_logit, fake_label)
d_fake_error += fake_error
d_real_error = d_real_error / sample_size
d_fake_error = d_fake_error / sample_size
d_loss = d_real_error + d_fake_error
d_loss.backward()
d_opt.step()
# * Train G
g_opt.zero_grad()
g_error = 0
for x in xs:
x = torch.Tensor(x).unsqueeze(1).to(device)
noise = torch.Tensor(sample_noise(1, noise_size)
).unsqueeze(1).to(device)
gen_y = G(x, noise)
gen_logit = D(gen_y, x)
gen_lable = torch.ones_like(gen_logit)
gen_error = bce_loss(gen_logit, gen_lable)
g_error += gen_error
g_error = g_error / sample_size
g_error.backward()
g_opt.step()
if unroll_steps:
D.load_state_dict(d_backup)
g_loss.append(g_error.cpu().detach().numpy())
if epoch % log_interval == 0:
torch.save(G.state_dict(), f'saved_models/epoch_{epoch}.pt')
fig = plt.figure(dpi=100)
ax1 = fig.add_subplot(1, 1, 1)
ax1.plot(range(len(d_real_loss)), d_real_loss, label='d real loss')
ax1.plot(range(len(d_fake_loss)), d_fake_loss, label='d fake loss')
ax1.plot(range(len(g_loss)), g_loss, label='g loss')
ax1.legend()
plt.savefig('hist.png')
plt.close()