-
Notifications
You must be signed in to change notification settings - Fork 5
/
engine.py
178 lines (141 loc) · 6.58 KB
/
engine.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
"""
Train and eval functions used in main.py
"""
import math
import os
import sys
from typing import Iterable
import numpy as np
import cv2
import torch
import torchvision.transforms as standard_transforms
import torch.nn.functional as F
import util.misc as utils
from util.misc import NestedTensor
class DeNormalize(object):
def __init__(self, mean, std):
self.mean = mean
self.std = std
def __call__(self, tensor):
for t, m, s in zip(tensor, self.mean, self.std):
t.mul_(s).add_(m)
return tensor
def visualization(samples, targets, pred, vis_dir, split_map=None):
"""
Visualize predictions
"""
gts = [t['points'].tolist() for t in targets]
pil_to_tensor = standard_transforms.ToTensor()
restore_transform = standard_transforms.Compose([
DeNormalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
standard_transforms.ToPILImage()
])
images = samples.tensors
masks = samples.mask
for idx in range(images.shape[0]):
sample = restore_transform(images[idx])
sample = pil_to_tensor(sample.convert('RGB')).numpy() * 255
sample_vis = sample.transpose([1, 2, 0])[:, :, ::-1].astype(np.uint8).copy()
# draw ground-truth points (red)
size = 2
for t in gts[idx]:
sample_vis = cv2.circle(sample_vis, (int(t[1]), int(t[0])), size, (0, 0, 255), -1)
# draw predictions (green)
for p in pred[idx]:
sample_vis = cv2.circle(sample_vis, (int(p[1]), int(p[0])), size, (0, 255, 0), -1)
# draw split map
if split_map is not None:
imgH, imgW = sample_vis.shape[:2]
split_map = (split_map * 255).astype(np.uint8)
split_map = cv2.applyColorMap(split_map, cv2.COLORMAP_JET)
split_map = cv2.resize(split_map, (imgW, imgH), interpolation=cv2.INTER_NEAREST)
sample_vis = split_map * 0.9 + sample_vis
# save image
if vis_dir is not None:
# eliminate invalid area
imgH, imgW = masks.shape[-2:]
valid_area = torch.where(~masks[idx])
valid_h, valid_w = valid_area[0][-1], valid_area[1][-1]
sample_vis = sample_vis[:valid_h+1, :valid_w+1]
name = targets[idx]['image_path'].split('/')[-1].split('.')[0]
cv2.imwrite(os.path.join(vis_dir, '{}_gt{}_pred{}.jpg'.format(name, len(gts[idx]), len(pred[idx]))), sample_vis)
# training
def train_one_epoch(model: torch.nn.Module, criterion: torch.nn.Module,
data_loader: Iterable, optimizer: torch.optim.Optimizer,
device: torch.device, epoch: int, max_norm: float = 0):
model.train()
criterion.train()
metric_logger = utils.MetricLogger(delimiter=" ")
metric_logger.add_meter('lr', utils.SmoothedValue(window_size=1, fmt='{value:.6f}'))
header = 'Epoch: [{}]'.format(epoch)
print_freq = 10
for samples, targets in metric_logger.log_every(data_loader, print_freq, header):
samples = samples.to(device)
targets = [{k: v.to(device) for k, v in t.items()} for t in targets]
gt_points = [target['points'] for target in targets]
outputs = model(samples, epoch=epoch, train=True,
criterion=criterion, targets=targets)
loss_dict, weight_dict, losses = outputs['loss_dict'], outputs['weight_dict'], outputs['losses']
# reduce losses over all GPUs for logging purposes
loss_dict_reduced = utils.reduce_dict(loss_dict)
loss_dict_reduced_unscaled = {f'{k}_unscaled': v
for k, v in loss_dict_reduced.items()}
loss_dict_reduced_scaled = {k: v * weight_dict[k]
for k, v in loss_dict_reduced.items() if k in weight_dict}
losses_reduced_scaled = sum(loss_dict_reduced_scaled.values())
loss_value = losses_reduced_scaled.item()
if not math.isfinite(loss_value):
print("Loss is {}, stopping training".format(loss_value))
print(loss_dict_reduced)
sys.exit(1)
optimizer.zero_grad()
losses.backward()
if max_norm > 0:
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm)
optimizer.step()
metric_logger.update(loss=loss_value, **loss_dict_reduced_scaled, **loss_dict_reduced_unscaled)
metric_logger.update(lr=optimizer.param_groups[0]["lr"])
# gather the stats from all processes
metric_logger.synchronize_between_processes()
print("Averaged stats:", metric_logger)
return {k: meter.global_avg for k, meter in metric_logger.meters.items()}
# evaluation
@torch.no_grad()
def evaluate(model, data_loader, device, epoch=0, vis_dir=None):
model.eval()
metric_logger = utils.MetricLogger(delimiter=" ")
header = 'Test:'
if vis_dir is not None:
os.makedirs(vis_dir, exist_ok=True)
print_freq = 10
for samples, targets in metric_logger.log_every(data_loader, print_freq, header):
samples = samples.to(device)
img_h, img_w = samples.tensors.shape[-2:]
# inference
outputs = model(samples, test=True, targets=targets)
outputs_scores = torch.nn.functional.softmax(outputs['pred_logits'], -1)[:, :, 1][0]
outputs_points = outputs['pred_points'][0]
outputs_offsets = outputs['pred_offsets'][0]
# process predicted points
predict_cnt = len(outputs_scores)
gt_cnt = targets[0]['points'].shape[0]
# compute error
mae = abs(predict_cnt - gt_cnt)
mse = (predict_cnt - gt_cnt) * (predict_cnt - gt_cnt)
# record results
results = {}
toTensor = lambda x: torch.tensor(x).float().cuda()
results['mae'], results['mse'] = toTensor(mae), toTensor(mse)
metric_logger.update(mae=results['mae'], mse=results['mse'])
results_reduced = utils.reduce_dict(results)
metric_logger.update(mae=results_reduced['mae'], mse=results_reduced['mse'])
# visualize predictions
if vis_dir:
points = [[point[0]*img_h, point[1]*img_w] for point in outputs_points] # recover to actual points
split_map = (outputs['split_map_raw'][0].detach().cpu().squeeze(0) > 0.5).float().numpy()
visualization(samples, targets, [points], vis_dir, split_map=split_map)
# gather the stats from all processes
metric_logger.synchronize_between_processes()
results = {k: meter.global_avg for k, meter in metric_logger.meters.items()}
results['mse'] = np.sqrt(results['mse'])
return results