-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtrain_AAE.py
325 lines (231 loc) · 9.47 KB
/
train_AAE.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# Copyright 2018 Ranya Almohsen
#
# Copyright 2018 Stanislav Pidhorskyi
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
from __future__ import print_function
import torch.utils.data
from torch import optim
from torchvision.utils import save_image
from net import *
import torch.nn.functional as F
from torch.autograd import Variable
import numpy as np
import json
import pickle
import time
import random
import os
import math
import json
from sklearn import svm
from sklearn.svm import SVC
from sklearn.multiclass import OneVsRestClassifier
from sklearn.externals import joblib
use_cuda = torch.cuda.is_available()
FloatTensor = torch.FloatTensor
IntTensor = torch.IntTensor
LongTensor = torch.LongTensor
torch.set_default_tensor_type('torch.FloatTensor')
# If zd_merge true, will use zd discriminator that looks at entire batch.
zd_merge = False
if use_cuda:
device = torch.cuda.current_device()
torch.set_default_tensor_type('torch.cuda.FloatTensor')
FloatTensor = torch.cuda.FloatTensor
IntTensor = torch.cuda.IntTensor
LongTensor = torch.cuda.LongTensor
print("Running on ", torch.cuda.get_device_name(device))
def setup(x):
if use_cuda:
return x.cuda()
else:
return x.cpu()
def numpy2torch(x):
return setup(torch.from_numpy(x))
def extract_batch(data, it, batch_size):
x = numpy2torch(data[it * batch_size:(it + 1) * batch_size, :, :]) / 255.0
# x.sub_(0.5).div_(0.5)
return Variable(x)
def extract_batch_label(data, it, batch_size):
y = numpy2torch(data[it * batch_size:(it + 1) * batch_size])
# x.sub_(0.5).div_(0.5)
return Variable(y)
def main(folding_id, class_fold, folds=5):
batch_size = 128
zsize = 32
mnist_train = []
mnist_valid = []
global fold_id
global fake_class_id
fold_id = folding_id
#reading from class_table_fold 0,1,2,3,4
class_data = json.load(open('class_table_fold_%d.txt' % class_fold))
train_classes = class_data[0]["train"]
inliner_classes = train_classes
#test_classes = class_data[opennessid]["test_target"]
#openness = 1.0 - math.sqrt(2 * len(train_classes) / (len(train_classes) + len(test_classes)))
#print("\tOpenness: %f" % openness)
for i in range(folds):
if i != folding_id:
with open('data_fold_%d.pkl' % i, 'rb') as pkl:
fold = pickle.load(pkl)
if len(mnist_valid) == 0:
mnist_valid = fold
mnist_train += fold
with open('data_fold_%d.pkl' % folding_id, 'rb') as pkl:
mnist_test = pickle.load(pkl)
random.shuffle(mnist_train)
random.shuffle(mnist_valid)
#outlier_classes = []
#outlier_classes = [x for x in test_classes if x not in inliner_classes]
# keep only train classes
mnist_train = [x for x in mnist_train if x[0] in inliner_classes]
#keep only test classes
#mnist_valid = [x for x in mnist_valid if x[0] in test_classes]
# mnist_test = [x for x in mnist_test if x[0] in test_classes]
def list_of_pairs_to_numpy(l):
return np.asarray([x[1] for x in l], np.float32), np.asarray([x[0] for x in l], np.int)
print("Train set size:", len(mnist_train))
mnist_train_x, mnist_train_y = list_of_pairs_to_numpy(mnist_train)
G = Generator(zsize, d=64)
setup(G)
G.weight_init(mean=0, std=0.02)
D = Discriminator(d=64)
setup(D)
D.weight_init(mean=0, std=0.02)
E = Encoder(zsize, d=64)
setup(E)
E.weight_init(mean=0, std=0.02)
if zd_merge:
ZD = ZDiscriminator_mergebatch(zsize, batch_size).to(device)
else:
ZD = ZDiscriminator(zsize, batch_size).to(device)
setup(ZD)
ZD.weight_init(mean=0, std=0.02)
lr = 0.002
G_optimizer = optim.Adam(G.parameters(), lr=lr, betas=(0.5, 0.999))
D_optimizer = optim.Adam(D.parameters(), lr=lr, betas=(0.5, 0.999))
E_optimizer = optim.Adam(E.parameters(), lr=lr, betas=(0.5, 0.999))
GE_optimizer = optim.Adam(list(E.parameters()) + list(G.parameters()), lr=lr, betas=(0.5, 0.999))
ZD_optimizer = optim.Adam(ZD.parameters(), lr=lr, betas=(0.5, 0.999))
train_epoch = 35
BCE_loss = nn.BCELoss()
y_real_ = torch.ones(batch_size)
y_fake_ = torch.zeros(batch_size)
y_real_z = torch.ones(1 if zd_merge else batch_size)
y_fake_z = torch.zeros(1 if zd_merge else batch_size)
sample = torch.randn(64, zsize).view(-1, zsize, 1, 1)
for epoch in range(train_epoch):
G.train()
D.train()
E.train()
ZD.train()
Gtrain_loss = 0
Dtrain_loss = 0
Etrain_loss = 0
GEtrain_loss = 0
ZDtrain_loss = 0
epoch_start_time = time.time()
def shuffle(X):
np.take(X, np.random.permutation(X.shape[0]), axis=0, out=X)
shuffle(mnist_train_x)
if (epoch + 1) % 30 == 0:
G_optimizer.param_groups[0]['lr'] /= 4
D_optimizer.param_groups[0]['lr'] /= 4
GE_optimizer.param_groups[0]['lr'] /= 4
E_optimizer.param_groups[0]['lr'] /= 4
ZD_optimizer.param_groups[0]['lr'] /= 4
print("learning rate change!")
for it in range(len(mnist_train_x) // batch_size):
x = extract_batch(mnist_train_x, it, batch_size).view(-1, 1, 32, 32)
#############################################
D.zero_grad()
D_result = D(x).squeeze()
D_real_loss = BCE_loss(D_result, y_real_)
z = torch.randn((batch_size, zsize)).view(-1, zsize, 1, 1)
z = Variable(z)
x_fake = G(z).detach()
D_result = D(x_fake).squeeze()
D_fake_loss = BCE_loss(D_result, y_fake_)
D_train_loss = D_real_loss + D_fake_loss
D_train_loss.backward()
D_optimizer.step()
Dtrain_loss += D_train_loss.item()
#############################################
G.zero_grad()
z = torch.randn((batch_size, zsize)).view(-1, zsize, 1, 1)
z = Variable(z)
x_fake = G(z)
D_result = D(x_fake).squeeze()
G_train_loss = BCE_loss(D_result, y_real_)
G_train_loss.backward()
G_optimizer.step()
Gtrain_loss += G_train_loss.item()
#############################################
ZD.zero_grad()
z = torch.randn((batch_size, zsize)).view(-1, zsize)
z = Variable(z)
ZD_result = ZD(z).squeeze()
ZD_real_loss = BCE_loss(ZD_result, y_real_z)
z = E(x).squeeze().detach()
ZD_result = ZD(z).squeeze()
ZD_fake_loss = BCE_loss(ZD_result, y_fake_z)
ZD_train_loss = ZD_real_loss + ZD_fake_loss
ZD_train_loss.backward()
ZD_optimizer.step()
ZDtrain_loss += ZD_train_loss.item()
#clf.fit(z, y)
#############################################
E.zero_grad()
G.zero_grad()
z = E(x)
x_d = G(z)
ZD_result = ZD(z.squeeze()).squeeze()
E_loss = BCE_loss(ZD_result, y_real_z) * 1.0
Recon_loss = F.binary_cross_entropy(x_d, x)
(Recon_loss + E_loss).backward()
GE_optimizer.step()
GEtrain_loss += Recon_loss.item()
Etrain_loss += E_loss.item()
if it == 0:
directory = 'results' + str(inliner_classes[0])
if not os.path.exists(directory):
os.makedirs(directory)
comparison = torch.cat([x[:64], x_d[:64]])
save_image(comparison.cpu(),
'results' + str(inliner_classes[0]) + '/reconstruction_' + str(epoch) + '.png', nrow=64)
Gtrain_loss /= (len(mnist_train_x))
Dtrain_loss /= (len(mnist_train_x))
ZDtrain_loss /= (len(mnist_train_x))
GEtrain_loss /= (len(mnist_train_x))
Etrain_loss /= (len(mnist_train_x))
epoch_end_time = time.time()
per_epoch_ptime = epoch_end_time - epoch_start_time
print('[%d/%d] - ptime: %.2f, Gloss: %.3f, Dloss: %.3f, ZDloss: %.3f, GEloss: %.3f, Eloss: %.3f' % (
(epoch + 1), train_epoch, per_epoch_ptime, Gtrain_loss, Dtrain_loss, ZDtrain_loss, GEtrain_loss, Etrain_loss))
with torch.no_grad():
resultsample = G(sample).cpu()
directory = 'results' + str(inliner_classes[0])
if not os.path.exists(directory):
os.makedirs(directory)
save_image(resultsample.view(64, 1, 32, 32),
'results' + str(inliner_classes[0]) + '/sample_' + str(epoch) + '.png')
print("Training finish!... save training results")
torch.save(G.state_dict(), "Gmodel_%d_%d.pkl" % (folding_id, class_fold))
torch.save(E.state_dict(), "Emodel_%d_%d.pkl" % (folding_id, class_fold))
#torch.save(D.state_dict(), "Dmodel_%d_%d.pkl")
#torch.save(ZD.state_dict(), "ZDmodel_%d_%d.pkl")
if __name__ == '__main__':
main(0, 0)