-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_gan.py
135 lines (106 loc) · 5.49 KB
/
main_gan.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
# import sys
import os
import time
import torch
import torch.backends.cudnn as cudnn
import torch.optim as optim
from torchvision import transforms
import models
import arguments
import logger
import utils
def main(args):
utils.seedme(args.seed)
cudnn.benchmark = True
os.system('mkdir -p {}'.format(args.outf))
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print "Using BCE loss: {}".format(not args.no_bce)
images_train, images_test, masks_train, masks_test = utils.load_seismic_data(args.root_dir, test_size=.2, random_state=args.seed)
images_train, masks_train = utils.concatenate_hflips(images_train, masks_train, shuffle=True, random_state=args.seed)
images_test, masks_test = utils.concatenate_hflips(images_test, masks_test, shuffle=True, random_state=args.seed)
# transform = transforms.Compose([utils.augment(), utils.ToTensor()])
transform = transforms.Compose([utils.ToTensor()])
dataset_train = utils.SegmentationDataset(images_train, masks_train, transform=transform)
dataloader = torch.utils.data.DataLoader(dataset_train, batch_size=args.batch_size, shuffle=True, drop_last=True, num_workers=1)
dataiter = utils.dataiterator(dataloader)
netF = models.choiceF[args.archF](num_features=args.num_features_F, num_residuals=args.num_residuals, gated=args.gated, gate_param=args.gate_param).to(device)
netD = models.choiceD[args.archD](num_features=args.num_features_D, nc=2, dropout=args.dropout).to(device)
if args.netF:
netF.load_state_dict(torch.load(args.netF))
if args.netD:
netD.load_state_dict(torch.load(args.netD))
print netF
print netD
optimizerF = optim.Adam(netF.parameters(), betas=(0.5, 0.999), lr=args.lr, amsgrad=True)
optimizerD = optim.Adam(netD.parameters(), betas=(0.5, 0.999), lr=args.lr, amsgrad=True)
alpha = torch.tensor(args.alpha).to(device)
loss_func = torch.nn.BCELoss()
smooth_binary = utils.SmoothBinary(scale=args.smooth_noise)
# images_test, masks_test = torch.from_numpy(images_test).to(device), torch.from_numpy(masks_test).to(device)
log = logger.LoggerGAN(args.outf, netF, netD, torch.from_numpy(images_train), torch.from_numpy(masks_train), torch.from_numpy(images_test), torch.from_numpy(masks_test), bcefunc=loss_func, device=device)
start_time = time.time()
for i in range(args.niter):
# --- train D
for p in netD.parameters():
p.requires_grad_(True)
for _ in range(args.niterD):
optimizerD.zero_grad()
images_real, masks_real = next(dataiter)
images_real, masks_real = images_real.to(device), masks_real.to(device)
masks_fake = netF(images_real).detach()
x_fake = torch.cat((images_real, masks_fake), dim=1)
# images_real, masks_real = next(dataiter)
# images_real, masks_real = images_real.to(device), masks_real.to(device)
masks_real = smooth_binary(masks_real)
x_real = torch.cat((images_real, masks_real), dim=1)
x_real.requires_grad_() # to compute gradD_real
x_fake.requires_grad_() # to compute gradD_fake
y_real = netD(x_real)
y_fake = netD(x_fake)
lossE = y_real.mean() - y_fake.mean()
# grad() does not broadcast so we compute for the sum, effect is the same
gradD_real = torch.autograd.grad(y_real.sum(), x_real, create_graph=True)[0]
gradD_fake = torch.autograd.grad(y_fake.sum(), x_fake, create_graph=True)[0]
omega = 0.5*(gradD_real.view(gradD_real.size(0), -1).pow(2).sum(dim=1).mean() +
gradD_fake.view(gradD_fake.size(0), -1).pow(2).sum(dim=1).mean())
loss = -lossE - alpha*(1.0 - omega) + 0.5*args.rho*(1.0 - omega).pow(2)
loss.backward()
optimizerD.step()
alpha -= args.rho*(1.0 - omega.item())
# --- train G
for p in netD.parameters():
p.requires_grad_(False)
optimizerF.zero_grad()
images_real, masks_real = next(dataiter)
images_real, masks_real = images_real.to(device), masks_real.to(device)
masks_fake = netF(images_real)
x_fake = torch.cat((images_real, masks_fake), dim=1)
y_fake = netD(x_fake)
loss = -y_fake.mean()
bceloss = loss_func(masks_fake, masks_real)
if not args.no_bce:
loss = loss + bceloss * args.bce_weight
loss.backward()
optimizerF.step()
log.dump(i+1, lossE.item(), alpha.item(), omega.item())
if (i+1) % args.nprint == 0:
print 'Time per loop: {} sec/loop'.format((time.time() - start_time)/args.nprint)
print "[{}/{}] lossE: {:.3f}, bceloss: {:.3f}, alpha: {:.3f}, omega: {:.3f}".format((i+1), args.niter, lossE.item(), bceloss.item(), alpha.item(), omega.item())
log.flush(i+1)
# if (i+1) > 5000:
torch.save(netF.state_dict(), '{}/netF_iter_{}.pth'.format(args.outf, i+1))
torch.save(netD.state_dict(), '{}/netD_iter_{}.pth'.format(args.outf, i+1))
start_time = time.time()
if __name__ == '__main__':
parser = arguments.BaseParser()
parser.add_argument('--outf', default='tmp/gan')
args = parser.parse_args()
if args.quick_test:
print "Running quick test..."
args.outf = '{}/tmp'.format(args.outf)
args.niter = 30
args.nprint = 10
args.batch_size = 8
args.num_features_D = 2
args.num_features_F = 2
main(args)