-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathutils.py
287 lines (246 loc) · 10.1 KB
/
utils.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
"""
Utilities
Fred Zhang <frederic.zhang@anu.edu.au>
The Australian National University
Australian Centre for Robotic Vision
"""
import os
import json
import time
import torch
import numpy as np
from tqdm import tqdm
import torch.distributed as dist
from torch.utils.data import Dataset
from torchvision.ops.boxes import box_iou
from torchvision.transforms.functional import hflip
from vcoco.vcoco import VCOCO
from hicodet.hicodet import HICODet
import pocket
from pocket.core import DistributedLearningEngine
from pocket.utils import DetectionAPMeter, HandyTimer, BoxPairAssociation, all_gather
def custom_collate(batch):
images = []
detections = []
targets = []
for im, det, tar in batch:
images.append(im)
detections.append(det)
targets.append(tar)
return images, detections, targets
class DataFactory(Dataset):
def __init__(self,
name, partition,
data_root, detection_root,
flip=False,
box_score_thresh_h=0.2,
box_score_thresh_o=0.2
):
if name not in ['hicodet', 'vcoco']:
raise ValueError("Unknown dataset ", name)
if name == 'hicodet':
assert partition in ['train2015', 'test2015'], \
"Unknown HICO-DET partition " + partition
self.dataset = HICODet(
root=os.path.join(data_root, 'hico_20160224_det/images', partition),
anno_file=os.path.join(data_root, 'instances_{}.json'.format(partition)),
target_transform=pocket.ops.ToTensor(input_format='dict')
)
self.human_idx = 49
else:
assert partition in ['train', 'val', 'trainval', 'test'], \
"Unknown V-COCO partition " + partition
image_dir = dict(
train='mscoco2014/train2014',
val='mscoco2014/train2014',
trainval='mscoco2014/train2014',
test='mscoco2014/val2014'
)
self.dataset = VCOCO(
root=os.path.join(data_root, image_dir[partition]),
anno_file=os.path.join(data_root, 'instances_vcoco_{}.json'.format(partition)
), target_transform=pocket.ops.ToTensor(input_format='dict')
)
self.human_idx = 1
self.name = name
self.detection_root = detection_root
self.box_score_thresh_h = box_score_thresh_h
self.box_score_thresh_o = box_score_thresh_o
self._flip = torch.randint(0, 2, (len(self.dataset),)) if flip \
else torch.zeros(len(self.dataset))
def __len__(self):
return len(self.dataset)
def filter_detections(self, detection):
"""Perform NMS and remove low scoring examples"""
boxes = torch.as_tensor(detection['boxes'])
labels = torch.as_tensor(detection['labels'])
scores = torch.as_tensor(detection['scores'])
# Filter out low scoring human boxes
idx = torch.nonzero(labels == self.human_idx).squeeze(1)
keep_idx = idx[torch.nonzero(scores[idx] >= self.box_score_thresh_h).squeeze(1)]
# Filter out low scoring object boxes
idx = torch.nonzero(labels != self.human_idx).squeeze(1)
keep_idx = torch.cat([
keep_idx,
idx[torch.nonzero(scores[idx] >= self.box_score_thresh_o).squeeze(1)]
])
boxes = boxes[keep_idx].view(-1, 4)
scores = scores[keep_idx].view(-1)
labels = labels[keep_idx].view(-1)
return dict(boxes=boxes, labels=labels, scores=scores)
def flip_boxes(self, detection, target, w):
detection['boxes'] = pocket.ops.horizontal_flip_boxes(w, detection['boxes'])
target['boxes_h'] = pocket.ops.horizontal_flip_boxes(w, target['boxes_h'])
target['boxes_o'] = pocket.ops.horizontal_flip_boxes(w, target['boxes_o'])
def __getitem__(self, i):
image, target = self.dataset[i]
if self.name == 'hicodet':
target['labels'] = target['verb']
# Convert ground truth boxes to zero-based index and the
# representation from pixel indices to coordinates
target['boxes_h'][:, :2] -= 1
target['boxes_o'][:, :2] -= 1
else:
target['labels'] = target['actions']
target['object'] = target.pop('objects')
detection_path = os.path.join(
self.detection_root,
self.dataset.filename(i).replace('jpg', 'json')
)
with open(detection_path, 'r') as f:
detection = pocket.ops.to_tensor(json.load(f),
input_format='dict')
if self._flip[i]:
image = hflip(image)
w, _ = image.size
self.flip_boxes(detection, target, w)
image = pocket.ops.to_tensor(image, 'pil')
return image, detection, target
def test(net, test_loader):
testset = test_loader.dataset.dataset
associate = BoxPairAssociation(min_iou=0.5)
meter = DetectionAPMeter(
600, nproc=1,
num_gt=testset.anno_interaction,
algorithm='11P'
)
net.eval()
for batch in tqdm(test_loader):
inputs = pocket.ops.relocate_to_cuda(batch[:-1])
with torch.no_grad():
output = net(*inputs)
if output is None:
continue
# Batch size is fixed as 1 for inference
assert len(output) == 1, "Batch size is not 1"
output = pocket.ops.relocate_to_cpu(output[0])
target = batch[-1][0]
# Format detections
box_idx = output['index']
boxes_h = output['boxes_h'][box_idx]
boxes_o = output['boxes_o'][box_idx]
objects = output['object'][box_idx]
scores = output['scores']
verbs = output['prediction']
interactions = torch.tensor([
testset.object_n_verb_to_interaction[o][v]
for o, v in zip(objects, verbs)
])
# Associate detected pairs with ground truth pairs
labels = torch.zeros_like(scores)
unique_hoi = interactions.unique()
for hoi_idx in unique_hoi:
gt_idx = torch.nonzero(target['hoi'] == hoi_idx).squeeze(1)
det_idx = torch.nonzero(interactions == hoi_idx).squeeze(1)
if len(gt_idx):
labels[det_idx] = associate(
(target['boxes_h'][gt_idx].view(-1, 4),
target['boxes_o'][gt_idx].view(-1, 4)),
(boxes_h[det_idx].view(-1, 4),
boxes_o[det_idx].view(-1, 4)),
scores[det_idx].view(-1)
)
meter.append(scores, interactions, labels)
return meter.eval()
class CustomisedDLE(DistributedLearningEngine):
def __init__(self, net, train_loader, val_loader, num_classes=117, **kwargs):
super().__init__(net, None, train_loader, **kwargs)
self.val_loader = val_loader
self.num_classes = num_classes
def _on_start(self):
self.meter = DetectionAPMeter(self.num_classes, algorithm='11P')
self.hoi_loss = pocket.utils.SyncedNumericalMeter(maxlen=self._print_interval)
self.intr_loss = pocket.utils.SyncedNumericalMeter(maxlen=self._print_interval)
def _on_each_iteration(self):
self._state.optimizer.zero_grad()
output = self._state.net(
*self._state.inputs, targets=self._state.targets)
loss_dict = output.pop()
if loss_dict['hoi_loss'].isnan():
raise ValueError(f"The HOI loss is NaN for rank {self._rank}")
self._state.loss = sum(loss for loss in loss_dict.values())
self._state.loss.backward()
self._state.optimizer.step()
self.hoi_loss.append(loss_dict['hoi_loss'])
self.intr_loss.append(loss_dict['interactiveness_loss'])
self._synchronise_and_log_results(output, self.meter)
def _on_end_epoch(self):
timer = HandyTimer(maxlen=2)
# Compute training mAP
if self._rank == 0:
with timer:
ap_train = self.meter.eval()
# Run validation and compute mAP
with timer:
ap_val = self.validate()
# Print performance and time
if self._rank == 0:
print("Epoch: {} | training mAP: {:.4f}, evaluation time: {:.2f}s |"
"validation mAP: {:.4f}, total time: {:.2f}s\n".format(
self._state.epoch, ap_train.mean().item(), timer[0],
ap_val.mean().item(), timer[1]
))
self.meter.reset()
super()._on_end_epoch()
def _print_statistics(self):
super()._print_statistics()
hoi_loss = self.hoi_loss.mean()
intr_loss = self.intr_loss.mean()
if self._rank == 0:
print(f"=> HOI classification loss: {hoi_loss:.4f},",
f"interactiveness loss: {intr_loss:.4f}")
self.hoi_loss.reset()
self.intr_loss.reset()
def _synchronise_and_log_results(self, output, meter):
scores = []; pred = []; labels = []
# Collate results within the batch
for result in output:
scores.append(result['scores'].detach().cpu().numpy())
pred.append(result['prediction'].cpu().float().numpy())
labels.append(result["labels"].cpu().numpy())
# Sync across subprocesses
all_results = np.stack([
np.concatenate(scores),
np.concatenate(pred),
np.concatenate(labels)
])
all_results_sync = all_gather(all_results)
# Collate and log results in master process
if self._rank == 0:
scores, pred, labels = torch.from_numpy(
np.concatenate(all_results_sync, axis=1)
).unbind(0)
meter.append(scores, pred, labels)
@torch.no_grad()
def validate(self):
meter = DetectionAPMeter(self.num_classes, algorithm='11P')
self._state.net.eval()
for batch in self.val_loader:
inputs = pocket.ops.relocate_to_cuda(batch)
results = self._state.net(*inputs)
self._synchronise_and_log_results(results, meter)
# Evaluate mAP in master process
if self._rank == 0:
return meter.eval()
else:
return None