forked from ltttpku/CMMP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference.py
executable file
·438 lines (383 loc) · 20.7 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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
"""
Visualise detected human-object interactions in an image
Fred Zhang <[email protected]>
The Australian National University
Australian Centre for Robotic Vision
"""
import os
import torch
import pocket
import warnings
import argparse
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.patheffects as peff
from mpl_toolkits.axes_grid1 import make_axes_locatable
# from utils import DataFactory
from utils_tip_cache_and_union_finetune import custom_collate, CustomisedDLE, DataFactory
# from upt import build_detector
from upt_tip_cache_model_free_finetune_distill3 import build_detector
import pdb
import random
from pocket.ops import relocate_to_cpu, relocate_to_cuda
warnings.filterwarnings("ignore")
OBJECTS = [
"person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat",
"traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat",
"dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack",
"umbrella", "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball",
"kite", "baseball bat", "baseball glove", "skateboard", "surfboard", "tennis racket",
"bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich",
"orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch",
"potted plant", "bed", "dining table", "toilet", "tv", "laptop", "mouse", "remote", "keyboard",
"cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock",
"vase", "scissors", "teddy bear", "hair drier", "toothbrush"
]
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, actions, action=None, thresh=0.2, save_filename=None, failure=False):
"""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
# Find the number of human and object instances
nh = len(output['pairing'][0].unique()); no = len(boxes)
scores = output['scores']
objects = output['objects']
pred = output['labels']
# Visualise detected human-object pairs with attached scores
# pdb.set_trace()
unique_actions = torch.unique(pred)
if action is not None:
plt.cla()
if failure:
keep = torch.nonzero(torch.logical_and(scores < thresh, pred == action)).squeeze(1)
else:
keep = torch.nonzero(torch.logical_and(scores >= thresh, pred == action)).squeeze(1)
bx_h, bx_o = boxes[output['pairing']].unbind(0)
pocket.utils.draw_box_pairs(image, bx_h[keep], bx_o[keep], width=5)
plt.imshow(image)
plt.axis('off')
# pdb.set_trace()
if len(keep) == 0: return
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.show()
plt.savefig(save_filename, bbox_inches='tight', pad_inches=0.0)
# plt.savefig(save_filename)
plt.cla()
return
pairing = output['pairing']
# coop_attn = output['attn_maps'][0]
# comp_attn = output['attn_maps'][1]
# Visualise attention from the cooperative layer
# for i, attn_1 in enumerate(coop_attn):
# fig, axe = plt.subplots(2, 4)
# fig.suptitle(f"Attention in coop. layer {i}")
# axe = np.concatenate(axe)
# ticks = list(range(attn_1[0].shape[0]))
# labels = [v + 1 for v in ticks]
# for ax, attn in zip(axe, attn_1):
# im = ax.imshow(attn.squeeze().T, vmin=0, vmax=1)
# divider = make_axes_locatable(ax)
# ax.set_xticks(ticks)
# ax.set_xticklabels(labels)
# ax.set_yticks(ticks)
# ax.set_yticklabels(labels)
# cax = divider.append_axes('right', size='5%', pad=0.05)
# fig.colorbar(im, cax=cax)
# x, y = torch.meshgrid(torch.arange(nh), torch.arange(no))
# x, y = torch.nonzero(x != y).unbind(1)
# pairs = [str((i.item() + 1, j.item() + 1)) for i, j in zip(x, y)]
# Visualise attention from the competitive layer
# fig, axe = plt.subplots(2, 4)
# fig.suptitle("Attention in comp. layer")
# axe = np.concatenate(axe)
# ticks = list(range(len(pairs)))
# for ax, attn in zip(axe, comp_attn):
# im = ax.imshow(attn, vmin=0, vmax=1)
# divider = make_axes_locatable(ax)
# ax.set_xticks(ticks)
# ax.set_xticklabels(pairs, rotation=45)
# ax.set_yticks(ticks)
# ax.set_yticklabels(pairs)
# cax = divider.append_axes('right', size='5%', pad=0.05)
# fig.colorbar(im, cax=cax)
# Print predicted actions and corresponding scores
unique_actions = torch.unique(pred)
for verb in unique_actions:
print(f"\n=> Action: {actions[verb]}")
sample_idx = torch.nonzero(pred == verb).squeeze(1)
for idx in sample_idx:
idxh, idxo = pairing[:, idx] + 1
print(
f"({idxh.item():<2}, {idxo.item():<2}),",
f"score: {scores[idx]:.4f}, object: {OBJECTS[objects[idx]]}."
)
# Draw the bounding boxes
plt.figure()
plt.imshow(image)
plt.axis('off')
ax = plt.gca()
draw_boxes(ax, boxes)
# plt.show()
plt.savefig('visualizations/test.png')
import detr.datasets.transforms_clip as T
def transform(image):
transforms = T.Compose([
T.RandomResize([800], max_size=1333),
])
clip_transforms = T.Compose([
T.IResize([224,224]),
])
normalize = T.Compose([
T.ToTensor(),
T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
image,_ = transforms(image,None)
image_clip,_ = clip_transforms(image,None)
image, _ = normalize(image, None)
image_clip, _ = normalize(image_clip,None)
return (image,image_clip), torch.zeros((3,224,224))
@torch.no_grad()
def main(args):
import torch.distributed as dist
os.environ["MASTER_ADDR"] = "localhost"
os.environ["MASTER_PORT"] = args.port
dist.init_process_group(
backend="nccl",
init_method="env://",
world_size=args.world_size,
rank=0
)
# Fix seed
seed = 1234
torch.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
args.clip_model_name = args.clip_dir_vit.split('/')[-1].split('.')[0]
if args.clip_model_name == 'ViT-B-16':
args.clip_model_name = 'ViT-B/16'
elif args.clip_model_name == 'ViT-L-14-336px':
args.clip_model_name = 'ViT-L/14@336px'
dataset = DataFactory(name=args.dataset, partition=args.partition, data_root=args.data_root, clip_model_name=args.clip_model_name, zero_shot=args.zs, zs_type=args.zs_type, num_classes=args.num_classes)
actions = dataset.dataset.verbs if args.dataset == 'hicodet' else \
dataset.dataset.actions
verb2interaction = None
args.human_idx = 0
object_n_verb_to_interaction = dataset.dataset.object_n_verb_to_interaction
if args.dataset == 'hicodet':
if args.num_classes == 117:
object_to_target = dataset.dataset.object_to_verb
elif args.num_classes == 600:
object_to_target = dataset.dataset.object_to_interaction
if args.zs:
object_to_target = dataset.zs_object_to_target
elif args.dataset == 'vcoco':
if args.num_classes == 24:
object_to_target = list(dataset.dataset.object_to_action.values())
elif args.num_classes == 236:
raise NotImplementedError
print('[INFO]: num_classes', args.num_classes)
if args.dataset == 'vcoco' or args.dataset == 'swig':
num_anno = None
else:
num_anno = torch.as_tensor(dataset.dataset.anno_interaction)
if args.num_classes == 117:
num_anno = torch.as_tensor(dataset.dataset.anno_action)
upt = build_detector(args, object_to_target, object_n_verb_to_interaction=object_n_verb_to_interaction, clip_model_path=args.clip_dir_vit, num_anno=num_anno, verb2interaction=verb2interaction)
if args.dataset == 'hicodet' and args.eval: ## after building model, manually change obj_to_target
if args.num_classes == 117:
upt.object_class_to_target_class = dataset.dataset.object_to_verb
else:
upt.object_class_to_target_class = dataset.dataset.object_to_interaction
upt.eval()
device = torch.device(args.device)
if args.prompt_learning:
upt.init_adapter_union_weight(device)
if os.path.exists(args.resume):
print(f"=> Continue from saved checkpoint {args.resume}")
checkpoint = torch.load(args.resume, map_location='cpu')
upt.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 = upt([image]) # image: Tuple(torch.Size([3, 800, 1204]), torch.Size([3, 224, 224]))
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)
from PIL import Image
# image = np.array(Image.open(args.image_path))
image = Image.open(args.image_path)
image_tensor, _ = transform(image)
output = upt([image_tensor])
visualise_entire_image(image, output[0], actions, action=args.action, thresh=args.action_score_thresh, save_filename=f'visualization/{args.dataset}')
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--d_detr', default=False, type=lambda x: (str(x).lower() == 'true'),)
parser.add_argument('--adapter_pos', type=str, default='all', choices=['all', 'front', 'end', 'random', 'last'])
parser.add_argument('--use_multi_hot', action='store_true')
parser.add_argument('--label_learning', action='store_true')
parser.add_argument('--label_choice', default='random', choices=['random', 'single_first', 'multi_first', 'single+multi', 'rare_first', 'non_rare_first', 'rare+non_rare'])
parser.add_argument('--use_mlp_proj', action='store_true')
parser.add_argument('--eval_trainset', action='store_true')
parser.add_argument('--vision_regularize', action='store_true')
parser.add_argument('--adapter_num_layers', type=int, default=1)
parser.add_argument('--featmap_dropout_rate', type=float, default=0.2)
parser.add_argument('--obj_affordance', action='store_true') ## use affordance embedding of objects
parser.add_argument('--lr-head', default=1e-3, type=float)
parser.add_argument('--lr-vit', default=1e-3, type=float)
parser.add_argument('--batch-size', default=8, type=int)
parser.add_argument('--weight-decay', default=1e-4, type=float)
parser.add_argument('--epochs', default=20, type=int)
parser.add_argument('--lr-drop', default=10, type=int)
parser.add_argument('--clip-max-norm', default=0.1, type=float)
parser.add_argument('--backbone', default='resnet50', type=str)
parser.add_argument('--dilation', action='store_true')
parser.add_argument('--position-embedding', default='sine', type=str, choices=('sine', 'learned'))
parser.add_argument('--repr-dim', default=512, type=int)
parser.add_argument('--hidden-dim', default=256, type=int)
parser.add_argument('--enc-layers', default=6, type=int)
parser.add_argument('--dec-layers', default=6, type=int)
parser.add_argument('--dim-feedforward', default=2048, type=int)
parser.add_argument('--dropout', default=0.1, type=float)
parser.add_argument('--nheads', default=8, type=int)
parser.add_argument('--num-queries', default=100, type=int)
parser.add_argument('--pre-norm', action='store_true')
parser.add_argument('--no-aux-loss', dest='aux_loss', action='store_false')
parser.add_argument('--set-cost-class', default=1, type=float)
parser.add_argument('--set-cost-bbox', default=5, type=float)
parser.add_argument('--set-cost-giou', default=2, type=float)
parser.add_argument('--bbox-loss-coef', default=5, type=float)
parser.add_argument('--giou-loss-coef', default=2, type=float)
parser.add_argument('--eos-coef', default=0.1, type=float,
help="Relative classification weight of the no-object class")
parser.add_argument('--alpha', default=0.5, type=float)
parser.add_argument('--gamma', default=0.2, type=float)
parser.add_argument('--dataset', default='hicodet', type=str)
parser.add_argument('--partition', default='test2015', type=str)
parser.add_argument('--num-workers', default=2, type=int)
parser.add_argument('--data-root', default='./hicodet')
# training parameters
parser.add_argument('--device', default='cpu',
help='device to use for training / testing')
parser.add_argument('--port', default='1233', type=str)
parser.add_argument('--seed', default=66, type=int)
parser.add_argument('--pretrained', default='', help='Path to a pretrained detector')
parser.add_argument('--resume', default='', help='Resume from a model')
parser.add_argument('--output-dir', default='checkpoints')
parser.add_argument('--print-interval', default=500, type=int)
parser.add_argument('--world-size', default=1, type=int)
parser.add_argument('--eval', action='store_true')
parser.add_argument('--cache', action='store_true')
parser.add_argument('--sanity', action='store_true')
parser.add_argument('--box-score-thresh', default=0.2, type=float)
parser.add_argument('--fg-iou-thresh', default=0.5, type=float)
parser.add_argument('--min-instances', default=3, type=int)
parser.add_argument('--max-instances', default=15, type=int)
parser.add_argument('--visual_mode', default='vit', type=str)
# add CLIP model resenet
# parser.add_argument('--clip_dir', default='./checkpoints/pretrained_clip/RN50.pt', type=str)
# parser.add_argument('--clip_visual_layers', default=[3, 4, 6, 3], type=list)
# parser.add_argument('--clip_visual_output_dim', default=1024, type=int)
# parser.add_argument('--clip_visual_input_resolution', default=1344, type=int)
# parser.add_argument('--clip_visual_width', default=64, type=int)
# parser.add_argument('--clip_visual_patch_size', default=64, type=int)
# parser.add_argument('--clip_text_output_dim', default=1024, type=int)
# parser.add_argument('--clip_text_transformer_width', default=512, type=int)
# parser.add_argument('--clip_text_transformer_heads', default=8, type=int)
# parser.add_argument('--clip_text_transformer_layers', default=12, type=int)
# parser.add_argument('--clip_text_context_length', default=13, type=int)
#### add CLIP vision transformer
parser.add_argument('--clip_dir_vit', default='./checkpoints/pretrained_clip/ViT-B-16.pt', type=str)
### ViT-L/14@336px START: emb_dim: 768
# >>> vision_width: 1024, vision_patch_size(conv's kernel-size&&stride-size): 14,
# >>> vision_layers(#layers in vision-transformer): 24 , image_resolution:336;
# >>> transformer_width:768, transformer_layers: 12, transformer_heads:12
parser.add_argument('--clip_visual_layers_vit', default=24, type=list)
parser.add_argument('--clip_visual_output_dim_vit', default=768, type=int)
parser.add_argument('--clip_visual_input_resolution_vit', default=336, type=int)
parser.add_argument('--clip_visual_width_vit', default=1024, type=int)
parser.add_argument('--clip_visual_patch_size_vit', default=14, type=int)
# parser.add_argument('--clip_text_output_dim_vit', default=512, type=int)
parser.add_argument('--clip_text_transformer_width_vit', default=768, type=int)
parser.add_argument('--clip_text_transformer_heads_vit', default=12, type=int)
parser.add_argument('--clip_text_transformer_layers_vit', default=12, type=int)
# ---END----ViT-L/14@336px----END----
### ViT-B-16 START
# parser.add_argument('--clip_visual_layers_vit', default=12, type=list)
# parser.add_argument('--clip_visual_output_dim_vit', default=512, type=int)
# parser.add_argument('--clip_visual_input_resolution_vit', default=224, type=int)
# parser.add_argument('--clip_visual_width_vit', default=768, type=int)
# parser.add_argument('--clip_visual_patch_size_vit', default=16, type=int)
# # parser.add_argument('--clip_text_output_dim_vit', default=512, type=int)
# parser.add_argument('--clip_text_transformer_width_vit', default=512, type=int)
# parser.add_argument('--clip_text_transformer_heads_vit', default=8, type=int)
# parser.add_argument('--clip_text_transformer_layers_vit', default=12, type=int)
# ---END----ViT-B-16-----END-----
parser.add_argument('--clip_text_context_length_vit', default=77, type=int) # 13 -77
parser.add_argument('--use_insadapter', action='store_true')
parser.add_argument('--use_distill', action='store_true')
parser.add_argument('--use_consistloss', action='store_true')
parser.add_argument('--use_mean', action='store_true') # 13 -77
parser.add_argument('--logits_type', default='HO+U+T', type=str) # 13 -77 # text_add_visual, visual
parser.add_argument('--num_shot', default='4', type=int) # 13 -77 # text_add_visual, visual
parser.add_argument('--obj_classifier', action='store_true') #
parser.add_argument('--classifier_loss_w', default=1.0, type=float)
parser.add_argument('--file1', default='union_embeddings_cachemodel_crop_padding_zeros_vitb16.p',type=str)
parser.add_argument('--interactiveness_prob_thres', default=0.1, type=float)
# parser.add_argument('--feature_type', default='hum_obj_uni', type=str)
# parser.add_argument('--use_deformable_attn', action='store_true')
parser.add_argument('--prior_type', type=str, default='cbe', choices=['cbe', 'cb', 'ce', 'be', 'c', 'b', 'e'])
parser.add_argument('--training_set_ratio', type=float, default=1.0)
parser.add_argument('--frozen_weights', type=str, default=None)
parser.add_argument('--zs', action='store_true') ## zero-shot
parser.add_argument('--hyper_lambda', type=float, default=2.8)
parser.add_argument('--use_weight_pred', action='store_true')
parser.add_argument('--zs_type', type=str, default='rare_first', choices=['rare_first', 'non_rare_first', 'unseen_verb'])
parser.add_argument('--domain_transfer', action='store_true')
parser.add_argument('--fill_zs_verb_type', type=int, default=0,) # (for init) 0: random; 1: weighted_sum,
parser.add_argument('--pseudo_label', action='store_true')
parser.add_argument('--tpt', action='store_true')
parser.add_argument('--vis_tor', type=float, default=1.0)
## prompt learning
parser.add_argument('--N_CTX', type=int, default=24) # number of context vectors
parser.add_argument('--CSC', type=bool, default=False) # class-specific context
parser.add_argument('--CTX_INIT', type=str, default='') # initialization words
parser.add_argument('--CLASS_TOKEN_POSITION', type=str, default='end') # # 'middle' or 'end' or 'front'
parser.add_argument('--prompt_learning', action='store_true')
parser.add_argument('--use_templates', action='store_true')
parser.add_argument('--LA', action='store_true') ## Language Aware
parser.add_argument('--LA_weight', default=0.6, type=float) ## Language Aware
parser.add_argument('--feat_mask_type', type=int, default=0,) # 0: dropout(random mask); 1:
parser.add_argument('--num_classes', type=int, default=117,)
parser.add_argument('--prior_method', type=int, default=0) ## 0: instance-wise, 1: pair-wise, 2: learnable
parser.add_argument('--box_proj', type=int, default=0,) ## 0: None; 1: f_u = ROI-feat + MLP(uni-box)
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()
args.failure = False
main(args)