-
Notifications
You must be signed in to change notification settings - Fork 1
/
sganTorch.py
292 lines (214 loc) · 8.41 KB
/
sganTorch.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
import torch
import torch.nn as nn
from torch import optim
from skimage import io
import numpy
import LoadData
fake_label = 0
real_label = 1
cuda = True
datasetVersion = 'dataset'
class Config():
def __init__(self, batchSize):
self.epochs = 50000
self.batchSize = batchSize
self.nc = 3
self.zx = 9
self.zxSample = 20
self.nz = 100
self.l2Fac = 1e-5 # L2 reg
self.genLayers = 5
self.disLayers = self.genLayers
self.genKernelSize = [(5, 5)] * self.genLayers
self.genKernelSize = self.genKernelSize[::-1]
self.disKernelSize = self.genKernelSize
self.genOutChannels = [self.nc] + [2 ** (n + 6) for n in range(self.genLayers - 1)]
self.genOutChannels = self.genOutChannels[::-1]
self.genInChannels = [self.nz] + self.genOutChannels[:-1]
self.genPadding = 2
self.disOutChannels = [2 ** (n + 6) for n in range(self.disLayers - 1)] + [1] # TODO check 1 or 2 output channels for final layer
self.disInChannels = [self.nc] + self.disOutChannels[:-1]
self.disPadding = 2
self.genStride = [(2, 2)] * self.genLayers # Dimensions: NC * W * H input, then genOutChannels[i] * (W * 2 ^ (i + 1)) * (H * 2 ^ (i + 1)), last is npx = zx * 32
self.disStride = [(2, 2)] * self.disLayers # Dimensions last layer is Wgen / 2**5 = Wgen / 32 = npx
self.lr = 0.0005
self.b1 = 0.5
self.l2_fac = 1e-5
self.Dupdates = 1
self.npx = zxToNpx(self.zx, self.genLayers)
def zxToNpx(zx, depth):
return (zx - 1) * 2 ** depth + 1
class NetG(nn.Module):
def __init__(self, config):
super(NetG, self).__init__()
self.layers = torch.nn.ModuleList()
# Transposed Convolution
# outChannels - num_filters - gen_fn
# kernelSize - filter_size - gen_ks
# stride - (2, 2)
# Batchnorm
print(config.zx)
for l in range(config.genLayers - 1):
tconv = torch.nn.ConvTranspose2d(
config.genInChannels[l],
config.genOutChannels[l],
config.genKernelSize[l],
stride = config.genStride[l],
padding=config.genPadding)
activation = torch.nn.ReLU()
bnorm = torch.nn.BatchNorm2d(
config.genOutChannels[l])
self.layers.append(tconv)
self.layers.append(bnorm)
self.layers.append(activation)
tconv = torch.nn.ConvTranspose2d(
config.genInChannels[-1],
config.genOutChannels[-1],
config.genKernelSize[-1],
stride = config.genStride[-1],
padding=config.genPadding)
activation = torch.nn.Tanh()
self.layers.append(tconv)
self.layers.append(activation)
def forward(self, x):
out = self.layers[0](x)
for i, l in enumerate(self.layers[1:]):
out = l(out)
return out
class NetD(nn.Module):
def __init__(self, config):
super(NetD, self).__init__()
self.layers = torch.nn.ModuleList()
first = torch.nn.Conv2d(
config.disInChannels[0],
config.disOutChannels[0],
config.disKernelSize[0],
stride = config.disStride[0],
padding = config.disPadding)
firstActivation = torch.nn.LeakyReLU(negative_slope=0.2)
self.layers.append(first)
self.layers.append(firstActivation)
for l in range(1, config.disLayers - 1):
conv = torch.nn.Conv2d(
config.disInChannels[l],
config.disOutChannels[l],
config.disKernelSize[l],
stride = config.disStride[l],
padding = config.disPadding)
activation = torch.nn.LeakyReLU(negative_slope = 0.2)
bnorm = torch.nn.BatchNorm2d(config.disOutChannels[l])
self.layers.append(conv)
self.layers.append(bnorm)
self.layers.append(activation)
last = torch.nn.Conv2d(
config.disInChannels[-1],
config.disOutChannels[-1],
config.disKernelSize[-1],
stride = config.disStride[-1],
padding = config.disPadding)
lastActivation = torch.nn.Sigmoid()
self.layers.append(last)
self.layers.append(lastActivation)
def forward(self, x):
out = self.layers[0](x)
for l in self.layers[1:]:
out = l(out)
return out
def lossD(predReal, predFake):
shape = predReal.shape
temp = predFake[:predReal.shape[0], :, :, :] # This is to accomodate partial batches
ones = torch.ones(shape)
if cuda:
ones = ones.cuda()
res = -torch.mean(torch.log(ones - temp)) - torch.mean(torch.log(predReal))
return res
def lossG(predFake):
shape = predFake.shape
res = -torch.mean(torch.log(predFake))
return res
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
nn.init.normal_(m.weight.data, 0.0, 0.02)
elif classname.find('BatchNorm') != -1:
nn.init.normal_(m.weight.data, 1.0, 0.02)
nn.init.constant_(m.bias.data, 0)
def main():
config = Config(6)
if cuda:
torch.backends.cudnn.deterministic=True
gen = NetG(config)
if cuda:
gen = gen.cuda()
dis = NetD(config)
if cuda:
dis = dis.cuda()
weights_init(gen)
weights_init(dis)
print(gen)
print(dis)
trainset = LoadData.loadDataset(datasetVersion, config.npx)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=config.batchSize, shuffle=True, num_workers=2)
optimizerG = optim.Adam(gen.parameters(), lr=config.lr, betas=(config.b1, 0.999))#, weight_decay=config.l2Fac)
optimizerD = optim.Adam(dis.parameters(), lr=config.lr, betas=(config.b1, 0.999))#, weight_decay=config.l2Fac)
# Fixed noise used to generate the samples, slightly larger than the one used for training
# A larger sample helps get a sense of the final large scale results
Zsample = torch.FloatTensor(1, config.nz, config.zxSample, config.zxSample).uniform_(-1, 1)
if cuda:
Zsample = Zsample.cuda()
for epoch in range(config.epochs):
errG = []
errD = []
it = 0
for i, data in enumerate(trainloader):
if it % (config.Dupdates + 1) == 0:
# Training the generator
gen.zero_grad()
# Generate the noise
Z = torch.FloatTensor(config.batchSize, config.nz, config.zx, config.zx).uniform_(-1, 1)
if cuda:
Z = Z.cuda()
genX = gen(Z)
dFake = dis(genX)
# Compute the loss on fake
og = lossG(dFake)
og.backward()
optimizerG.step()
errG.append(og.item())
else:
# Training the discriminator
dis.zero_grad()
inputs, labels = data
inputs = inputs.float()
if cuda:
inputs = inputs.cuda()
Z = torch.FloatTensor(config.batchSize, config.nz, config.zx, config.zx).uniform_(-1, 1)
if cuda:
Z = Z.cuda()
X = inputs
genX = gen(Z)
# Compute real and fake labels
dFake = dis(genX)
dReal = dis(X)
# Compute the loss
od = lossD(dReal, dFake)
od.backward()
optimizerD.step()
errD.append(od.item())
it += 1
print(f"Epoch {epoch} Losses: G = {numpy.mean(errG)} D = {numpy.mean(errD)}")
# Save a smallish sample
if epoch % 5 == 0:
with torch.no_grad():
fake = gen(Zsample).detach().cpu().numpy()[0, :, :, :]
im = numpy.zeros((fake.shape[1], fake.shape[2], fake.shape[0]))
im[:, :, 0] = fake[0, :, :]
im[:, :, 1] = fake[1, :, :]
im[:, :, 2] = fake[2, :, :]
io.imsave(f"samples/torch_sample_{datasetVersion}_{epoch}.png", im)
# Save the model
if epoch % 50 == 0:
torch.save(gen.state_dict(), f"models/gen_{epoch}.pth")
torch.save(dis.state_dict(), f"models/dis_{epoch}.pth")
if __name__ == "__main__":
main()