-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprojection.py
209 lines (140 loc) · 5.91 KB
/
projection.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
import os
import numpy as np
import torch
from torch import optim
from torch import nn
from torch.autograd import Variable
from torch.utils.data import Dataset, DataLoader
from torchvision import datasets as dset
import torchvision.transforms as transforms
from torchvision.utils import save_image
from layers.SNConv2d import SNConv2d
from layers.SNLinear import SNLinear
import datasets
import gan
from logger import Logger
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
m.weight.data.normal_(0.0, 0.02)
elif classname.find('BatchNorm') != -1:
m.weight.data.normal_(1.0, 0.02)
m.bias.data.fill_(0)
class mnistnet_D(nn.Module):
def __init__(self, nc=1, ndf=64, BN=True, bias=True, n_classes=10):
super(mnistnet_D,self).__init__()
self.n_classes=n_classes
self.layer1 = nn.Sequential(nn.Conv2d(nc,ndf,kernel_size=4,stride=2,padding=1, bias=bias),
nn.BatchNorm2d(ndf),
nn.LeakyReLU(0.2,inplace=True))
# 16 x 16
self.layer2 = nn.Sequential(nn.Conv2d(ndf,ndf*2,kernel_size=4,stride=2,padding=1, bias=bias),
nn.BatchNorm2d(ndf*2),
nn.LeakyReLU(0.2,inplace=True))
# 8 x 8
self.layer3 = nn.Sequential(nn.Conv2d(ndf*2,ndf*4,kernel_size=4,stride=2,padding=1, bias=bias),
nn.BatchNorm2d(ndf*4),
nn.LeakyReLU(0.2,inplace=True))
# 4 x 4
self.layer4 = nn.Sequential(nn.Conv2d(ndf*4,1,kernel_size=4,stride=1,padding=0, bias=bias))#,
# nn.Sigmoid())
self.embedding = nn.Linear(n_classes, ndf*4, bias=False)
# self.linear = SNLinear(ndf*4, 1)
def forward(self,x, y):
# x, y = input
h = self.layer1(x)
h = self.layer2(h)
h = self.layer3(h)
h = torch.sum(h, dim=2).sum(dim=2) # Global pooling
# output = self.linear(h)
th = torch.cuda if h.is_cuda else torch
y_onehot = th.FloatTensor(y.size()[0], self.n_classes)
y_onehot.zero_()
y_onehot.scatter_(1, y.data.view(-1,1), 1)
y_onehot = Variable(y_onehot)
w_y = self.embedding(y_onehot.float())
output = torch.sum(w_y * h, dim=1).view(-1, 1)
# out = self.layer4(out)
return output.view(-1)
class mnistnet_G(nn.Module):
def __init__(self, nc=1, ngf=64, nz=100, bias=False): # 256 ok
super(mnistnet_G,self).__init__()
self.layer1 = nn.Sequential(nn.ConvTranspose2d(nz,ngf*4,kernel_size=4,bias=bias),
nn.BatchNorm2d(ngf*4),
nn.ReLU())
# 4 x 4
self.layer2 = nn.Sequential(nn.ConvTranspose2d(ngf*4,ngf*2,kernel_size=4,stride=2,padding=1,bias=bias),
nn.BatchNorm2d(ngf*2),
nn.ReLU())
# 8 x 8
self.layer3 = nn.Sequential(nn.ConvTranspose2d(ngf*2,ngf,kernel_size=4,stride=2,padding=1,bias=bias),
nn.BatchNorm2d(ngf),
nn.ReLU())
# 16 x 16
self.layer4 = nn.Sequential(nn.ConvTranspose2d(ngf,nc,kernel_size=4,stride=2,padding=1,bias=bias),
# nn.Sigmoid())
nn.Tanh())
self.apply(weights_init)
def forward(self,x):
out = self.layer1(x)
out = self.layer2(out)
out = self.layer3(out)
out = self.layer4(out)
return out
opt = gan.Options()
opt.cuda = True
opt.path = 'projection/'
opt.num_iter = 100000
opt.batch_size = 64
opt.visualize_nth = 2000
opt.conditionalD = False
opt.conditional = True
opt.wgangp_lambda = 10.0
opt.n_classes = 10
opt.nz = (100,1,1)
opt.num_disc_iters = 1
opt.checkpoints = [1000, 2000, 5000, 10000, 20000, 40000, 60000, 100000, 200000, 300000, 500000]
log = Logger(base_dir=opt.path, tag='multiGAN')
data = datasets.labeledMNISTDataset()
mydataloader = datasets.MyDataLoader()
data_iter = mydataloader.return_iterator(DataLoader(data, batch_size=opt.batch_size, shuffle=True, num_workers=4), is_cuda=opt.cuda, conditional=opt.conditional, pictures=True)
# netG = mnistnet.Generator(nz=100, BN=True)
# netD = mnistnet.Discriminator(nc=1, BN=True)
netG = mnistnet_G(nc=1,nz=110)
netD = mnistnet_D(nc=1,BN=True)
optimizerD = optim.Adam(netD.parameters(), lr=2e-4, betas=(.5, .999))
optimizerG = optim.Adam(netG.parameters(), lr=2e-4, betas=(.5, .999))
def save_samples(gan, i_iter):
gan.netG.eval()
if 'noise' not in save_samples.__dict__:
save_samples.noise = Variable(gan.gen_latent_noise(100, opt.nz))
if not os.path.exists(opt.path + 'tmp/'):
os.makedirs(opt.path + 'tmp/')
y = np.repeat(np.arange(10), 10)
y = torch.autograd.Variable(torch.from_numpy(y))
if gan.opt.cuda:
y = y.cuda()
noise = save_samples.noise
noise = gan.join_xy((noise, y))
fake = netG(noise)
fake = fake.view(-1, 1, 32, 32)
fake_01 = (fake.data.cpu() + 1.0) * 0.5
# print(fake_01.min(), fake_01.max())
save_image(fake_01, opt.path + 'tmp/' + '{:0>5}.jpeg'.format(i_iter), nrow=10)
# alkjfd
gan.netG.train()
def callback(gan, i_iter):
# if i_iter % 5000 == 0:
# save_inception_score(gan, i_iter)
if i_iter % 50 == 0:
torch.save(netD.embedding.state_dict(), opt.path + 'emb{}.pth'.format(i_iter))
# torch.save(netD.embedding2.state_dict(), opt.path + '2emb{}.pth'.format(i_iter))
if i_iter % 50 == 0:
save_samples(gan, i_iter)
if i_iter % 50 == 0:
log.save()
gan1 = gan.GAN(netG=netG, netD=netD, optimizerD=optimizerD, optimizerG=optimizerG, opt=opt)
gan1.train(data_iter, opt, logger=log, callback=callback)
torch.save(netG.state_dict(), opt.path + 'gen.pth')
torch.save(netD.state_dict(), opt.path + 'disc.pth')
log.close()