-
Notifications
You must be signed in to change notification settings - Fork 0
/
EGAN_train.py
223 lines (193 loc) · 7.58 KB
/
EGAN_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
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
import time
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
from torchvision import datasets
from torchvision.transforms import transforms
from model import discriminator, generator
import numpy as np
import matplotlib.pyplot as plt
import random
start_time = time.time()
plt.rcParams['image.cmap'] = 'gray'
def show_images(images):
sqrtn = int(np.ceil(np.sqrt(images.shape[0])))
for index, image in enumerate(images):
plt.subplot(sqrtn, sqrtn, index+1)
plt.imshow(image.reshape(28, 28))
# Discriminator Loss => BCELoss
def d_loss_function(inputs, targets):
return nn.BCELoss()(inputs, targets)
# Mutations
def g_BCE_function(inputs):
targets = torch.ones([inputs.shape[0], 1])
targets = targets.to(device)
return nn.BCELoss()(inputs, targets)
def g_MAE_function(inputs):
targets = torch.ones([inputs.shape[0], 1])
targets = targets.to(device)
return nn.SmoothL1Loss()(inputs, targets)
def g_MSE_function(inputs):
targets = torch.ones([inputs.shape[0], 1])
targets = targets.to(device)
return nn.MSELoss()(inputs, targets)
def fitnessScore(G, D, testloader):
num_output = 0
fq = 0
fd = 0
d1 = 0
d2 = 0
for data in testloader:
real_inputs = data[0].to(device)
test = 255 * (0.5 * real_inputs[0] + 0.5)
real_inputs = real_inputs.view(-1, 784)
real_outputs = D(real_inputs)
real_outputs_lst = real_outputs.tolist()
noise = (torch.rand(real_inputs.shape[0], 128) - 0.5) / 0.5
noise = noise.to(device)
fake_inputs = G(noise)
fake_outputs = D(fake_inputs)
fake_outputs_lst = fake_outputs.tolist()
for i in range(len(fake_outputs_lst)):
for j in range(len(fake_outputs_lst[i])):
num_output += 1
# fq
fq += fake_outputs_lst[i][j]
# fd
d1 += np.log(real_outputs_lst[i][j])
d2 += np.log(1 - fake_outputs_lst[i][j])
outputs = real_outputs + fake_outputs
gradients = torch.autograd.grad(outputs, inputs=D.parameters(),
grad_outputs=torch.ones(outputs.size()).to(device),
create_graph=True,retain_graph=True,only_inputs=True)
gradients = list(gradients)
with torch.no_grad():
for i, grad in enumerate(gradients):
grad = grad.view(-1)
allgrad = grad if i == 0 else torch.cat([allgrad,grad])
fd = -torch.log(torch.norm(allgrad)).data.cpu().numpy()
fq = fq/num_output
return fq, fd
def initialization(populationNum):
G = []
g_optimizer = []
for i in range(populationNum):
G.append(generator().to(device))
g_optimizer.append(optim.Adam(G[i].parameters(), lr=lr, betas=(0.5, 0.999)))
return G, g_optimizer
'''
# GPU
device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
print('GPU State:', device)
'''
# Settings
device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
epochs = 200
lr = 0.0002
batch_size = 32
population_size = 3
path = "parent.pth"
# Model
G, g_optimizer = initialization(population_size)
D = discriminator().to(device)
d_optimizer = optim.Adam(D.parameters(), lr=lr, betas=(0.5, 0.999))
print(G)
print(D)
# Transform
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))])
# Load data
train_set = datasets.MNIST('mnist/', train=True, download=True, transform=transform)
test_set = datasets.MNIST('mnist/', train=False, download=True, transform=transform)
train_loader = DataLoader(train_set, batch_size=batch_size, shuffle=True)
test_loader = DataLoader(test_set, batch_size=batch_size, shuffle=False)
# compute fitness
fitness_Lst = []
for i in range(len(G)):
fq,fd = fitnessScore(G[i],D,test_loader)
fitness_Lst.append(fq + 0.1 * fd)
print(fitness_Lst)
for epoch in range(epochs):
epoch += 1
for times, data in enumerate(train_loader):
# parent selection
print("1-parent selection")
index = random.randint(0, 2)
times += 1
real_inputs = data[0].to(device)
test = 255 * (0.5 * real_inputs[0] + 0.5)
real_inputs = real_inputs.view(-1, 784)
real_outputs = D(real_inputs)
real_label = torch.ones(real_inputs.shape[0], 1).to(device)
noise = (torch.rand(real_inputs.shape[0], 128) - 0.5) / 0.5
noise = noise.to(device)
fake_inputs = G[index](noise)
fake_outputs = D(fake_inputs)
fake_label = torch.zeros(fake_inputs.shape[0], 1).to(device)
outputs = torch.cat((real_outputs, fake_outputs), 0)
targets = torch.cat((real_label, fake_label), 0)
# Zero the parameter gradients
d_optimizer.zero_grad()
# Backward propagation
d_loss = d_loss_function(outputs, targets)
if(d_loss.item()>0.5):
print(d_loss.item())
d_loss.backward()
d_optimizer.step()
# Generator
noise = (torch.rand(real_inputs.shape[0], 128)-0.5)/0.5
noise = noise.to(device)
fake_inputs = G[index](noise)
fake_outputs = D(fake_inputs)
# Implement three different mutations
offspring = []
offspring_optimizer = []
offspring_fitness = []
g_loss = []
torch.save(G[index].state_dict(), path)
for i in range(3):
child = generator().to(device)
child.load_state_dict(torch.load(path))
child.eval()
offspring.append(child)
offspring_optimizer.append(optim.Adam(child.parameters(), lr=lr, betas=(0.5, 0.999)))
print("2-mutation...")
if i == 0:
g_loss.append(g_BCE_function(fake_outputs))
elif i == 1:
g_loss.append(g_MAE_function(fake_outputs))
else:
g_loss.append(g_MSE_function(fake_outputs))
offspring_optimizer[i].zero_grad()
g_loss[i].backward(retain_graph=True)
offspring_optimizer[i].step()
# Compute offspring fitness
fd,fq = fitnessScore(offspring[i],D,test_loader)
offspring_fitness.append(fd + 0.1 * fq)
print("3-survivor selection")
# Survivor Selection
G = G + offspring
g_optimizer = g_optimizer + offspring_optimizer
fitness_Lst = []
for i in range(len(G)):
fd,fq = fitnessScore(G[i],D,test_loader)
fitness_Lst.append(fd + 0.1 * fq)
for i in range(population_size):
x = fitness_Lst.index(min(fitness_Lst))
del G[x]
del g_optimizer[x]
del fitness_Lst[x]
print(fitness_Lst)
x = fitness_Lst.index(max(fitness_Lst))
fq,fd = fitnessScore(G[x],D,test_loader)
if times % 2 == 0 or times == len(train_loader):
g = g_BCE_function(fake_outputs)
print('[{}/{}, {}/{}] D_loss: {:.3f} G_loss: {:.3f}'.format(epoch, epochs, times, len(train_loader), d_loss.item(), g.item()))
print("highest fitness: ", max(fitness_Lst))
if epoch % 10 == 0:
imgs_numpy = (fake_inputs.data.cpu().numpy()+1.0)/2.0
show_images(imgs_numpy[:16])
plt.show()
print('Training Finished.')
print('Cost Time: {}s'.format(time.time()-start_time))