-
Notifications
You must be signed in to change notification settings - Fork 697
Expand file tree
/
Copy pathfunction.py
More file actions
206 lines (171 loc) · 7.21 KB
/
function.py
File metadata and controls
206 lines (171 loc) · 7.21 KB
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
# ------------------------------------------------------------------------------
# Copyright (c) Microsoft
# Licensed under the MIT License.
# Written by Ke Sun (sunk@mail.ustc.edu.cn)
# ------------------------------------------------------------------------------
import logging
import os
import time
import numpy as np
import numpy.ma as ma
from tqdm import tqdm
import torch
import torch.nn as nn
import torch.distributed as dist
from torch.nn import functional as F
from utils.utils import AverageMeter
from utils.utils import get_confusion_matrix
from utils.utils import adjust_learning_rate
from utils.utils import get_world_size, get_rank
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
dist.reduce(reduced_inp, dst=0)
return reduced_inp
def train(config, epoch, num_epoch, epoch_iters, base_lr, num_iters,
trainloader, optimizer, model, writer_dict, device):
# Training
model.train()
batch_time = AverageMeter()
ave_loss = AverageMeter()
tic = time.time()
cur_iters = epoch*epoch_iters
writer = writer_dict['writer']
global_steps = writer_dict['train_global_steps']
rank = get_rank()
world_size = get_world_size()
for i_iter, batch in enumerate(trainloader):
images, labels, _, _ = batch
images = images.to(device)
labels = labels.long().to(device)
losses, _ = model(images, labels)
loss = losses.mean()
reduced_loss = reduce_tensor(loss)
model.zero_grad()
loss.backward()
optimizer.step()
# measure elapsed time
batch_time.update(time.time() - tic)
tic = time.time()
# update average loss
ave_loss.update(reduced_loss.item())
lr = adjust_learning_rate(optimizer,
base_lr,
num_iters,
i_iter+cur_iters)
if i_iter % config.PRINT_FREQ == 0 and rank == 0:
print_loss = ave_loss.average() / world_size
msg = 'Epoch: [{}/{}] Iter:[{}/{}], Time: {:.2f}, ' \
'lr: {:.6f}, Loss: {:.6f}' .format(
epoch, num_epoch, i_iter, epoch_iters,
batch_time.average(), lr, print_loss)
logging.info(msg)
writer.add_scalar('train_loss', print_loss, global_steps)
writer_dict['train_global_steps'] = global_steps + 1
def validate(config, testloader, model, writer_dict, device):
rank = get_rank()
world_size = get_world_size()
model.eval()
ave_loss = AverageMeter()
confusion_matrix = torch.zeros([config.DATASET.NUM_CLASSES, config.DATASET.NUM_CLASSES], device=device)
with torch.no_grad():
for _, batch in enumerate(testloader):
image, label, _, _ = batch
size = label.size()
image = image.to(device)
label = label.long().to(device)
losses, pred = model(image, label)
pred = F.upsample(input=pred, size=(
size[-2], size[-1]), mode='bilinear')
loss = losses.mean()
reduced_loss = reduce_tensor(loss)
ave_loss.update(reduced_loss.item())
confusion_matrix += get_confusion_matrix_gpu(label, pred, size,
config.DATASET.NUM_CLASSES,
config.TRAIN.IGNORE_LABEL,
device)
reduced_confusion_matrix = reduce_tensor(confusion_matrix)
pos = torch.sum(reduced_confusion_matrix, 1)
res = torch.sum(reduced_confusion_matrix, 0)
tp = torch.diag(reduced_confusion_matrix)
IoU_array = (tp / torch.maximum(torch.ones_like(tp), pos + res - tp))
mean_IoU = torch.mean(IoU_array)
print_loss = ave_loss.average()/world_size
if rank == 0:
writer = writer_dict['writer']
global_steps = writer_dict['valid_global_steps']
writer.add_scalar('valid_loss', print_loss, global_steps)
writer.add_scalar('valid_mIoU', mean_IoU, global_steps)
writer_dict['valid_global_steps'] = global_steps + 1
return print_loss, mean_IoU, IoU_array
def testval(config, test_dataset, testloader, model,
sv_dir='', sv_pred=False):
model.eval()
confusion_matrix = np.zeros(
(config.DATASET.NUM_CLASSES, config.DATASET.NUM_CLASSES))
with torch.no_grad():
for index, batch in enumerate(tqdm(testloader)):
image, label, _, name = batch
size = label.size()
pred = test_dataset.multi_scale_inference(
model,
image,
scales=config.TEST.SCALE_LIST,
flip=config.TEST.FLIP_TEST)
if pred.size()[-2] != size[-2] or pred.size()[-1] != size[-1]:
pred = F.upsample(pred, (size[-2], size[-1]),
mode='bilinear')
confusion_matrix += get_confusion_matrix(
label,
pred,
size,
config.DATASET.NUM_CLASSES,
config.TRAIN.IGNORE_LABEL)
if sv_pred:
sv_path = os.path.join(sv_dir,'test_val_results')
if not os.path.exists(sv_path):
os.mkdir(sv_path)
test_dataset.save_pred(pred, sv_path, name)
if index % 100 == 0:
logging.info('processing: %d images' % index)
pos = confusion_matrix.sum(1)
res = confusion_matrix.sum(0)
tp = np.diag(confusion_matrix)
IoU_array = (tp / np.maximum(1.0, pos + res - tp))
mean_IoU = IoU_array.mean()
logging.info('mIoU: %.4f' % (mean_IoU))
pos = confusion_matrix.sum(1)
res = confusion_matrix.sum(0)
tp = np.diag(confusion_matrix)
pixel_acc = tp.sum()/pos.sum()
mean_acc = (tp/np.maximum(1.0, pos)).mean()
IoU_array = (tp / np.maximum(1.0, pos + res - tp))
mean_IoU = IoU_array.mean()
return mean_IoU, IoU_array, pixel_acc, mean_acc
def test(config, test_dataset, testloader, model,
sv_dir='', sv_pred=True):
model.eval()
with torch.no_grad():
for _, batch in enumerate(tqdm(testloader)):
image, size, name = batch
size = size[0]
pred = test_dataset.multi_scale_inference(
model,
image,
scales=config.TEST.SCALE_LIST,
flip=config.TEST.FLIP_TEST)
if pred.size()[-2] != size[0] or pred.size()[-1] != size[1]:
pred = F.upsample(pred, (size[-2], size[-1]),
mode='bilinear')
if sv_pred:
sv_path = os.path.join(sv_dir,'test_results')
if not os.path.exists(sv_path):
os.mkdir(sv_path)
test_dataset.save_pred(pred, sv_path, name)