-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
403 lines (343 loc) · 14.1 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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import os
import argparse
import torch
from torch import nn
import torch.backends.cudnn as cudnn
from torch.utils.data.distributed import DistributedSampler
from torch.utils.data import DataLoader
import numpy as np
from hrcnet import PoseHighResolutionNet
from dataset import potsdam
import setproctitle
import time
import sklearn.metrics as metric
from loss import SE_loss, Edge_loss
import logging
logging.basicConfig(filename='hrnet_train.log', level=logging.INFO)
# 0=impervious surfacescd
# 1=building
# 2=low vegetation
# 3=tree
# 4=car
# 5=background
# os.environ["CUDA_VISIBLE_DEVICES"] = "0,1"
# os.environ.setdefault('RANK', '0')
# os.environ.setdefault('WORLD_SIZE', '1')
# os.environ.setdefault('MASTER_ADDR', '202.204.54.125')
# os.environ.setdefault('MASTER_PORT', '29555')
class FullModel(nn.Module):
"""
Distribute the loss on multi-gpu to reduce
the memory cost in the main gpu.
You can check the following discussion.
https://discuss.pytorch.org/t/dataparallel-imbalanced-memory-usage/22551/21
"""
def __init__(self, model, loss, edge_loss1, se_loss1):
super(FullModel, self).__init__()
self.model = model
self.loss = loss
self.edge_loss = edge_loss1
self.se_loss = se_loss1
def forward(self, inputs, labels, edge=None, train=True):
output, edge_out, se_out = self.model(inputs)
if train:
seg_loss = self.loss(output, labels)
edge_loss = self.edge_loss(edge_out, edge)
se_loss = self.se_loss(se_out, labels)
loss = seg_loss + 0.9*edge_loss + 0.2 * se_loss
return torch.unsqueeze(loss, 0)
else:
return output
def get_world_size():
if not torch.distributed.is_initialized():
return 1
return torch.distributed.get_world_size()
def get_rank():
if not torch.distributed.is_initialized():
return 0
return torch.distributed.get_rank()
class params():
def __init__(self):
self.number_of_classes = 6
self.TRAIN_BATCH_SIZE_PER_GPU = 8
self.VAL_BATCH_SIZE_PER_GPU = 16
self.learning_rate = 0.01
"hrnet48"
self.STAGE2 = {'NUM_MODULES': 1,
'NUM_BRANCHES': 2,
'NUM_BLOCKS': [4,4],
'NUM_CHANNELS': [48,96],
'BLOCK':'BASIC',
'FUSE_METHOD': 'SUM'}
self.STAGE3 = {'NUM_MODULES': 4,
'NUM_BRANCHES': 3,
'NUM_BLOCKS': [6, 6, 6],
'NUM_CHANNELS': [48, 96, 192],
'BLOCK': 'BASIC',
'FUSE_METHOD': 'SUM'}
self.STAGE4 = {'NUM_MODULES': 3,
'NUM_BRANCHES': 4,
'NUM_BLOCKS': [3, 3, 3, 3],
'NUM_CHANNELS': [48, 96, 192, 384],
'BLOCK': 'BASIC',
'FUSE_METHOD': 'SUM'}
# "hrnet32"
# self.STAGE2 = {'NUM_MODULES': 1,
# 'NUM_BRANCHES': 2,
# 'NUM_BLOCKS': [4, 4],
# 'NUM_CHANNELS': [32, 64],
# 'BLOCK': 'BASIC',
# 'FUSE_METHOD': 'SUM'}
# self.STAGE3 = {'NUM_MODULES': 4,
# 'NUM_BRANCHES': 3,
# 'NUM_BLOCKS': [4, 4, 4],
# 'NUM_CHANNELS': [32, 64, 128],
# 'BLOCK': 'BASIC',
# 'FUSE_METHOD': 'SUM'}
# self.STAGE4 = {'NUM_MODULES': 3,
# 'NUM_BRANCHES': 4,
# 'NUM_BLOCKS': [4, 4, 4, 4],
# 'NUM_CHANNELS': [32, 64, 128, 256],
# 'BLOCK': 'BASIC',
# 'FUSE_METHOD': 'SUM'}
# "hrnet32_light"
# self.STAGE2 = {'NUM_MODULES': 1,
# 'NUM_BRANCHES': 2,
# 'NUM_BLOCKS': [4, 4],
# 'NUM_CHANNELS': [32, 64],
# 'BLOCK': 'BASIC',
# 'FUSE_METHOD': 'SUM'}
# self.STAGE3 = {'NUM_MODULES': 1,
# 'NUM_BRANCHES': 3,
# 'NUM_BLOCKS': [6, 6, 6],
# 'NUM_CHANNELS': [32, 64, 128],
# 'BLOCK': 'BASIC',
# 'FUSE_METHOD': 'SUM'}
# self.STAGE4 = {'NUM_MODULES': 1,
# 'NUM_BRANCHES': 4,
# 'NUM_BLOCKS': [3, 3, 3, 3],
# 'NUM_CHANNELS': [32, 64, 128, 256],
# 'BLOCK': 'BASIC',
# 'FUSE_METHOD': 'SUM'}
def get_params():
pa = params()
return pa
def get_model(args):
model = PoseHighResolutionNet(args)
return model
def reduce_tensor(inp):
"""
Reduce the loss from all processes so that
process with rank 0 has the averaged results.
"""
world_size = get_world_size()
if world_size < 2:
return inp
with torch.no_grad():
reduced_inp = inp
torch.distributed.reduce(reduced_inp, dst=0)
return reduced_inp
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.initialized = False
self.val = None
self.avg = None
self.sum = None
self.count = None
def initialize(self, val, weight):
self.val = val
self.avg = val
self.sum = val * weight
self.count = weight
self.initialized = True
def update(self, val, weight=1):
if not self.initialized:
self.initialize(val, weight)
else:
self.add(val, weight)
def add(self, val, weight):
self.val = val
self.sum += val * weight
self.count += weight
self.avg = self.sum / self.count
def value(self):
return self.val
def average(self):
return self.avg
def parse_args():
parser = argparse.ArgumentParser(description='Train segmentation network')
parser.add_argument("--local_rank", type=int, default=0)
parser.add_argument('opts',
help="Modify config options using the command-line",
default=None,
nargs=argparse.REMAINDER)
args2 = parser.parse_args()
return args2
def train():
args = get_params()
args2 = parse_args()
dataset_dir = "./dataset"
cudnn.benchmark = True
cudnn.deterministic = False
cudnn.enabled = True
distributed = True
device = torch.device(('cuda:{}').format(args2.local_rank))
PoseHighResolutionNet = get_model(args)
if distributed:
torch.cuda.set_device(args2.local_rank)
torch.distributed.init_process_group(
backend="nccl", init_method="env://",
)
potsdam_train = potsdam(base_dir=dataset_dir, train=True)
if distributed:
train_sampler = DistributedSampler(potsdam_train)
else:
train_sampler = None
dataloader_train = DataLoader(
potsdam_train,
batch_size=args.TRAIN_BATCH_SIZE_PER_GPU,
shuffle=True and train_sampler is None,
num_workers=4,
pin_memory=True,
drop_last=True,
sampler=train_sampler)
potsdam_val = potsdam(base_dir=dataset_dir, train=False)
if distributed:
val_sampler = DistributedSampler(potsdam_val)
else:
val_sampler = None
dataloader_val = DataLoader(
potsdam_val,
batch_size=args.VAL_BATCH_SIZE_PER_GPU,
shuffle=False,
num_workers=4,
pin_memory=True,
sampler=val_sampler)
seg_criterion = nn.CrossEntropyLoss(ignore_index=255)
edge_criterion = Edge_loss()
se_criterion = SE_loss()
# print(PoseHighResolutionNet)
# output = sys.stdout
# outputfile = open("hrnet_structure.txt", "a")
# sys.stdout = outputfile
PoseHighResolutionNet = FullModel(PoseHighResolutionNet, seg_criterion, edge_criterion, se_criterion)
PoseHighResolutionNet = nn.SyncBatchNorm.convert_sync_batchnorm(PoseHighResolutionNet)
PoseHighResolutionNet = PoseHighResolutionNet.to(device)
PoseHighResolutionNet = nn.parallel.DistributedDataParallel(
PoseHighResolutionNet, device_ids=[args2.local_rank], output_device=args2.local_rank)
optimizer = torch.optim.SGD([{'params':
filter(lambda p: p.requires_grad,
PoseHighResolutionNet.parameters()),
'lr': args.learning_rate}],
lr=args.learning_rate,
momentum=0.9,
weight_decay=0.0005,
nesterov=False,
)
# PoseHighResolutionNet.load_state_dict({k.replace('module.', ''): v for k, v in torch.load('model/hrnetv2_w48_imagenet_pretrained.pth').items()})
# PoseHighResolutionNet.load_state_dict(torch.load('model/hrnetv2_w48_imagenet_pretrained.pth'))
start = time.clock()
end_epoch = 400
lr = args.learning_rate
miou = [0]
best_miou = 0.1
last_epoch = 0
test_epoch = end_epoch - 50
ave_loss = AverageMeter()
world_size = get_world_size()
reduced_loss = [0]
model_state_file = "model/checkpoint_hrnet.pkl.tar"
if os.path.isfile(model_state_file):
logging.info("=> loading checkpoint '{}'".format(model_state_file))
checkpoint = torch.load(model_state_file, map_location=lambda storage, loc: storage)
best_miou = checkpoint['best_mIoU']
last_epoch = checkpoint['epoch']
PoseHighResolutionNet.load_state_dict(checkpoint['state_dict'])
optimizer.load_state_dict(checkpoint['optimizer'])
logging.info("=> loaded checkpoint '{}' (epoch {})".format(
model_state_file, checkpoint['epoch']))
for epoch in range(last_epoch, end_epoch):
if distributed:
train_sampler.set_epoch(epoch)
PoseHighResolutionNet.train()
setproctitle.setproctitle("Training:" + str(epoch) + "/" + "{}".format(end_epoch))
for i, sample in enumerate(dataloader_train):
images, labels, edge = sample['image'], sample['label'], sample['edge']
images, labels, edge = images.to(device), labels.to(device), edge.to(device)
labels = labels.long().squeeze(1)
edge = edge.long().squeeze(1)
losses = PoseHighResolutionNet(images, labels, edge)
loss = losses.mean()
ave_loss.update(loss.item())
lr = adjust_learning_rate(optimizer,
args.learning_rate,
end_epoch * len(dataloader_train),
i + epoch * len(dataloader_train))
if i % 50 == 0:
reduced_loss[0] = ave_loss.average()
print_loss = reduce_tensor(torch.from_numpy(np.array(reduced_loss)).to(device)).cpu()[0] / world_size
if args2.local_rank == 0:
time_cost = time.clock() - start
print("epoch:[{}/{}], iter:[{}/{}], loss:{}, time:{}, lr:{}, best_miou:{}".format(epoch,end_epoch,i,len(dataloader_train),print_loss,time_cost,lr,best_miou))
logging.info(
"epoch:[{}/{}], iter:[{}/{}], loss:{}, time:{}, lr:{}, best_miou:{}, miou:{}".format(epoch, end_epoch, i,
len(dataloader_train),
print_loss, time_cost, lr,
best_miou, miou[0]))
PoseHighResolutionNet.zero_grad()
loss.backward()
optimizer.step()
start = time.clock()
if epoch > test_epoch:
OA = validate(dataloader_val, device, PoseHighResolutionNet)
miou = reduce_tensor(OA).cpu()
if args2.local_rank == 0:
print(miou)
if epoch % 100 == 0 and epoch != 0:
torch.save(PoseHighResolutionNet.state_dict(),
'model/checkpoint_hrnet_{0}_{1}.pkl'.format(epoch, args.learning_rate))
if miou[0] >= best_miou:
best_miou = miou[0]
torch.save(PoseHighResolutionNet.state_dict(),
'model/checkpoint_hrnet_best_result_{}.pkl'.format(epoch))
torch.save({
'epoch': epoch + 1,
'best_mIoU': best_miou,
'state_dict': PoseHighResolutionNet.state_dict(),
'optimizer': optimizer.state_dict(),
}, 'model/checkpoint_hrnet.pkl.tar')
torch.save(PoseHighResolutionNet.state_dict(),
'model/checkpoint_hrnet_{0}_{1}.pkl'.format(end_epoch, args.learning_rate))
def adjust_learning_rate(optimizer, base_lr, max_iters,
cur_iters, power=0.9):
lr = base_lr*((1-float(cur_iters)/max_iters)**(power))
optimizer.param_groups[0]['lr'] = lr
return lr
def validate(dataloader_val, device, PoseHighResolutionNet):
PoseHighResolutionNet.eval()
OA = [0]
n = 0
with torch.no_grad():
for i, sample in enumerate(dataloader_val):
images, labels = sample['image'], sample['label']
images, labels = images.to(device), labels.to(device)
labels = labels.long().squeeze(1)
logits = PoseHighResolutionNet(images, labels, train=False)
logits = logits.argmax(dim=1)
logits = logits.cpu().detach().numpy()
labels = labels.cpu().detach().numpy()
for j in range(logits.shape[0]):
n += 1
prediction = logits[j]
prediction = np.reshape(prediction, (384 * 384))
label = labels[j]
label = np.reshape(label, (384 * 384))
oa = metric.accuracy_score(label, prediction)
OA = OA + oa
OA = OA/(2*n)
print("OA:{}".format(OA))
OA = torch.from_numpy(OA).to(device)
return OA
if __name__ == '__main__':
train()