-
Notifications
You must be signed in to change notification settings - Fork 47
/
run_joint_confidence.py
201 lines (173 loc) · 7.61 KB
/
run_joint_confidence.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
##############################################
# This code is based on samples from pytorch #
##############################################
# Writer: Kimin Lee
from __future__ import print_function
import argparse
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import data_loader
import numpy as np
import torchvision.utils as vutils
import models
from torchvision import datasets, transforms
from torch.autograd import Variable
# Training settings
parser = argparse.ArgumentParser(description='Training code - joint confidence')
parser.add_argument('--batch-size', type=int, default=128, help='input batch size for training')
parser.add_argument('--epochs', type=int, default=100, help='number of epochs to train')
parser.add_argument('--lr', type=float, default=0.0002, help='learning rate')
parser.add_argument('--no-cuda', action='store_true', default=False, help='disables CUDA training')
parser.add_argument('--seed', type=int, default=1, help='random seed')
parser.add_argument('--log-interval', type=int, default=100, help='how many batches to wait before logging training status')
parser.add_argument('--dataset', default='svhn', help='cifar10 | svhn')
parser.add_argument('--dataroot', required=True, help='path to dataset')
parser.add_argument('--imageSize', type=int, default=32, help='the height / width of the input image to network')
parser.add_argument('--outf', default='.', help='folder to output images and model checkpoints')
parser.add_argument('--wd', type=float, default=0.0, help='weight decay')
parser.add_argument('--droprate', type=float, default=0.1, help='learning rate decay')
parser.add_argument('--decreasing_lr', default='60', help='decreasing strategy')
parser.add_argument('--num_classes', type=int, default=10, help='the # of classes')
parser.add_argument('--beta', type=float, default=1, help='penalty parameter for KL term')
args = parser.parse_args()
if args.dataset == 'cifar10':
args.beta = 0.1
args.batch_size = 64
print(args)
args.cuda = not args.no_cuda and torch.cuda.is_available()
print("Random Seed: ", args.seed)
torch.manual_seed(args.seed)
if args.cuda:
torch.cuda.manual_seed(args.seed)
kwargs = {'num_workers': 1, 'pin_memory': True} if args.cuda else {}
print('load data: ',args.dataset)
train_loader, test_loader = data_loader.getTargetDataSet(args.dataset, args.batch_size, args.imageSize, args.dataroot)
print('Load model')
model = models.vgg13()
print(model)
print('load GAN')
nz = 100
netG = models.Generator(1, nz, 64, 3) # ngpu, nz, ngf, nc
netD = models.Discriminator(1, 3, 64) # ngpu, nc, ndf
# Initial setup for GAN
real_label = 1
fake_label = 0
criterion = nn.BCELoss()
fixed_noise = torch.FloatTensor(64, nz, 1, 1).normal_(0, 1)
if args.cuda:
model.cuda()
netD.cuda()
netG.cuda()
criterion.cuda()
fixed_noise = fixed_noise.cuda()
fixed_noise = Variable(fixed_noise)
print('Setup optimizer')
optimizer = optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.wd)
optimizerD = optim.Adam(netD.parameters(), lr=args.lr, betas=(0.5, 0.999))
optimizerG = optim.Adam(netG.parameters(), lr=args.lr, betas=(0.5, 0.999))
decreasing_lr = list(map(int, args.decreasing_lr.split(',')))
def train(epoch):
model.train()
for batch_idx, (data, target) in enumerate(train_loader):
gan_target = torch.FloatTensor(target.size()).fill_(0)
uniform_dist = torch.Tensor(data.size(0), args.num_classes).fill_((1./args.num_classes))
if args.cuda:
data, target = data.cuda(), target.cuda()
gan_target, uniform_dist = gan_target.cuda(), uniform_dist.cuda()
data, target, uniform_dist = Variable(data), Variable(target), Variable(uniform_dist)
###########################
# (1) Update D network #
###########################
# train with real
gan_target.fill_(real_label)
targetv = Variable(gan_target)
optimizerD.zero_grad()
output = netD(data)
errD_real = criterion(output, targetv)
errD_real.backward()
D_x = output.data.mean()
# train with fake
noise = torch.FloatTensor(data.size(0), nz, 1, 1).normal_(0, 1).cuda()
if args.cuda:
noise = noise.cuda()
noise = Variable(noise)
fake = netG(noise)
targetv = Variable(gan_target.fill_(fake_label))
output = netD(fake.detach())
errD_fake = criterion(output, targetv)
errD_fake.backward()
D_G_z1 = output.data.mean()
errD = errD_real + errD_fake
optimizerD.step()
###########################
# (2) Update G network #
###########################
optimizerG.zero_grad()
# Original GAN loss
targetv = Variable(gan_target.fill_(real_label))
output = netD(fake)
errG = criterion(output, targetv)
D_G_z2 = output.data.mean()
# minimize the true distribution
KL_fake_output = F.log_softmax(model(fake))
errG_KL = F.kl_div(KL_fake_output, uniform_dist)*args.num_classes
generator_loss = errG + args.beta*errG_KL
generator_loss.backward()
optimizerG.step()
###########################
# (3) Update classifier #
###########################
# cross entropy loss
optimizer.zero_grad()
output = F.log_softmax(model(data))
loss = F.nll_loss(output, target)
# KL divergence
noise = torch.FloatTensor(data.size(0), nz, 1, 1).normal_(0, 1).cuda()
if args.cuda:
noise = noise.cuda()
noise = Variable(noise)
fake = netG(noise)
KL_fake_output = F.log_softmax(model(fake))
KL_loss_fake = F.kl_div(KL_fake_output, uniform_dist)*args.num_classes
total_loss = loss + args.beta*KL_loss_fake
total_loss.backward()
optimizer.step()
if batch_idx % args.log_interval == 0:
print('Classification Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}, KL fake Loss: {:.6f}'.format(
epoch, batch_idx * len(data), len(train_loader.dataset),
100. * batch_idx / len(train_loader), loss.data.item(), KL_loss_fake.data.item()))
fake = netG(fixed_noise)
vutils.save_image(fake.data, '%s/gan_samples_epoch_%03d.png'%(args.outf, epoch), normalize=True)
def test(epoch):
model.eval()
test_loss = 0
correct = 0
total = 0
for data, target in test_loader:
total += data.size(0)
if args.cuda:
data, target = data.cuda(), target.cuda()
data, target = Variable(data, volatile=True), Variable(target)
output = F.log_softmax(model(data))
test_loss += F.nll_loss(output, target).data.item()
pred = output.data.max(1)[1] # get the index of the max log-probability
correct += pred.eq(target.data).cpu().sum()
test_loss = test_loss
test_loss /= len(test_loader) # loss function already averages over batch size
print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
test_loss, correct, total,
100. * correct / total))
for epoch in range(1, args.epochs + 1):
train(epoch)
test(epoch)
if epoch in decreasing_lr:
optimizerG.param_groups[0]['lr'] *= args.droprate
optimizerD.param_groups[0]['lr'] *= args.droprate
optimizer.param_groups[0]['lr'] *= args.droprate
if epoch % 20 == 0:
# do checkpointing
torch.save(netG.state_dict(), '%s/netG_epoch_%d.pth' % (args.outf, epoch))
torch.save(netD.state_dict(), '%s/netD_epoch_%d.pth' % (args.outf, epoch))
torch.save(model.state_dict(), '%s/model_epoch_%d.pth' % (args.outf, epoch))