-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.py
168 lines (140 loc) · 6.26 KB
/
main.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
import os
import random
import argparse
import numpy as np
import torch
import torch.optim as optim
from torch.utils.data import DataLoader
from dataloader.dataset import MedicalDataSets
from albumentations.augmentations import transforms
from albumentations.core.composition import Compose
from albumentations import RandomRotate90, Resize
from utils.util import AverageMeter
import utils.losses as losses
from utils.metrics import iou_score
from network.CMUNeXt import cmunext, cmunext_s, cmunext_l
def seed_torch(seed):
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
random.seed(seed)
np.random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
seed_torch(41)
parser = argparse.ArgumentParser()
parser.add_argument('--model', type=str, default="CMUNeXt",
choices=["CMUNeXt", "CMUNeXt-S", "CMUNeXt-L"], help='model')
parser.add_argument('--base_dir', type=str, default="./data/busi", help='dir')
parser.add_argument('--train_file_dir', type=str, default="busi_train.txt", help='dir')
parser.add_argument('--val_file_dir', type=str, default="busi_val.txt", help='dir')
parser.add_argument('--base_lr', type=float, default=0.01,
help='segmentation network learning rate')
parser.add_argument('--batch_size', type=int, default=8,
help='batch_size per gpu')
args = parser.parse_args()
def getDataloader():
img_size = 256
train_transform = Compose([
RandomRotate90(),
transforms.Flip(),
Resize(img_size, img_size),
transforms.Normalize(),
])
val_transform = Compose([
Resize(img_size, img_size),
transforms.Normalize(),
])
db_train = MedicalDataSets(base_dir=args.base_dir, split="train", transform=train_transform,
train_file_dir=args.train_file_dir, val_file_dir=args.val_file_dir)
db_val = MedicalDataSets(base_dir=args.base_dir, split="val", transform=val_transform,
train_file_dir=args.train_file_dir, val_file_dir=args.val_file_dir)
print("train num:{}, val num:{}".format(len(db_train), len(db_val)))
trainloader = DataLoader(db_train, batch_size=8, shuffle=True,
num_workers=8, pin_memory=False)
valloader = DataLoader(db_val, batch_size=1, shuffle=False,
num_workers=1)
return trainloader, valloader
def get_model(args):
if args.model == "CMUNeXt":
model = cmunext()
elif args.model == "CMUNeXt-S":
model = cmunext_s()
elif args.model == "CMUNeXt-L":
model = cmunext_l()
else:
model = None
print("model err")
exit(0)
return model.cuda()
def train(args):
base_lr = args.base_lr
trainloader, valloader = getDataloader()
model = get_model(args)
print("train file dir:{} val file dir:{}".format(args.train_file_dir, args.val_file_dir))
optimizer = optim.SGD(model.parameters(), lr=base_lr, momentum=0.9, weight_decay=0.0001)
criterion = losses.__dict__['BCEDiceLoss']().cuda()
print("{} iterations per epoch".format(len(trainloader)))
best_iou = 0
iter_num = 0
max_epoch = 300
max_iterations = len(trainloader) * max_epoch
for epoch_num in range(max_epoch):
model.train()
avg_meters = {'loss': AverageMeter(),
'iou': AverageMeter(),
'val_loss': AverageMeter(),
'val_iou': AverageMeter(),
'SE': AverageMeter(),
'PC': AverageMeter(),
'F1': AverageMeter(),
'ACC': AverageMeter()
}
for i_batch, sampled_batch in enumerate(trainloader):
volume_batch, label_batch = sampled_batch['image'], sampled_batch['label']
volume_batch, label_batch = volume_batch.cuda(), label_batch.cuda()
outputs = model(volume_batch)
loss = criterion(outputs, label_batch)
iou, dice, _, _, _, _, _ = iou_score(outputs, label_batch)
optimizer.zero_grad()
loss.backward()
optimizer.step()
lr_ = base_lr * (1.0 - iter_num / max_iterations) ** 0.9
for param_group in optimizer.param_groups:
param_group['lr'] = lr_
iter_num = iter_num + 1
avg_meters['loss'].update(loss.item(), volume_batch.size(0))
avg_meters['iou'].update(iou, volume_batch.size(0))
model.eval()
with torch.no_grad():
for i_batch, sampled_batch in enumerate(valloader):
input, target = sampled_batch['image'], sampled_batch['label']
input = input.cuda()
target = target.cuda()
output = model(input)
loss = criterion(output, target)
iou, _, SE, PC, F1, _, ACC = iou_score(output, target)
avg_meters['val_loss'].update(loss.item(), input.size(0))
avg_meters['val_iou'].update(iou, input.size(0))
avg_meters['SE'].update(SE, input.size(0))
avg_meters['PC'].update(PC, input.size(0))
avg_meters['F1'].update(F1, input.size(0))
avg_meters['ACC'].update(ACC, input.size(0))
print(
'epoch [%d/%d] train_loss : %.4f, train_iou: %.4f '
'- val_loss %.4f - val_iou %.4f - val_SE %.4f - val_PC %.4f - val_F1 %.4f - val_ACC %.4f'
% (epoch_num, max_epoch, avg_meters['loss'].avg, avg_meters['iou'].avg,
avg_meters['val_loss'].avg, avg_meters['val_iou'].avg, avg_meters['SE'].avg,
avg_meters['PC'].avg, avg_meters['F1'].avg, avg_meters['ACC'].avg))
if avg_meters['val_iou'].avg > best_iou:
if not os.path.exists('./checkpoint'):
os.mkdir('checkpoint')
torch.save(model.state_dict(), 'checkpoint/{}_model_{}.pth'
.format(args.model, args.train_file_dir.split(".")[0]))
best_iou = avg_meters['val_iou'].avg
print("=> saved best model")
return "Training Finished!"
if __name__ == "__main__":
train(args)