-
Notifications
You must be signed in to change notification settings - Fork 1
/
framework.py
182 lines (140 loc) · 6.82 KB
/
framework.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
import torch
import torch.nn as nn
from torch.autograd import Variable
import os
from tqdm import tqdm
from utils.metrics import IoU
from loss import dice_bce_loss
import copy
import numpy
class Solver:
def __init__(self, net, optimizer, dataset):
self.net = torch.nn.DataParallel(net.cuda(), device_ids=list(range(torch.cuda.device_count())))
self.optimizer = optimizer
self.dataset = dataset
self.loss = dice_bce_loss()
self.metrics = IoU(threshold=0.5)
self.old_lr = optimizer.param_groups[0]["lr"]
def set_input(self, img_batch, mask_batch=None):
self.img = img_batch
self.mask = mask_batch
def data2cuda(self, volatile=False):
if volatile:
with torch.no_grad():
self.img = Variable(self.img.cuda())
else:
self.img = Variable(self.img.cuda())
if self.mask is not None:
if volatile:
with torch.no_grad():
self.mask = Variable(self.mask.cuda())
else:
self.mask = Variable(self.mask.cuda())
def optimize(self):
self.net.train()
self.data2cuda()
self.optimizer.zero_grad()
pred = self.net.forward(self.img)
loss = self.loss(self.mask, pred)
loss.backward()
self.optimizer.step()
batch_iou, intersection, union = self.metrics(self.mask, pred)
return pred, loss.item(), batch_iou, intersection, union
def test_batch(self):
self.net.eval()
self.data2cuda(volatile=True)
pred = self.net.forward(self.img)
loss = self.loss(self.mask, pred)
batch_iou, intersection, union = self.metrics(self.mask, pred)
pred = pred.cpu().data.numpy().squeeze(1)
return pred, loss.item(), batch_iou, intersection, union
def update_lr(self, ratio=1.0):
new_lr = self.old_lr / ratio
for param_group in self.optimizer.param_groups:
param_group["lr"] = new_lr
print("==> update learning rate: %f -> %f" % (self.old_lr, new_lr))
self.old_lr = new_lr
class Framework:
def __init__(self, *args, **kwargs):
self.solver = Solver(*args, **kwargs)
def set_train_dl(self, dataloader):
self.train_dl = dataloader
def set_validation_dl(self, dataloader):
self.validation_dl = dataloader
def set_test_dl(self, dataloader):
self.test_dl = dataloader
def set_save_path(self, save_path):
self.save_path = save_path
def fit(self, epochs, no_optim_epochs=5):
val_best_metrics = test_best_metrics = [0, 0]
no_optim = 0
for epoch in range(1, epochs + 1):
print(f"epoch {epoch}/{epochs}")
train_loss, train_metrics = self.fit_one_epoch(self.train_dl, mode='training')
val_loss, val_metrics = self.fit_one_epoch(self.validation_dl, mode='val')
test_loss, test_metrics = self.fit_one_epoch(self.test_dl, mode='testing')
if val_best_metrics[1] < val_metrics[1]:
val_best_metrics = val_metrics
test_best_metrics = test_metrics
val_best_net = copy.deepcopy(self.solver.net.state_dict())
epoch_val = epoch
no_optim = 0
else:
no_optim += 1
if no_optim > no_optim_epochs:
if self.solver.old_lr < 1e-8:
print('early stop at {epoch} epoch')
break
else:
no_optim = 0
self.solver.update_lr(ratio=10.0)
print(f'train_loss: {train_loss:.4f} train_metrics: {train_metrics}')
print(f' val_loss: {val_loss:.4f} val_metrics: {val_metrics}')
print(f' test_loss: {test_loss:.4f} test_metrics: {test_metrics}')
print('current best epoch:', epoch_val, ',val g_iou:', val_best_metrics[1], ',test g_iou:', test_best_metrics[1])
print('epoch finished')
print()
print("############ Final IoU Results ############")
print('selected epoch: ', epoch_val)
print(' val set: A_IOU ', val_best_metrics[0], ', G_IOU ', val_best_metrics[1])
print('test set: A_IOU ', test_best_metrics[0], ', G_IOU ', test_best_metrics[1])
torch.save(val_best_net, os.path.join(self.save_path, f"epoch{epoch_val}_val{val_best_metrics[1]:.4f}_test{test_best_metrics[1]:.4f}.pth"))
def fit_one_epoch(self, dataloader, mode='training'):
epoch_loss = 0.0
local_batch_iou = 0.0
intersection = []
union = []
dataloader_iter = iter(dataloader)
iter_num = len(dataloader_iter)
progress_bar = tqdm(enumerate(dataloader_iter), total=iter_num)
for i, (img, mask) in progress_bar:
self.solver.set_input(img, mask)
if mode=='training':
pred_map, iter_loss, batch_iou, samples_intersection, samples_union = self.solver.optimize()
else:
pred_map, iter_loss, batch_iou, samples_intersection, samples_union = self.solver.test_batch()
epoch_loss += iter_loss
progress_bar.set_description(f'{mode} iter: {i} loss: {iter_loss:.4f}')
local_batch_iou += batch_iou
samples_intersection = samples_intersection.cpu().data.numpy()
samples_union = samples_union.cpu().data.numpy()
for sample_id in range(len(samples_intersection)):
if samples_union[sample_id] == 0: # the IOU is ignored when its union is 0
continue
intersection.append(samples_intersection[sample_id])
union.append(samples_union[sample_id])
intersection = numpy.array(intersection)
union = numpy.array(union)
'''
In the code[1] of paper[1], average_iou is the mean of the IoU of all batches.
For a fair comparison, we follow code[1] to compute the average_iou in our paper.
However, more strictly, average_iou should be the mean of the IoU of all samples, i.e., average_iou = (intersection/union).mean()
I recommend using global_iou
paper[1]: Leveraging Crowdsourced GPS Data for Road Extraction from Aerial Imagery, CVPR 2019
code[1]: https://github.com/suniique/Leveraging-Crowdsourced-GPS-Data-for-Road-Extraction-from-Aerial-Imagery/blob/master/framework.py#L106
'''
average_iou = local_batch_iou / iter_num
#average_iou = (intersection/union).mean()
global_iou = intersection.sum()/union.sum()
metrics = [average_iou, global_iou]
return epoch_loss, metrics