-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_one.py
144 lines (104 loc) · 4.88 KB
/
train_one.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
import argparse
import random
import numpy as np
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim as optim
import torch.utils.data
import torchvision.datasets as dset
import torchvision.transforms as transforms
import torch.nn.utils as utils
import torch.nn.functional as F
from torch.autograd import Variable
from utils import random_masking, perturb, KL_div
import model
if __name__=="__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--dataroot', default='./data', help='path to dataset')
parser.add_argument('--workers', type=int, help='number of data loading workers', default=0)
parser.add_argument('--batchSize', type=int, default=64, help='input batch size')
parser.add_argument('--nc', type=int, default=3, help='input image channels')
parser.add_argument('--nz', type=int, default=100, help='size of the latent z vector')
parser.add_argument('--ngf', type=int, default=32, help = 'hidden channel sieze')
parser.add_argument('--niter', type=int, default=200, help='number of epochs to train for')
parser.add_argument('--lr', type=float, default=3e-4, help='learning rate')
parser.add_argument('--beta1', type=float, default=0.9, help='beta1 for adam. default=0.9')
parser.add_argument('--beta', type=float, default=1., help='beta for beta-vae')
parser.add_argument('--ngpu' , type=int, default=1, help='number of GPUs to use')
parser.add_argument('--perturbed', action='store_true', help='Whether to train on perturbed data, used for comparing with likelihood ratio by Ren et al.')
parser.add_argument('--ratio', type=float, default=0.2, help='ratio for perturbation of data, see Ren et al.')
opt = parser.parse_args()
device = 'cuda' if torch.cuda.is_available() else 'cpu'
opt.manualSeed = random.randint(1, 10000) # fix seed
print("Random Seed: ", opt.manualSeed)
random.seed(opt.manualSeed)
torch.manual_seed(opt.manualSeed)
cudnn.benchmark = True
dataset = dset.CIFAR10(root=opt.dataroot, download=True,train = True,
transform=transforms.Compose([
transforms.Resize((opt.imageSize)),
transforms.ToTensor(),
]))
dataloader = torch.utils.data.DataLoader(dataset, batch_size=opt.batchSize,
shuffle=True, num_workers=int(opt.workers))
ngpu = int(opt.ngpu)
nz = int(opt.nz)
ngf = int(opt.ngf)
nc = int(opt.nc)
# custom weights initialization called on netG and netD
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
m.weight.data.normal_(0.0, 0.02)
image_size = [32, 32, 3]
netG = model.Decoder(image_size, nz, 256)
netG.apply(weights_init)
netE = model.Encoder(image_size, nz)
netE.apply(weights_init)
netE.to(device)
netG.to(device)
# setup optimizer
optimizer1 = optim.Adam(netE.parameters(), lr=opt.lr, weight_decay = 3e-5)
optimizer2 = optim.Adam(netG.parameters(), lr=opt.lr, weight_decay = 3e-5)
netE.train()
netG.train()
loss_fn = nn.CrossEntropyLoss(reduction = 'none')
rec_l = []
kl = []
tloss = []
for epoch in range(opt.niter):
for i, (x, _) in enumerate(dataloader):
x = x.to(device)
if opt.perturbed:
x = perturb(x, opt.ratio, device)
x_d = F.interpolate(x, scale_factor=0.25)
x_d = F.interpolate(x_d, scale_factor=4)
# x_d = random_masking(x, 0.5, 4)
b = x.size(0)
target = Variable(x.data.view(-1) * 255).long()
[z,mu,logvar] = netE(x_d.float())
recon = netG(z)
recon = recon.contiguous()
recon = recon.view(-1,256)
recl = loss_fn(recon, target)
recl = torch.sum(recl) / b
kld = KL_div(mu,logvar)
loss = recl + opt.beta*kld.mean()
optimizer1.zero_grad()
optimizer2.zero_grad()
loss.backward()
for group in optimizer1.param_groups: # clip gradients
utils.clip_grad_norm_(group['params'], 100, norm_type=2)
for group in optimizer2.param_groups: # clip gradients
utils.clip_grad_norm_(group['params'], 100, norm_type=2)
optimizer1.step()
optimizer2.step()
rec_l.append(recl.detach().cpu().item())
kl.append(kld.mean().detach().cpu().item())
if not i % 100:
print('epoch:{} recon:{} kl:{}'.format(epoch,np.mean(rec_l),np.mean(kl)
))
torch.save(netG.state_dict(), './saved_models/netG.pth')
torch.save(netE.state_dict(), './saved_models/netE.pth')