-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainSupervised.py
184 lines (154 loc) · 7.17 KB
/
trainSupervised.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
import argparse
from torch.utils.data import DataLoader
from torch.utils import data
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, reverse_one_hot, compute_global_accuracy, fast_hist, per_class_iu, compute_loss
from loss import DiceLoss
import torch.cuda.amp as amp
from dataset.cityscapes_dataset import cityscapesDataSet
from PIL import Image
from arguments import get_args
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) #Transform into a 2D array with only 1 channel, where each pixel value is the classified class key
predict = np.array(predict.cpu())
# get RGB label image
label = label.squeeze()
if args.loss == 'dice':
label = reverse_one_hot(label)
label = np.array(label.cpu())
# compute per pixel accuracy
precision = compute_global_accuracy(predict, label) #accuracy over all classes given the prediction and the label
hist += fast_hist(label.flatten(), predict.flatten(), args.num_classes)
# there is no need to transform the one-hot array to visual RGB array
precision_record.append(precision)
precision = np.mean(precision_record) # mean over all precisions
miou_list = per_class_iu(hist)
miou = np.mean(miou_list)
print('precision per pixel for test: %.3f' % precision)
print('mIoU for validation: %.3f' % miou)
print(f'mIoU per class: {miou_list}')
return precision, miou
def train(args, model, optimizer, dataloader_train, dataloader_val):
writer = SummaryWriter(comment=''.format(args.optimizer, args.context_path))
scaler = amp.GradScaler()
if args.loss == 'dice':
loss_func = DiceLoss()
elif args.loss == 'crossentropy':
loss_func = torch.nn.CrossEntropyLoss(ignore_index=255)
max_miou = 0
step = 0
for epoch in range(args.num_epochs):
lr = poly_lr_scheduler(optimizer, args.learning_rate, iter=epoch, max_iter=args.num_epochs)
model.train()
tq = tqdm(total=len(dataloader_train) * args.batch_size)
tq.set_description('epoch %d, lr %f' % (epoch, lr))
loss_record = []
for i, (data,label,_,_) in enumerate(dataloader_train):
data = data.cuda()
label = label.long().cuda()
optimizer.zero_grad() #sets the gradients of all optimized to zero.
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), epoch)
print('loss for train : %f' % (loss_train_mean))
if epoch % args.checkpoint_step == 0 and epoch != 0:
import os
if not os.path.isdir(args.save_model_path):
os.mkdir(args.save_model_path)
torch.save(model.module.state_dict(),
os.path.join(args.save_model_path, 'latest_dice_loss.pth'))
if epoch % args.validation_step == 0 and epoch != 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'))
writer.add_scalar('epoch/precision_val', precision, epoch)
writer.add_scalar('epoch/miou val', miou, epoch)
def main(params):
args, img_mean = get_args(params)
cropSize= (args.crop_width , args.crop_height)
dataset_train = cityscapesDataSet(args.dataset, args.data, max_iters= args.num_epochs* args.num_iter*args.batch_size, crop_size=cropSize, ignore_label=255, encodeseg = 1)
dataset_val = cityscapesDataSet(args.dataset, args.val, max_iters= args.num_epochs* args.num_iter*args.batch_size , crop_size=cropSize, encodeseg=1)
# Define HERE your dataloaders:
dataloader_train = DataLoader(dataset_train,
batch_size=args.batch_size,
shuffle=True,
num_workers = args.num_workers,
drop_last=True
)
dataloader_val = DataLoader(dataset_val,
batch_size=1,
shuffle=True ,
num_workers = args.num_workers
)
# build model
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()
# build optimizer
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:
print('not supported optimizer \n')
return None
# load pretrained model if exists
if args.pretrained_model_path is not None:
print('load model from %s ...' % args.pretrained_model_path)
model.module.load_state_dict(torch.load(args.pretrained_model_path))
print('Done!')
# train
train(args, model, optimizer, dataloader_train, dataloader_val)
# final test
val(args, model, dataloader_val)
if __name__ == '__main__':
params = [
'--num_epochs', '100',
'--learning_rate', '2.5e-2',
'--data', './dataset/data/Cityscapes/train.txt',
'--num_workers', '8',
'--num_classes', '19',
'--cuda', '0',
'--batch_size', '4',
'--save_model_path', './checkpoints_101_sgd',
'--context_path', 'resnet101', # set resnet18 or resnet101, only support resnet18 and resnet101
'--optimizer', 'rmsprop'
]
main(params)