-
Notifications
You must be signed in to change notification settings - Fork 0
/
Train.py
222 lines (181 loc) · 8.51 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
222
import torch
from torch.autograd import Variable
import os
import argparse
from datetime import datetime
from lib.networks import PVT_Dense_Decoder
from utils.dataloader import get_loader, test_dataset
from utils.utils import clip_gradient, adjust_lr, AvgMeter
import torch.nn.functional as F
import numpy as np
import logging
import matplotlib.pyplot as plt
def structure_loss(pred, mask):
weit = 1 + 5 * torch.abs(F.avg_pool2d(mask, kernel_size=31, stride=1, padding=15) - mask)
wbce = F.binary_cross_entropy_with_logits(pred, mask, reduce='none')
wbce = (weit * wbce).sum(dim=(2, 3)) / weit.sum(dim=(2, 3))
pred = torch.sigmoid(pred)
inter = ((pred * mask) * weit).sum(dim=(2, 3))
union = ((pred + mask) * weit).sum(dim=(2, 3))
wiou = 1 - (inter + 1) / (union - inter + 1)
return (wbce + wiou).mean()
def test(model, path, dataset):
data_path = os.path.join(path, dataset)
image_root = '{}/images/'.format(data_path)
gt_root = '{}/masks/'.format(data_path)
model.eval()
num1 = len(os.listdir(gt_root))
test_loader = test_dataset(image_root, gt_root, 352)
DSC = 0.0
for i in range(num1):
image, gt, name = test_loader.load_data()
gt = np.asarray(gt, np.float32)
gt /= (gt.max() + 1e-8)
image = image.cuda()
res, res1, res2 = model(image)
# eval Dice
res = F.upsample(res + res1 + res2 , size=gt.shape, mode='bilinear', align_corners=False)
res = res.sigmoid().data.cpu().numpy().squeeze()
res = (res - res.min()) / (res.max() - res.min() + 1e-8)
input = res
target = np.array(gt)
N = gt.shape
smooth = 1
input_flat = np.reshape(input, (-1))
target_flat = np.reshape(target, (-1))
intersection = (input_flat * target_flat)
dice = (2 * intersection.sum() + smooth) / (input.sum() + target.sum() + smooth)
dice = '{:.4f}'.format(dice)
dice = float(dice)
DSC = DSC + dice
return DSC / num1
def train(train_loader, model, optimizer, epoch, test_path):
model.train()
global best
size_rates = [0.75, 1, 1.25]
loss_P3_record = AvgMeter()
for i, pack in enumerate(train_loader, start=1):
for rate in size_rates:
optimizer.zero_grad()
# ---- data prepare ----
images, gts = pack
images = Variable(images).cuda()
gts = Variable(gts).cuda()
# ---- rescale ----
trainsize = int(round(opt.trainsize * rate / 32) * 32)
if rate != 1:
images = F.upsample(images, size=(trainsize, trainsize), mode='bilinear', align_corners=True)
gts = F.upsample(gts, size=(trainsize, trainsize), mode='bilinear', align_corners=True)
# ---- forward ----
P1, P2, P3 = model(images)
# ---- loss function ----
loss_P1 = structure_loss(P1, gts)
loss_P2 = structure_loss(P2, gts)
loss_P3 = structure_loss(P2, gts)
loss = loss_P1 + loss_P2 + loss_P3
# ---- backward ----
loss.backward()
clip_gradient(optimizer, opt.clip)
optimizer.step()
# ---- recording loss ----
if rate == 1:
loss_P3_record.update(loss_P2.data, opt.batchsize)
# ---- train visualization ----
if i % 20 == 0 or i == total_step:
print('{} Epoch [{:03d}/{:03d}], Step [{:04d}/{:04d}], '
' lateral-5: {:0.4f}]'.
format(datetime.now(), epoch, opt.epoch, i, total_step,
loss_P3_record.show()))
# save model
save_path = (opt.train_save)
if not os.path.exists(save_path):
os.makedirs(save_path)
torch.save(model.state_dict(), save_path +str(epoch)+ 'Polyp-Dense.pth')
# choose the best model
global dict_plot
test1path = './dataset/TestDataset/'
if (epoch + 1) % 1 == 0:
for dataset in ['CVC-300', 'CVC-ClinicDB', 'Kvasir', 'CVC-ColonDB', 'ETIS-LaribPolypDB']:
dataset_dice = test(model, test1path, dataset)
logging.info('epoch: {}, dataset: {}, dice: {}'.format(epoch, dataset, dataset_dice))
print(dataset, ': ', dataset_dice)
dict_plot[dataset].append(dataset_dice)
meandice = test(model, test_path, 'test')
dict_plot['test'].append(meandice)
if meandice > best:
best = meandice
torch.save(model.state_dict(), save_path + 'PolypPVT.pth')
torch.save(model.state_dict(), save_path +str(epoch)+ 'PolypPVT-best.pth')
print('##############################################################################best', best)
logging.info('##############################################################################best:{}'.format(best))
def plot_train(dict_plot=None, name = None):
color = ['red', 'lawngreen', 'lime', 'gold', 'm', 'plum', 'blue']
line = ['-', "--"]
for i in range(len(name)):
plt.plot(dict_plot[name[i]], label=name[i], color=color[i], linestyle=line[(i + 1) % 2])
transfuse = {'CVC-300': 0.902, 'CVC-ClinicDB': 0.918, 'Kvasir': 0.918, 'CVC-ColonDB': 0.773,'ETIS-LaribPolypDB': 0.733, 'test':0.83}
plt.axhline(y=transfuse[name[i]], color=color[i], linestyle='-')
plt.xlabel("epoch")
plt.ylabel("dice")
plt.title('Train')
plt.legend()
plt.savefig('eval.png')
# plt.show()
if __name__ == '__main__':
dict_plot = {'CVC-300':[], 'CVC-ClinicDB':[], 'Kvasir':[], 'CVC-ColonDB':[], 'ETIS-LaribPolypDB':[], 'test':[]}
name = ['CVC-300', 'CVC-ClinicDB', 'Kvasir', 'CVC-ColonDB', 'ETIS-LaribPolypDB', 'test']
##################model_name#############################
model_name = 'PVT-Dense-Decoder'
###############################################
parser = argparse.ArgumentParser()
parser.add_argument('--epoch', type=int,
default=100, help='epoch number')
parser.add_argument('--lr', type=float,
default=1e-4, help='learning rate')
parser.add_argument('--optimizer', type=str,
default='AdamW', help='choosing optimizer AdamW or SGD')
parser.add_argument('--augmentation',
default=False, help='choose to do random flip rotation')
parser.add_argument('--batchsize', type=int,
default=16, help='training batch size')
parser.add_argument('--trainsize', type=int,
default=352, help='training dataset size')
parser.add_argument('--clip', type=float,
default=0.5, help='gradient clipping margin')
parser.add_argument('--decay_rate', type=float,
default=0.1, help='decay rate of learning rate')
parser.add_argument('--decay_epoch', type=int,
default=50, help='every n epochs decay learning rate')
parser.add_argument('--train_path', type=str,
default='./dataset/TrainDataset/',
help='path to train dataset')
parser.add_argument('--test_path', type=str,
default='./dataset/TestDataset/',
help='path to testing Kvasir dataset')
parser.add_argument('--train_save', type=str,
default='./model_pth/'+model_name+'/')
opt = parser.parse_args()
logging.basicConfig(filename='train_log.log',
format='[%(asctime)s-%(filename)s-%(levelname)s:%(message)s]',
level=logging.INFO, filemode='a', datefmt='%Y-%m-%d %I:%M:%S %p')
# ---- build models ----
# torch.cuda.set_device(0) # set your gpu device
model = PVT_Dense_Decoder().cuda()
best = 0
params = model.parameters()
if opt.optimizer == 'AdamW':
optimizer = torch.optim.AdamW(params, opt.lr, weight_decay=1e-4)
else:
optimizer = torch.optim.SGD(params, opt.lr, weight_decay=1e-4, momentum=0.9)
print(optimizer)
image_root = '{}/images/'.format(opt.train_path)
gt_root = '{}/masks/'.format(opt.train_path)
train_loader = get_loader(image_root, gt_root, batchsize=opt.batchsize, trainsize=opt.trainsize,
augmentation=opt.augmentation)
total_step = len(train_loader)
print("#" * 20, "Start Training", "#" * 20)
for epoch in range(1, opt.epoch):
adjust_lr(optimizer, opt.lr, epoch, 0.1, 200)
train(train_loader, model, optimizer, epoch, opt.test_path)
# plot the eval.png in the training stage
# plot_train(dict_plot, name)