-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathinference.py
164 lines (136 loc) · 6.34 KB
/
inference.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
"""
Visualise detected human-object interactions and
the cross-attention weights.
Fred Zhang <frederic.zhang@anu.edu.au>
The Australian National University
Australian Centre for Robotic Vision
"""
import os
import math
import torch
import pocket
import pocket.advis
import warnings
import argparse
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.patheffects as peff
from utils import DataFactory
from pvic import build_detector
from configs import base_detector_args, advanced_detector_args
warnings.filterwarnings("ignore")
def draw_boxes(ax, boxes):
xy = boxes[:, :2].unbind(0)
h, w = (boxes[:, 2:] - boxes[:, :2]).unbind(1)
for i, (a, b, c) in enumerate(zip(xy, h.tolist(), w.tolist())):
patch = patches.Rectangle(a.tolist(), b, c, facecolor='none', edgecolor='w')
ax.add_patch(patch)
txt = plt.text(*a.tolist(), str(i+1), fontsize=20, fontweight='semibold', color='w')
txt.set_path_effects([peff.withStroke(linewidth=5, foreground='#000000')])
plt.draw()
def visualise_entire_image(image, output, attn, action=None, thresh=0.2):
"""Visualise bounding box pairs in the whole image by classes"""
# Rescale the boxes to original image size
ow, oh = image.size
h, w = output['size']
scale_fct = torch.as_tensor([
ow / w, oh / h, ow / w, oh / h
]).unsqueeze(0)
boxes = output['boxes'] * scale_fct
image_copy = image.copy()
scores = output['scores']
pred = output['labels']
# Visualise detected human-object pairs with attached scores
if action is not None:
keep = torch.nonzero(torch.logical_and(scores >= thresh, pred == action)).squeeze(1)
bx_h, bx_o = boxes[output['pairing']].unbind(1)
pocket.utils.draw_box_pairs(image, bx_h[keep], bx_o[keep], width=5)
plt.imshow(image)
plt.axis('off')
for i in range(len(keep)):
txt = plt.text(*bx_h[keep[i], :2], f"{scores[keep[i]]:.2f}", fontsize=15, fontweight='semibold', color='w')
txt.set_path_effects([peff.withStroke(linewidth=5, foreground='#000000')])
plt.draw()
plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0)
plt.margins(0, 0)
plt.gca().xaxis.set_major_locator(plt.NullLocator())
plt.gca().yaxis.set_major_locator(plt.NullLocator())
plt.savefig("fig.png", bbox_inches="tight", pad_inches=0)
for i in keep:
ho_pair_idx = output["x"][i]
attn_map = attn[0, :, ho_pair_idx].reshape(8, math.ceil(h / 32), math.ceil(w / 32))
attn_image = image_copy.copy()
pocket.utils.draw_boxes(attn_image, torch.stack([bx_h[i], bx_o[i]]), width=4)
if args.avg_attn:
pocket.advis.heatmap(attn_image, attn_map.mean(0, keepdim=True), save_path=f"pair_{i}_avg_attn.png")
plt.close()
else:
for j in range(8):
pocket.advis.heatmap(attn_image, attn_map[j: j+1], save_path=f"pair_{i}_attn_head_{j+1}.png")
plt.close()
@torch.no_grad()
def main(args):
dataset = DataFactory(name=args.dataset, partition=args.partition, data_root=args.data_root)
conversion = dataset.dataset.object_to_verb if args.dataset == 'hicodet' \
else list(dataset.dataset.object_to_action.values())
args.num_verbs = 117 if args.dataset == 'hicodet' else 24
model = build_detector(args, conversion)
model.eval()
attn_weights = []
hook = model.decoder.layers[-1].qk_attn.register_forward_hook(
lambda self, input, output: attn_weights.append(output[1])
)
if os.path.exists(args.resume):
print(f"=> Continue from saved checkpoint {args.resume}")
checkpoint = torch.load(args.resume, map_location='cpu')
model.load_state_dict(checkpoint['model_state_dict'])
else:
print(f"=> Start from a randomly initialised model")
if args.image_path is None:
image, _ = dataset[args.index]
output = model([image])
image = dataset.dataset.load_image(
os.path.join(dataset.dataset._root,
dataset.dataset.filename(args.index)
))
else:
image = dataset.dataset.load_image(args.image_path)
image_tensor, _ = dataset.transforms(image, None)
output = model([image_tensor])
hook.remove()
visualise_entire_image(
image, output[0], attn_weights[0],
args.action, args.action_score_thresh
)
if __name__ == "__main__":
if "DETR" not in os.environ:
raise KeyError(f"Specify the detector type with env. variable \"DETR\".")
elif os.environ["DETR"] == "base":
parser = argparse.ArgumentParser(parents=[base_detector_args(),])
parser.add_argument('--detector', default='base', type=str)
parser.add_argument('--raw-lambda', default=2.8, type=float)
elif os.environ["DETR"] == "advanced":
parser = argparse.ArgumentParser(parents=[advanced_detector_args(),])
parser.add_argument('--detector', default='advanced', type=str)
parser.add_argument('--raw-lambda', default=1.7, type=float)
parser.add_argument('--partition', type=str, default="test2015")
parser.add_argument('--kv-src', default='C5', type=str, choices=['C5', 'C4', 'C3'])
parser.add_argument('--repr-dim', default=384, type=int)
parser.add_argument('--triplet-enc-layers', default=1, type=int)
parser.add_argument('--triplet-dec-layers', default=2, type=int)
parser.add_argument('--alpha', default=.5, type=float)
parser.add_argument('--gamma', default=.1, type=float)
parser.add_argument('--box-score-thresh', default=.05, type=float)
parser.add_argument('--min-instances', default=3, type=int)
parser.add_argument('--max-instances', default=15, type=int)
parser.add_argument('--avg-attn', action='store_true', default=False)
parser.add_argument('--resume', default='', help='Resume from a model')
parser.add_argument('--index', default=0, type=int)
parser.add_argument('--action', default=None, type=int,
help="Index of the action class to visualise.")
parser.add_argument('--action-score-thresh', default=0.2, type=float,
help="Threshold on action classes.")
parser.add_argument('--image-path', default=None, type=str,
help="Path to an image file.")
args = parser.parse_args()
main(args)