forked from Hydrino/ACGAN_cifar10
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
168 lines (105 loc) · 5 KB
/
main.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
import torch
import torch.nn as nn
from torchvision.utils import make_grid
import torch.optim as optim
import torchvision
import torch.nn.functional as F
import torchvision.transforms as transforms
from model import Generator, Discriminator
from utils import showImage, weights_init, check_folders
check_folders()
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(device)
tf = transforms.Compose([transforms.Resize(64),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
trainset = torchvision.datasets.CIFAR10(root = './data', train = True, download = True,
transform = tf)
#testset = torchvision.datasets.CIFAR10(root = './data', train = False, download = True,
# transform = tf)
#dataset = torch.utils.data.ConcatDataset([trainset, testset])
trainloader = torch.utils.data.DataLoader(trainset, batch_size = 128, shuffle = True)
#print(len(dataset))
#print(dataset[0][0].size())
classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck', 'fake')
dataiter = iter(trainloader)
images,labels = dataiter.next()
print(images.size())
showImage(make_grid(images[0:64]))
# custom weights initialization called on netG and netD
gen = Generator().to(device)
gen.apply(weights_init)
disc = Discriminator().to(device)
disc.apply(weights_init)
paramsG = list(gen.parameters())
print(len(paramsG))
paramsD = list(disc.parameters())
print(len(paramsD))
optimG = optim.Adam(gen.parameters(), 0.0002, betas = (0.5,0.999))
optimD = optim.Adam(disc.parameters(), 0.0002, betas = (0.5,0.999))
validity_loss = nn.BCELoss()
real_labels = 0.7 + 0.5 * torch.rand(10, device = device)
fake_labels = 0.3 * torch.rand(10, device = device)
epochs = 400
for epoch in range(1,epochs+1):
for idx, (images,labels) in enumerate(trainloader):
batch_size = images.size(0)
labels= labels.to(device)
images = images.to(device)
real_label = real_labels[idx % 10]
fake_label = fake_labels[idx % 10]
fake_class_labels = 10*torch.ones((batch_size,),dtype = torch.long,device = device)
if idx % 25 == 0:
real_label, fake_label = fake_label, real_label
# ---------------------
# disc
# ---------------------
optimD.zero_grad()
# real
validity_label = torch.full((batch_size,),real_label , device = device)
pvalidity, plabels = disc(images)
errD_real_val = validity_loss(pvalidity, validity_label)
errD_real_label = F.nll_loss(plabels,labels)
errD_real = errD_real_val + errD_real_label
errD_real.backward()
D_x = pvalidity.mean().item()
#fake
noise = torch.randn(batch_size,100,device = device)
sample_labels = torch.randint(0,10,(batch_size,),device = device, dtype = torch.long)
fakes = gen(noise,sample_labels)
validity_label.fill_(fake_label)
pvalidity, plabels = disc(fakes.detach())
errD_fake_val = validity_loss(pvalidity, validity_label)
errD_fake_label = F.nll_loss(plabels, fake_class_labels)
errD_fake = errD_fake_val + errD_fake_label
errD_fake.backward()
D_G_z1 = pvalidity.mean().item()
#finally update the params!
errD = errD_real + errD_fake
optimD.step()
# ------------------------
# gen
# ------------------------
optimG.zero_grad()
noise = torch.randn(batch_size,100,device = device)
sample_labels = torch.randint(0,10,(batch_size,),device = device, dtype = torch.long)
validity_label.fill_(1)
fakes = gen(noise,sample_labels)
pvalidity,plabels = disc(fakes)
errG_val = validity_loss(pvalidity, validity_label)
errG_label = F.nll_loss(plabels, sample_labels)
errG = errG_val + errG_label
errG.backward()
D_G_z2 = pvalidity.mean().item()
optimG.step()
if idx%20==0:
print("[{}/{}] [{}/{}] D_x: [{:.4f}] D_G: [{:.4f}/{:.4f}] G_loss: [{:.4f}] D_loss: [{:.4f}] D_label: [{:.4f}] "
.format(epoch,epochs, idx, len(trainloader),D_x, D_G_z1,D_G_z2,errG,errD,
errD_real_label + errD_fake_label + errG_label))
noise = torch.randn(10,100,device = device)
labels = torch.arange(0,10,dtype = torch.long,device = device)
gen_images = gen(noise,labels).detach()
showImage(make_grid(gen_images), epoch)
torch.save(gen.state_dict(),'checkpoints/gen_%i.pth'%epoch)
torch.save(disc.state_dict(),'checkpoints/disc%i.pth'%epoch)