-
Notifications
You must be signed in to change notification settings - Fork 57
/
train.py
221 lines (196 loc) · 7.6 KB
/
train.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
210
211
212
213
214
215
216
217
218
219
220
221
#CUDA_VISIBLE_DEVICES=X python train.py --cuda --outpath ./outputs
from __future__ import print_function
import argparse
import os
import numpy as np
from PIL import Image
import torch
from torch.utils import data
import torch.backends.cudnn as cudnn
import torch.optim as optim
from torch import nn
import torchvision.utils as vutils
from torch.autograd import Variable
import torch.nn.functional as F
from net import NetS, NetC
from LoadData import Dataset, loader, Dataset_val
# Training settings
parser = argparse.ArgumentParser(description='Example')
parser.add_argument('--batchSize', type=int, default=36, help='training batch size')
parser.add_argument('--niter', type=int, default=10000, help='number of epochs to train for')
parser.add_argument('--lr', type=float, default=0.0002, help='Learning Rate. Default=0.02')
parser.add_argument('--ngpu', type=int, default=1, help='number of GPUs to use, for now it only supports one GPU')
parser.add_argument('--beta1', type=float, default=0.5, help='beta1 for adam. default=0.5')
parser.add_argument('--decay', type=float, default=0.5, help='Learning rate decay. default=0.5')
parser.add_argument('--cuda', action='store_true', help='using GPU or not')
parser.add_argument('--seed', type=int, default=666, help='random seed to use. Default=1111')
parser.add_argument('--outpath', default='./outputs', help='folder to output images and model checkpoints')
opt = parser.parse_args()
print(opt)
try:
os.makedirs(opt.outpath)
except OSError:
pass
# custom weights initialization called on NetS and NetC
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)
def dice_loss(input,target):
num=input*target
num=torch.sum(num,dim=2)
num=torch.sum(num,dim=2)
den1=input*input
den1=torch.sum(den1,dim=2)
den1=torch.sum(den1,dim=2)
den2=target*target
den2=torch.sum(den2,dim=2)
den2=torch.sum(den2,dim=2)
dice=2*(num/(den1+den2))
dice_total=1-1*torch.sum(dice)/dice.size(0)#divide by batchsize
return dice_total
cuda = opt.cuda
if cuda and not torch.cuda.is_available():
raise Exception("No GPU found, please run without --cuda")
torch.manual_seed(opt.seed)
if cuda:
torch.cuda.manual_seed(opt.seed)
cudnn.benchmark = True
print('===> Building model')
NetS = NetS(ngpu = opt.ngpu)
# NetS.apply(weights_init)
print(NetS)
NetC = NetC(ngpu = opt.ngpu)
# NetC.apply(weights_init)
print(NetC)
if cuda:
NetS = NetS.cuda()
NetC = NetC.cuda()
# criterion = criterion.cuda()
# setup optimizer
lr = opt.lr
decay = opt.decay
optimizerG = optim.Adam(NetS.parameters(), lr=lr, betas=(opt.beta1, 0.999))
optimizerD = optim.Adam(NetC.parameters(), lr=lr, betas=(opt.beta1, 0.999))
# load training data
dataloader = loader(Dataset('./'),opt.batchSize)
# load testing data
dataloader_val = loader(Dataset_val('./'), 36)
max_iou = 0
NetS.train()
for epoch in range(opt.niter):
for i, data in enumerate(dataloader, 1):
#train C
NetC.zero_grad()
input, label = Variable(data[0]), Variable(data[1])
if cuda:
input = input.cuda()
target = label.cuda()
target = target.type(torch.FloatTensor)
target = target.cuda()
output = NetS(input)
#output = F.sigmoid(output*k)
output = F.sigmoid(output)
output = output.detach()
output_masked = input.clone()
input_mask = input.clone()
#detach G from the network
for d in range(3):
output_masked[:,d,:,:] = input_mask[:,d,:,:].unsqueeze(1) * output
if cuda:
output_masked = output_masked.cuda()
result = NetC(output_masked)
target_masked = input.clone()
for d in range(3):
target_masked[:,d,:,:] = input_mask[:,d,:,:].unsqueeze(1) * target
if cuda:
target_masked = target_masked.cuda()
target_D = NetC(target_masked)
loss_D = - torch.mean(torch.abs(result - target_D))
loss_D.backward()
optimizerD.step()
#clip parameters in D
for p in NetC.parameters():
p.data.clamp_(-0.05, 0.05)
#train G
NetS.zero_grad()
output = NetS(input)
output = F.sigmoid(output)
for d in range(3):
output_masked[:,d,:,:] = input_mask[:,d,:,:].unsqueeze(1) * output
if cuda:
output_masked = output_masked.cuda()
result = NetC(output_masked)
for d in range(3):
target_masked[:,d,:,:] = input_mask[:,d,:,:].unsqueeze(1) * target
if cuda:
target_masked = target_masked.cuda()
target_G = NetC(target_masked)
loss_dice = dice_loss(output,target)
loss_G = torch.mean(torch.abs(result - target_G))
loss_G_joint = torch.mean(torch.abs(result - target_G)) + loss_dice
loss_G_joint.backward()
optimizerG.step()
print("===> Epoch[{}]({}/{}): Batch Dice: {:.4f}".format(epoch, i, len(dataloader), 1 - loss_dice.data[0]))
print("===> Epoch[{}]({}/{}): G_Loss: {:.4f}".format(epoch, i, len(dataloader), loss_G.data[0]))
print("===> Epoch[{}]({}/{}): D_Loss: {:.4f}".format(epoch, i, len(dataloader), loss_D.data[0]))
vutils.save_image(data[0],
'%s/input.png' % opt.outpath,
normalize=True)
vutils.save_image(data[1],
'%s/label.png' % opt.outpath,
normalize=True)
vutils.save_image(output.data,
'%s/result.png' % opt.outpath,
normalize=True)
if epoch % 10 == 0:
NetS.eval()
IoUs, dices = [], []
for i, data in enumerate(dataloader_val, 1):
input, gt = Variable(data[0]), Variable(data[1])
if cuda:
input = input.cuda()
gt = gt.cuda()
pred = NetS(input)
pred[pred < 0.5] = 0
pred[pred >= 0.5] = 1
pred = pred.type(torch.LongTensor)
pred_np = pred.data.cpu().numpy()
gt = gt.data.cpu().numpy()
for x in range(input.size()[0]):
IoU = np.sum(pred_np[x][gt[x]==1]) / float(np.sum(pred_np[x]) + np.sum(gt[x]) - np.sum(pred_np[x][gt[x]==1]))
dice = np.sum(pred_np[x][gt[x]==1])*2 / float(np.sum(pred_np[x]) + np.sum(gt[x]))
IoUs.append(IoU)
dices.append(dice)
NetS.train()
IoUs = np.array(IoUs, dtype=np.float64)
dices = np.array(dices, dtype=np.float64)
mIoU = np.mean(IoUs, axis=0)
mdice = np.mean(dices, axis=0)
print('mIoU: {:.4f}'.format(mIoU))
print('Dice: {:.4f}'.format(mdice))
if mIoU > max_iou:
max_iou = mIoU
torch.save(NetS.state_dict(), '%s/NetS_epoch_%d.pth' % (opt.outpath, epoch))
vutils.save_image(data[0],
'%s/input_val.png' % opt.outpath,
normalize=True)
vutils.save_image(data[1],
'%s/label_val.png' % opt.outpath,
normalize=True)
pred = pred.type(torch.FloatTensor)
vutils.save_image(pred.data,
'%s/result_val.png' % opt.outpath,
normalize=True)
if epoch % 25 == 0:
lr = lr*decay
if lr <= 0.00000001:
lr = 0.00000001
print('Learning Rate: {:.6f}'.format(lr))
# print('K: {:.4f}'.format(k))
print('Max mIoU: {:.4f}'.format(max_iou))
optimizerG = optim.Adam(NetS.parameters(), lr=lr, betas=(opt.beta1, 0.999))
optimizerD = optim.Adam(NetC.parameters(), lr=lr, betas=(opt.beta1, 0.999))