-
Notifications
You must be signed in to change notification settings - Fork 24
/
upt.py
288 lines (244 loc) · 10.1 KB
/
upt.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
"""
Unary-pairwise transformer for human-object interaction detection
Fred Zhang <frederic.zhang@anu.edu.au>
The Australian National University
Australian Centre for Robotic Vision
"""
import os
import torch
import torch.distributed as dist
from torch import nn, Tensor
from typing import Optional, List
from torchvision.ops.boxes import batched_nms, box_iou
from ops import binary_focal_loss_with_logits
from interaction_head import InteractionHead
import sys
sys.path.append('detr')
from models import build_model
from util import box_ops
from util.misc import nested_tensor_from_tensor_list
class UPT(nn.Module):
"""
Unary-pairwise transformer
Parameters:
-----------
detector: nn.Module
Object detector (DETR)
postprocessor: nn.Module
Postprocessor for the object detector
interaction_head: nn.Module
Interaction head of the network
human_idx: int
Index of the human class
num_classes: int
Number of action classes
alpha: float
Hyper-parameter in the focal loss
gamma: float
Hyper-parameter in the focal loss
box_score_thresh: float
Threshold used to eliminate low-confidence objects
fg_iou_thresh: float
Threshold used to associate detections with ground truth
min_instances: float
Minimum number of instances (human or object) to sample
max_instances: float
Maximum number of instances (human or object) to sample
"""
def __init__(self,
detector: nn.Module,
postprocessor: nn.Module,
interaction_head: nn.Module,
human_idx: int, num_classes: int,
alpha: float = 0.5, gamma: float = 2.0,
box_score_thresh: float = 0.2, fg_iou_thresh: float = 0.5,
min_instances: int = 3, max_instances: int = 15,
) -> None:
super().__init__()
self.detector = detector
self.postprocessor = postprocessor
self.interaction_head = interaction_head
self.human_idx = human_idx
self.num_classes = num_classes
self.alpha = alpha
self.gamma = gamma
self.box_score_thresh = box_score_thresh
self.fg_iou_thresh = fg_iou_thresh
self.min_instances = min_instances
self.max_instances = max_instances
def recover_boxes(self, boxes, size):
boxes = box_ops.box_cxcywh_to_xyxy(boxes)
h, w = size
scale_fct = torch.stack([w, h, w, h])
boxes = boxes * scale_fct
return boxes
def associate_with_ground_truth(self, boxes_h, boxes_o, targets):
n = boxes_h.shape[0]
labels = torch.zeros(n, self.num_classes, device=boxes_h.device)
gt_bx_h = self.recover_boxes(targets['boxes_h'], targets['size'])
gt_bx_o = self.recover_boxes(targets['boxes_o'], targets['size'])
x, y = torch.nonzero(torch.min(
box_iou(boxes_h, gt_bx_h),
box_iou(boxes_o, gt_bx_o)
) >= self.fg_iou_thresh).unbind(1)
labels[x, targets['labels'][y]] = 1
return labels
def compute_interaction_loss(self, boxes, bh, bo, logits, prior, targets):
labels = torch.cat([
self.associate_with_ground_truth(bx[h], bx[o], target)
for bx, h, o, target in zip(boxes, bh, bo, targets)
])
prior = torch.cat(prior, dim=1).prod(0)
x, y = torch.nonzero(prior).unbind(1)
logits = logits[x, y]; prior = prior[x, y]; labels = labels[x, y]
n_p = len(torch.nonzero(labels))
if dist.is_initialized():
world_size = dist.get_world_size()
n_p = torch.as_tensor([n_p], device='cuda')
dist.barrier()
dist.all_reduce(n_p)
n_p = (n_p / world_size).item()
loss = binary_focal_loss_with_logits(
torch.log(
prior / (1 + torch.exp(-logits) - prior) + 1e-8
), labels, reduction='sum',
alpha=self.alpha, gamma=self.gamma
)
return loss / n_p
def prepare_region_proposals(self, results, hidden_states):
region_props = []
for res, hs in zip(results, hidden_states):
sc, lb, bx = res.values()
keep = batched_nms(bx, sc, lb, 0.5)
sc = sc[keep].view(-1)
lb = lb[keep].view(-1)
bx = bx[keep].view(-1, 4)
hs = hs[keep].view(-1, 256)
keep = torch.nonzero(sc >= self.box_score_thresh).squeeze(1)
is_human = lb == self.human_idx
hum = torch.nonzero(is_human).squeeze(1)
obj = torch.nonzero(is_human == 0).squeeze(1)
n_human = is_human[keep].sum(); n_object = len(keep) - n_human
# Keep the number of human and object instances in a specified interval
if n_human < self.min_instances:
keep_h = sc[hum].argsort(descending=True)[:self.min_instances]
keep_h = hum[keep_h]
elif n_human > self.max_instances:
keep_h = sc[hum].argsort(descending=True)[:self.max_instances]
keep_h = hum[keep_h]
else:
keep_h = torch.nonzero(is_human[keep]).squeeze(1)
keep_h = keep[keep_h]
if n_object < self.min_instances:
keep_o = sc[obj].argsort(descending=True)[:self.min_instances]
keep_o = obj[keep_o]
elif n_object > self.max_instances:
keep_o = sc[obj].argsort(descending=True)[:self.max_instances]
keep_o = obj[keep_o]
else:
keep_o = torch.nonzero(is_human[keep] == 0).squeeze(1)
keep_o = keep[keep_o]
keep = torch.cat([keep_h, keep_o])
region_props.append(dict(
boxes=bx[keep],
scores=sc[keep],
labels=lb[keep],
hidden_states=hs[keep]
))
return region_props
def postprocessing(self, boxes, bh, bo, logits, prior, objects, attn_maps, image_sizes):
n = [len(b) for b in bh]
logits = logits.split(n)
detections = []
for bx, h, o, lg, pr, obj, attn, size in zip(
boxes, bh, bo, logits, prior, objects, attn_maps, image_sizes
):
pr = pr.prod(0)
x, y = torch.nonzero(pr).unbind(1)
scores = torch.sigmoid(lg[x, y])
detections.append(dict(
boxes=bx, pairing=torch.stack([h[x], o[x]]),
scores=scores * pr[x, y], labels=y,
objects=obj[x], attn_maps=attn, size=size
))
return detections
def forward(self,
images: List[Tensor],
targets: Optional[List[dict]] = None
) -> List[dict]:
"""
Parameters:
-----------
images: List[Tensor]
Input images in format (C, H, W)
targets: List[dict], optional
Human-object interaction targets
Returns:
--------
results: List[dict]
Detected human-object interactions. Each dict has the following keys:
`boxes`: torch.Tensor
(N, 4) Bounding boxes for detected human and object instances
`pairing`: torch.Tensor
(2, M) Pairing indices, with human instance preceding the object instance
`scores`: torch.Tensor
(M,) Interaction score for each pair
`labels`: torch.Tensor
(M,) Predicted action class for each pair
`objects`: torch.Tensor
(M,) Predicted object class for each pair
`attn_maps`: list
Attention weights in the cooperative and competitive layers
`size`: torch.Tensor
(2,) Image height and width
"""
if self.training and targets is None:
raise ValueError("In training mode, targets should be passed")
image_sizes = torch.as_tensor([
im.size()[-2:] for im in images
], device=images[0].device)
if isinstance(images, (list, torch.Tensor)):
images = nested_tensor_from_tensor_list(images)
features, pos = self.detector.backbone(images)
src, mask = features[-1].decompose()
assert mask is not None
hs = self.detector.transformer(self.detector.input_proj(src), mask, self.detector.query_embed.weight, pos[-1])[0]
outputs_class = self.detector.class_embed(hs)
outputs_coord = self.detector.bbox_embed(hs).sigmoid()
results = {'pred_logits': outputs_class[-1], 'pred_boxes': outputs_coord[-1]}
results = self.postprocessor(results, image_sizes)
region_props = self.prepare_region_proposals(results, hs[-1])
logits, prior, bh, bo, objects, attn_maps = self.interaction_head(
features[-1].tensors, image_sizes, region_props
)
boxes = [r['boxes'] for r in region_props]
if self.training:
interaction_loss = self.compute_interaction_loss(boxes, bh, bo, logits, prior, targets)
loss_dict = dict(
interaction_loss=interaction_loss
)
return loss_dict
detections = self.postprocessing(boxes, bh, bo, logits, prior, objects, attn_maps, image_sizes)
return detections
def build_detector(args, class_corr):
detr, _, postprocessors = build_model(args)
if os.path.exists(args.pretrained):
if dist.get_rank() == 0:
print(f"Load weights for the object detector from {args.pretrained}")
detr.load_state_dict(torch.load(args.pretrained, map_location='cpu')['model_state_dict'])
predictor = torch.nn.Linear(args.repr_dim * 2, args.num_classes)
interaction_head = InteractionHead(
predictor, args.hidden_dim, args.repr_dim,
detr.backbone[0].num_channels,
args.num_classes, args.human_idx, class_corr
)
detector = UPT(
detr, postprocessors['bbox'], interaction_head,
human_idx=args.human_idx, num_classes=args.num_classes,
alpha=args.alpha, gamma=args.gamma,
box_score_thresh=args.box_score_thresh,
fg_iou_thresh=args.fg_iou_thresh,
min_instances=args.min_instances,
max_instances=args.max_instances,
)
return detector