-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtrain_pl_polyp.py
345 lines (267 loc) · 11.8 KB
/
train_pl_polyp.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
from typing import Optional
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '3'
import numpy as np
import copy
from pytorch_lightning.utilities.types import EVAL_DATALOADERS, STEP_OUTPUT
import torch
from torch import nn
import torch.nn.functional as F
from torchvision import datasets, transforms
from tqdm import tqdm
import pytorch_lightning as pl
import yaml
from easydict import EasyDict
import random
from pytorch_lightning import callbacks
from pytorch_lightning.accelerators import accelerator
from pytorch_lightning.core.hooks import CheckpointHooks
from pytorch_lightning.callbacks import ModelCheckpoint,DeviceStatsMonitor,EarlyStopping,LearningRateMonitor
from pytorch_lightning.strategies import DDPStrategy
from pytorch_lightning.loggers import TensorBoardLogger
from argparse import Namespace
from data_polyp import get_trainloader,get_testloader
from torch.utils.data import DataLoader
from loss import *
# from models2.refinenet import RefineNet
from torchvision.utils import save_image
output_dir = 'logs'
version_name='Baseline'
logger = TensorBoardLogger(name='vivim_polyp',save_dir = output_dir )
import matplotlib.pyplot as plt
# import tent
import math
from medpy import metric
# from misc import *
import misc2
import torchmetrics
from modeling.vivim import Vivim
from poloy_metrics import *
from modeling.utils import JointEdgeSegLoss
# torch.set_float32_matmul_precision('high')
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()
class CoolSystem(pl.LightningModule):
def __init__(self, hparams):
super(CoolSystem, self).__init__()
self.params = hparams
self.epochs = self.params.epochs
self.save_path='/home/yijun/project/ultra/save_images_polyp2'
self.data_root=self.params.data_root
self.initlr = self.params.initlr
self.train_batchsize = self.params.train_bs
self.val_batchsize = self.params.val_bs
#Train setting
self.initlr = self.params.initlr #initial learning
self.weight_decay = self.params.weight_decay #optimizers weight decay
self.crop_size = self.params.crop_size #random crop size
self.num_workers = self.params.num_workers
self.epochs = self.params.epochs
self.shift_length = self.params.shift_length
self.val_aug = self.params.val_aug
self.with_edge = self.params.with_edge
self.gts = []
self.preds = []
self.nFrames = 5
self.upscale_factor = 1
self.data_augmentation = True
self.criterion = JointEdgeSegLoss(classes=2) if self.with_edge else structure_loss
self.model = Vivim(with_edge=self.with_edge)
self.save_hyperparameters()
def configure_optimizers(self):
# REQUIRED
optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad, self.model.parameters()), lr=self.initlr,betas=[0.9,0.999])#,weight_decay=self.weight_decay)
# optimizer = Lion(filter(lambda p: p.requires_grad, self.model.parameters()), lr=self.initlr,betas=[0.9,0.99],weight_decay=0)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=self.epochs, eta_min=self.initlr * 0.01)
return [optimizer], [scheduler]
# def training_epoch_start(self):
# self.scheduler = pipeline.create_SR3scheduler(self.diff_opt['scheduler'], 'train')
def init_weight(self,ckpt_path=None):
if ckpt_path:
checkpoint = torch.load(ckpt_path)
print(checkpoint.keys())
checkpoint_model = checkpoint
state_dict = self.model.state_dict()
# # 1. filter out unnecessary keys
checkpoint_model = {k: v for k, v in checkpoint_model.items() if k in state_dict.keys()}
print(checkpoint_model.keys())
# 2. overwrite entries in the existing state dict
state_dict.update(checkpoint_model)
self.model.load_state_dict(checkpoint_model, strict=False)
def evaluate_one_img(self, pred, gt):
dice = misc2.dice(pred, gt)
specificity = misc2.specificity(pred, gt)
jaccard = misc2.jaccard(pred, gt)
precision = misc2.precision(pred, gt)
recall = misc2.recall(pred, gt)
f_measure = misc2.fscore(pred, gt)
return dice, specificity, precision, recall, f_measure, jaccard
def training_step(self, batch, batch_idx):
self.model.train()
neigbor, target, edge_gt = batch
# print(edge_gt.shape)
target = target.cuda()
#bicubic = bicubic.cuda()
neigbor = neigbor.cuda()
bz, nf, nc, h, w = target.shape
# noisy_images = torch.cat([noisy_images,neigbor_],dim=1)
# print(neigbor.shape)
#print("timesteps:",timesteps)#.type())
if not self.with_edge:
pred = self.model(neigbor)#, return_dict=False)[0]
target = target.reshape(bz*nf,nc,h,w)
loss = self.criterion(pred[self.nFrames//2::self.nFrames], target[self.nFrames//2::self.nFrames])
else:
pred,e0 = self.model(neigbor)#, return_dict=False)[0]
target = target.reshape(bz*nf,nc,h,w)
edge_gt = edge_gt.reshape(bz*nf,1,h,w)
loss = self.criterion((pred[self.nFrames//2::self.nFrames], e0[self.nFrames//2::self.nFrames]), (target[self.nFrames//2::self.nFrames], edge_gt[self.nFrames//2::self.nFrames]))
self.log("train_loss",loss,prog_bar=True)
# self.log("aux_loss",aux_loss,prog_bar=True)
return {"loss":loss}
def on_validation_epoch_end(self):
self.sm = Smeasure()
self.em = Emeasure()
self.mae = MAE()
dice_lst, specificity_lst, precision_lst, recall_lst, f_measure_lst, jaccard_lst = [], [], [], [], [], []
Thresholds = np.linspace(1, 0, 256)
# print(Thresholds)
for pred, gt in zip(self.preds,self.gts):
pred = torch.sigmoid(pred)
# gt = gt.to(int)
self.sm.step(pred.squeeze(0).squeeze(0).detach().cpu().numpy(),gt.squeeze(0).squeeze(0).detach().cpu().numpy())
self.em.step(pred.squeeze(0).squeeze(0).detach().cpu().numpy(),gt.squeeze(0).squeeze(0).detach().cpu().numpy())
self.mae.step(pred.squeeze(0).squeeze(0).detach().cpu().numpy(),gt.squeeze(0).squeeze(0).detach().cpu().numpy())
gt = (gt>0.5).to(int)
dice_l, specificity_l, precision_l, recall_l, f_measure_l, jaccard_l = [], [], [], [], [], []
for j, threshold in enumerate(Thresholds):
# print(threshold)
pred_one_hot = (pred>threshold).to(int)
dice, specificity, precision, recall, f_measure, jaccard = self.evaluate_one_img(pred_one_hot.detach().cpu().numpy(), gt.detach().cpu().numpy())
# print(dice)
dice_l.append(dice)
specificity_l.append(specificity)
precision_l.append(precision)
recall_l.append(recall)
f_measure_l.append(f_measure)
jaccard_l.append(jaccard)
dice_lst.append(sum(dice_l) / len(dice_l))
specificity_lst.append(sum(specificity_l) / len(specificity_l))
precision_lst.append(sum(precision_l) / len(precision_l))
recall_lst.append(sum(recall_l) / len(recall_l))
f_measure_lst.append(sum(f_measure_l) / len(f_measure_l))
jaccard_lst.append(sum(jaccard_l) / len(jaccard_l))
# print(sum(dice_l) / len(dice_l))
# mean
dice = sum(dice_lst) / len(dice_lst)
acc = sum(specificity_lst) / len(specificity_lst)
precision = sum(precision_lst) / len(precision_lst)
recall = sum(recall_lst) / len(recall_lst)
f_measure = sum(f_measure_lst) / len(f_measure_lst)
jac = sum(jaccard_lst) / len(jaccard_lst)
sm = self.sm.get_results()['Smeasure']
em = self.em.get_results()['meanEm']
mae = self.mae.get_results()['MAE']
print(len(self.gts))
print(len(self.preds))
self.log('Dice',dice)
self.log('Jaccard',jac)
self.log('Precision',precision)
self.log('Recall',recall)
self.log('Fmeasure',f_measure)
self.log('specificity',acc)
self.log('Smeasure',sm)
self.log('Emeasure',em)
self.log('MAE',mae)
self.gts = []
self.preds = []
print("Val: Dice {0}, Jaccard {1}, Precision {2}, Recall {3}, Fmeasure {4}, specificity: {5}, Smeasure {6}, Emeasure {7}, MAE: {8}".format(dice,jac,precision,recall,f_measure,acc,sm,em,mae))
def validation_step(self,batch,batch_idx):
# torch.set_grad_enabled(True)
self.model.eval()
neigbor,target = batch
bz, nf, nc, h, w = neigbor.shape
# import time
# start = time.time()
if not self.with_edge:
samples = self.model(neigbor)
else:
samples,_ = self.model(neigbor)
samples = samples[self.nFrames//2::self.nFrames]
filename = "sample_{}.png".format(batch_idx)
save_image(samples,os.path.join(self.save_path, filename))
filename = "target_{}.png".format(batch_idx)
save_image(target,os.path.join(self.save_path, filename))
self.preds.append(samples)
self.gts.append(target)
def train_dataloader(self):
train_loader = get_trainloader(self.data_root, batchsize=self.train_batchsize, trainsize=self.crop_size)
return train_loader
def val_dataloader(self):
val_loader = get_testloader(self.data_root, batchsize=self.val_batchsize, trainsize=self.crop_size)
return val_loader
def main():
RESUME = False
resume_checkpoint_path = r'/home/yijun/project/ultra/logs/uentm_polyp/version_2/checkpoints/ultra-epoch.ckpt'
if RESUME == False:
resume_checkpoint_path =None
#128: 32-0.0005
args={
'epochs': 200, #datasetsw
'data_root':'./polyp/',
'train_bs':8,
'test_bs':1,
'val_bs':1,
'initlr':1e-4,
'weight_decay':0.01,
'crop_size':256,
'num_workers':8,
'shift_length':32,
'val_aug':False,
'with_edge':False,
'seed': 1234
}
torch.manual_seed(args['seed'])
random.seed(args['seed'])
np.random.seed(args['seed'])
if torch.cuda.is_available():
torch.cuda.manual_seed_all(args['seed'])
torch.backends.cudnn.benchmark = True
hparams = Namespace(**args)
model = CoolSystem(hparams)
checkpoint_callback = ModelCheckpoint(
monitor='Dice',
#dirpath='/mnt/data/yt/Documents/TSANet-underwater/snapshots',
filename='ultra-epoch{epoch:02d}-Dice-{Dice:.4f}-Jaccard-{Jaccard:.4f}',
auto_insert_metric_name=False,
every_n_epochs=1,
save_top_k=1,
mode = "max",
save_last=True
)
lr_monitor_callback = LearningRateMonitor(logging_interval='step')
trainer = pl.Trainer(
check_val_every_n_epoch=5,
max_epochs=hparams.epochs,
accelerator='gpu',
devices=1,
precision=32,
logger=logger,
strategy="auto",
enable_progress_bar=True,
log_every_n_steps=5,
callbacks = [checkpoint_callback,lr_monitor_callback]
)
trainer.fit(model,ckpt_path=resume_checkpoint_path)
# val_path=r'/home/yijun/project/ultra/logs/uentm_polyp/version_60/checkpoints/ultra-epoch.ckpt'
# trainer.validate(model,ckpt_path=val_path)
if __name__ == '__main__':
#your code
main()