-
Notifications
You must be signed in to change notification settings - Fork 0
/
seg_train.py
236 lines (201 loc) · 10.2 KB
/
seg_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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import argparse
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
from dataset.CamVid import CamVid
import os
from model.build_BiSeNet import BiSeNet
import torch
from tensorboardX import SummaryWriter
from tqdm import tqdm
import numpy as np
from utils import poly_lr_scheduler
from utils import reverse_one_hot, compute_global_accuracy, fast_hist, \
per_class_iu
from loss import DiceLoss
import torch.cuda.amp as amp
# ------------------- Validation function -------------------
def val(args, model, dataloader):
print('start val!')
with torch.no_grad():
model.eval()
precision_record = []
hist = np.zeros((args.num_classes, args.num_classes))
for i, (data, label) in enumerate(dataloader):
label = label.type(torch.LongTensor)
data = data.cuda()
label = label.long().cuda()
# get RGB predict image
predict = model(data).squeeze()
predict = reverse_one_hot(predict)
predict = np.array(predict.cpu())
# get RGB label image
label = label.squeeze()
label = reverse_one_hot(label)
label = np.array(label.cpu())
# compute per pixel accuracy
precision = compute_global_accuracy(predict, label)
hist += fast_hist(label.flatten(), predict.flatten(), args.num_classes)
precision_record.append(precision)
precision = np.mean(precision_record)
miou_list = per_class_iu(hist)[:-1]
miou = np.mean(miou_list)
print('precision per pixel for test: %.3f' % precision)
print('mIoU for validation: %.3f' % miou)
return precision, miou
# ------------------- Training function -------------------
def train(args, model, optimizer, dataloader_train, dataloader_val):
writer = SummaryWriter(comment=''.format(args.optimizer, args.context_path))
scaler = amp.GradScaler()
loss_func = DiceLoss()
step = 0
# Start resuming information (if pretrained mode exists)
epoch_start_i = args.epoch_start_i
max_miou = args.max_miou
if epoch_start_i != 0:
print('Recovered epoch: ', epoch_start_i)
print('Recovered max_miou: ', max_miou)
for i_iter in range(epoch_start_i, args.num_epochs):
lr = poly_lr_scheduler(optimizer, args.learning_rate, iter=i_iter, max_iter=args.num_epochs)
model.train()
tq = tqdm(total=len(dataloader_train) * args.batch_size)
tq.set_description('epoch %d, lr %f' % (i_iter, lr))
loss_record = []
for i, (data, label) in enumerate(dataloader_train):
data = data.cuda()
label = label.long().cuda()
optimizer.zero_grad()
with amp.autocast():
output, output_sup1, output_sup2 = model(data)
loss1 = loss_func(output, label)
loss2 = loss_func(output_sup1, label)
loss3 = loss_func(output_sup2, label)
loss = loss1 + loss2 + loss3
scaler.scale(loss).backward()
scaler.step(optimizer)
scaler.update()
tq.update(args.batch_size)
tq.set_postfix(loss='%.6f' % loss)
step += 1
writer.add_scalar('loss_step', loss, step)
loss_record.append(loss.item())
tq.close()
loss_train_mean = np.mean(loss_record)
writer.add_scalar('epoch/loss_epoch_train', float(loss_train_mean), i_iter)
print('loss for train : %f' % (loss_train_mean))
# -------------------- saving checkpoint --------------------
if i_iter % args.checkpoint_step == 0 and i_iter != 0:
import os
if not os.path.isdir(args.save_model_path):
os.mkdir(args.save_model_path)
state = {
"epoch": i_iter,
"max_miou": max_miou,
"model_state_dict": model.module.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
}
torch.save(state,
os.path.join(args.save_model_path, 'latest_dice_loss.pth'))
print('Checkpoint saved')
# -------------------- validation step --------------------
if i_iter % args.validation_step == 0 and i_iter != 0:
precision, miou = val(args, model, dataloader_val)
if miou > max_miou:
max_miou = miou
import os
os.makedirs(args.save_model_path, exist_ok=True)
torch.save(model.module.state_dict(),
os.path.join(args.save_model_path, 'best_dice_loss.pth'))
print("Found a better model. Best model updated --> max_miou: ", max_miou)
writer.add_scalar('epoch/precision_val', precision, i_iter)
writer.add_scalar('epoch/miou_val', miou, i_iter)
def main(params):
# -------------------- basic parameters --------------------
parser = argparse.ArgumentParser()
parser.add_argument('--num_epochs', type=int, default=300, help='Number of epochs to train for')
parser.add_argument('--checkpoint_step', type=int, default=10, help='How often to save checkpoints (epochs)')
parser.add_argument('--validation_step', type=int, default=10, help='How often to perform validation (epochs)')
parser.add_argument('--dataset', type=str, default="CamVid", help='Dataset you are using.')
parser.add_argument('--crop_height', type=int, default=720, help='Height of cropped/resized input image to network')
parser.add_argument('--crop_width', type=int, default=960, help='Width of cropped/resized input image to network')
parser.add_argument('--batch_size', type=int, default=32, help='Number of images in each batch')
parser.add_argument('--context_path', type=str, default="resnet101",
help='The context path model you are using, resnet18, resnet101.')
parser.add_argument('--learning_rate', type=float, default=0.01, help='learning rate used for train')
parser.add_argument('--data', type=str, default='', help='path of training data')
parser.add_argument('--num_workers', type=int, default=4, help='num of workers')
parser.add_argument('--num_classes', type=int, default=32, help='num of object classes (with void)')
parser.add_argument('--cuda', type=str, default='0', help='GPU ids used for training')
parser.add_argument('--use_gpu', type=bool, default=True, help='whether to user gpu for training')
parser.add_argument('--pretrained_model_path', type=str, default=None, help='path to pretrained model')
parser.add_argument('--save_model_path', type=str, default=None, help='path to save model')
parser.add_argument('--optimizer', type=str, default='rmsprop', help='optimizer, support rmsprop, sgd, adam')
parser.add_argument('--loss', type=str, default='dice', help='loss function, dice or crossentropy')
parser.add_argument('--epoch_start_i', type=int, default=0, help='Start counting epochs from this number')
parser.add_argument('--max_miou', type=float, default=0, help="Maximum value of miou achieved.")
args = parser.parse_args(params)
# --------------------- CamVid Dataset and dataloader ---------------------
train_path = [os.path.join(args.data, 'train'), os.path.join(args.data, 'val')]
train_label_path = [os.path.join(args.data, 'train_labels'), os.path.join(args.data, 'val_labels')]
test_path = os.path.join(args.data, 'test')
test_label_path = os.path.join(args.data, 'test_labels')
csv_path = os.path.join(args.data, 'class_dict.csv')
dataset_train = CamVid(train_path, train_label_path, csv_path, scale=(args.crop_height, args.crop_width),
loss='dice', mode='train')
dataloader_train = DataLoader(
dataset_train,
batch_size=args.batch_size,
shuffle=True,
num_workers=args.num_workers,
drop_last=True,
)
dataset_val = CamVid(test_path, test_label_path, csv_path, scale=(args.crop_height, args.crop_width),
loss='dice', mode='test')
dataloader_val = DataLoader(
dataset_val,
batch_size=1,
shuffle=True,
num_workers=args.num_workers
)
# ------------------- Models building -------------------
os.environ['CUDA_VISIBLE_DEVICES'] = args.cuda
model = BiSeNet(args.num_classes, args.context_path)
if torch.cuda.is_available() and args.use_gpu:
model = torch.nn.DataParallel(model).cuda()
# ------------------- Optimizer building -------------------
if args.optimizer == 'rmsprop':
optimizer = torch.optim.RMSprop(model.parameters(), args.learning_rate)
elif args.optimizer == 'sgd':
optimizer = torch.optim.SGD(model.parameters(), args.learning_rate, momentum=0.9, weight_decay=1e-4)
elif args.optimizer == 'adam':
optimizer = torch.optim.Adam(model.parameters(), args.learning_rate)
else: # rmsprop
print('not supported optimizer \n')
return None
# ------------------- Pre-trained model loading -------------------
if os.path.exists(args.pretrained_model_path):
print('load model from %s ...' % args.pretrained_model_path)
checkpoint = torch.load(args.pretrained_model_path)
model.module.load_state_dict(checkpoint['model_state_dict'])
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
args.epoch_start_i = checkpoint['epoch'] + 1
args.max_miou = checkpoint['max_miou']
print('Pre-trained model found and recovered!')
# ------------------- Train and (final) validation -------------------
train(args, model, optimizer, dataloader_train, dataloader_val)
val(args, model, dataloader_val)
if __name__ == '__main__':
params = [
'--num_epochs', '100',
'--learning_rate', '2.5e-2',
'--data', './data/CamVid',
'--num_workers', '8',
'--num_classes', '12',
'--cuda', '0',
'--batch_size', '4',
'--save_model_path', './seg_checkpoints',
'--context_path', 'resnet18',
'--optimizer', 'sgd',
'--checkpoint_step', '5',
'--pretrained_model_path', './seg_checkpoints/latest_dice_loss.pth'
]
main(params)