-
Notifications
You must be signed in to change notification settings - Fork 7
/
STIP_main.py
282 lines (248 loc) · 13.5 KB
/
STIP_main.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
import argparse
import datetime
import json
import random
import time
import multiprocessing
from pathlib import Path
import numpy as np
import torch
from torch.utils.data import DataLoader, DistributedSampler
import src.data.datasets as datasets
import src.util.misc as utils
from src.engine.arg_parser import get_args_parser
from src.data.datasets import build_dataset, get_coco_api_from_dataset
from src.engine.trainer import train_one_epoch
from src.engine import hoi_evaluator, hoi_accumulator
from src.models import build_model
import wandb
from src.engine.evaluator_coco import coco_evaluate
from src.util.logger import print_params, print_args
from collections import OrderedDict
def save_ckpt(args, model_without_ddp, optimizer, lr_scheduler, epoch, filename):
# save_ckpt: function for saving checkpoints
output_dir = Path(args.output_dir)
if args.output_dir:
checkpoint_path = output_dir / f'{filename}.pth'
utils.save_on_master({
'model': model_without_ddp.state_dict(),
'optimizer': optimizer.state_dict(),
'lr_scheduler': lr_scheduler.state_dict(),
'epoch': epoch,
'args': args,
}, checkpoint_path)
def main(args):
utils.init_distributed_mode(args)
if not args.train_detr is not None: # pretrained DETR
print("Freeze weights for detector")
device = torch.device(args.device)
# fix the seed for reproducibility
seed = args.seed + utils.get_rank()
torch.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
# Data Setup
dataset_train = build_dataset(image_set='train', args=args)
dataset_val = build_dataset(image_set='val' if not args.eval else 'test', args=args)
assert dataset_train.num_action() == dataset_val.num_action(), "Number of actions should be the same between splits"
args.num_classes = dataset_train.num_category()
args.num_actions = dataset_train.num_action()
args.action_names = dataset_train.get_actions()
if args.share_enc: args.hoi_enc_layers = args.enc_layers
if args.pretrained_dec: args.hoi_dec_layers = args.dec_layers
if args.dataset_file == 'vcoco':
# Save V-COCO dataset statistics
args.valid_ids = np.array(dataset_train.get_object_label_idx()).nonzero()[0]
args.invalid_ids = np.argwhere(np.array(dataset_train.get_object_label_idx()) == 0).squeeze(1)
args.human_actions = dataset_train.get_human_action()
args.object_actions = dataset_train.get_object_action()
args.num_human_act = dataset_train.num_human_act()
elif args.dataset_file == 'hico-det':
args.valid_obj_ids = dataset_train.get_valid_obj_ids()
args.correct_mat = torch.tensor(dataset_val.correct_mat).to(device)
print_args(args)
if args.distributed:
sampler_train = DistributedSampler(dataset_train, shuffle=True)
sampler_val = DistributedSampler(dataset_val, shuffle=False)
else:
sampler_train = torch.utils.data.RandomSampler(dataset_train)
sampler_val = torch.utils.data.SequentialSampler(dataset_val)
batch_sampler_train = torch.utils.data.BatchSampler(
sampler_train, args.batch_size, drop_last=True)
data_loader_train = DataLoader(dataset_train, batch_sampler=batch_sampler_train,
collate_fn=utils.collate_fn, num_workers=args.num_workers)
data_loader_val = DataLoader(dataset_val, args.batch_size, sampler=sampler_val,
drop_last=False, collate_fn=utils.collate_fn, num_workers=args.num_workers)
# Model Setup
model, criterion, postprocessors = build_model(args)
model.to(device)
model_without_ddp = model
if args.distributed:
model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[args.gpu])
model_without_ddp = model.module
n_parameters = print_params(model)
param_dicts = [
{"params": [p for n, p in model_without_ddp.named_parameters() if "detr" not in n and p.requires_grad]},
{
"params": [p for n, p in model_without_ddp.named_parameters() if ("detr" in n and 'backbone' not in n) and p.requires_grad],
"lr": args.lr * 0.1,
},
{
"params": [p for n, p in model_without_ddp.named_parameters() if ("detr" in n and 'backbone' in n) and p.requires_grad],
"lr": args.lr * 0.01,
},
]
optimizer = torch.optim.AdamW(param_dicts, lr=args.lr, weight_decay=args.weight_decay)
# lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, args.lr_drop)
lr_scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='max', factor=args.reduce_lr_on_plateau_factor, patience=args.reduce_lr_on_plateau_patience, verbose=True)
# Weight Setup
if args.detr_weights is not None:
print(f"Loading detr weights from args.detr_weights={args.detr_weights}")
if args.detr_weights.startswith('https'):
checkpoint = torch.hub.load_state_dict_from_url(
args.detr_weights, map_location='cpu', check_hash=True)
else:
checkpoint = torch.load(args.detr_weights, map_location='cpu')
if 'hico_ft_q16.pth' in args.detr_weights: # hack: for loading hico fine-tuned detr
mapped_state_dict = OrderedDict()
for k, v in checkpoint['model'].items():
if k.startswith('detr.'):
mapped_state_dict[k.replace('detr.', '')] = v
model_without_ddp.detr.load_state_dict(mapped_state_dict)
else:
model_without_ddp.detr.load_state_dict(checkpoint['model'])
if args.resume:
print(f"Loading model weights from args.resume={args.resume}")
if args.resume.startswith('https'):
checkpoint = torch.hub.load_state_dict_from_url(
args.resume, map_location='cpu', check_hash=True)
else:
checkpoint = torch.load(args.resume, map_location='cpu')
model_without_ddp.load_state_dict(checkpoint['model'])
if args.eval:
# test only mode
if args.HOIDet:
if args.dataset_file == 'vcoco':
total_res = hoi_evaluator(args, model, criterion, postprocessors, data_loader_val, device)
sc1, sc2 = hoi_accumulator(args, total_res, True, False)
elif args.dataset_file == 'hico-det':
test_stats = hoi_evaluator(args, model, None, postprocessors, data_loader_val, device)
print(f'| mAP (full)\t\t: {test_stats["mAP"]:.2f}')
print(f'| mAP (rare)\t\t: {test_stats["mAP rare"]:.2f}')
print(f'| mAP (non-rare)\t: {test_stats["mAP non-rare"]:.2f}')
else: raise ValueError(f'dataset {args.dataset_file} is not supported.')
return
else:
# check original detr code
base_ds = get_coco_api_from_dataset(data_loader_val)
test_stats, coco_evaluator = coco_evaluate(model, criterion, postprocessors,
data_loader_val, base_ds, device, args.output_dir)
if args.output_dir:
utils.save_on_master(coco_evaluator.coco_eval["bbox"].eval, args.output_dir / "eval.pth")
return
# stats
scenario1, scenario2 = 0, 0
best_mAP, best_rare, best_non_rare = 0, 0, 0
# add argparse
if args.wandb and utils.get_rank() == 0:
wandb.init(
project=args.project_name,
group=args.group_name,
name=args.run_name,
config=args
)
wandb.watch(model)
# Training starts here!
start_time = time.time()
for epoch in range(args.start_epoch, args.epochs):
if args.distributed:
sampler_train.set_epoch(epoch)
train_stats = train_one_epoch(
model, criterion, data_loader_train, optimizer, device, epoch, args.epochs,
args.clip_max_norm, dataset_file=args.dataset_file, log=args.wandb)
if isinstance(lr_scheduler, torch.optim.lr_scheduler.StepLR): lr_scheduler.step()
# Validation
if args.validate:
print('-'*100)
if args.dataset_file == 'vcoco':
total_res = hoi_evaluator(args, model, criterion, postprocessors, data_loader_val, device)
if utils.get_rank() == 0:
sc1, sc2 = hoi_accumulator(args, total_res, False, args.wandb)
if sc1 > scenario1:
scenario1 = sc1
scenario2 = sc2
save_ckpt(args, model_without_ddp, optimizer, lr_scheduler, epoch, filename='best')
print(f'| Scenario #1 mAP : {sc1:.2f} ({scenario1:.2f})')
print(f'| Scenario #2 mAP : {sc2:.2f} ({scenario2:.2f})')
if isinstance(lr_scheduler, torch.optim.lr_scheduler.ReduceLROnPlateau): lr_scheduler.step(sc1)
elif args.dataset_file == 'hico-det':
test_stats = hoi_evaluator(args, model, None, postprocessors, data_loader_val, device)
if utils.get_rank() == 0:
if test_stats['mAP'] > best_mAP:
best_mAP = test_stats['mAP']
best_rare = test_stats['mAP rare']
best_non_rare = test_stats['mAP non-rare']
save_ckpt(args, model_without_ddp, optimizer, lr_scheduler, epoch, filename='best')
print(f'| mAP (full)\t\t: {test_stats["mAP"]:.2f} ({best_mAP:.2f})')
print(f'| mAP (rare)\t\t: {test_stats["mAP rare"]:.2f} ({best_rare:.2f})')
print(f'| mAP (non-rare)\t: {test_stats["mAP non-rare"]:.2f} ({best_non_rare:.2f})')
if args.wandb and utils.get_rank() == 0:
wandb.log({
'mAP': test_stats['mAP']
})
if isinstance(lr_scheduler, torch.optim.lr_scheduler.ReduceLROnPlateau): lr_scheduler.step(test_stats['mAP'])
print('-'*100)
# if epoch%2==0:
# save_ckpt(args, model_without_ddp, optimizer, lr_scheduler, epoch, filename=f'checkpoint_{epoch}')
total_time = time.time() - start_time
total_time_str = str(datetime.timedelta(seconds=int(total_time)))
print('Training time {}'.format(total_time_str))
if args.dataset_file == 'vcoco':
print(f'| Scenario #1 mAP : {scenario1:.2f}')
print(f'| Scenario #2 mAP : {scenario2:.2f}')
elif args.dataset_file == 'hico-det':
print(f'| mAP (full)\t\t: {best_mAP:.2f}')
print(f'| mAP (rare)\t\t: {best_rare:.2f}')
print(f'| mAP (non-rare)\t: {best_non_rare:.2f}')
if __name__ == '__main__':
parser = argparse.ArgumentParser(
'End-to-End Human Object Interaction training and evaluation script',
parents=[get_args_parser()]
)
# training
parser.add_argument('--detr_weights', default=None, type=str)
parser.add_argument('--train_detr', action='store_true', default=False)
parser.add_argument('--finetune_detr_weight', default=0.1, type=float)
parser.add_argument('--lr_detr', default=1e-5, type=float)
parser.add_argument('--reduce_lr_on_plateau_patience', default=2, type=int)
parser.add_argument('--reduce_lr_on_plateau_factor', default=0.1, type=float)
# loss
parser.add_argument('--proposal_focal_loss_alpha', default=0.75, type=float) # large alpha for high recall
parser.add_argument('--action_focal_loss_alpha', default=0.5, type=float)
parser.add_argument('--proposal_focal_loss_gamma', default=2, type=float)
parser.add_argument('--action_focal_loss_gamma', default=2, type=float)
parser.add_argument('--proposal_loss_coef', default=1, type=float)
parser.add_argument('--action_loss_coef', default=1, type=float)
# ablations
parser.add_argument('--no_hard_mining_for_relation_discovery', dest='use_hard_mining_for_relation_discovery', action='store_false', default=True)
parser.add_argument('--no_relation_dependency_encoding', dest='use_relation_dependency_encoding', action='store_false', default=True)
parser.add_argument('--no_memory_layout_encoding', dest='use_memory_layout_encoding', action='store_false', default=True, help='layout encodings')
parser.add_argument('--no_nms_on_detr', dest='apply_nms_on_detr', action='store_false', default=True)
parser.add_argument('--no_tail_semantic_feature', dest='use_tail_semantic_feature', action='store_false', default=True)
parser.add_argument('--no_spatial_feature', dest='use_spatial_feature', action='store_false', default=True)
parser.add_argument('--no_interaction_decoder', action='store_true', default=False)
# not sensitive or effective
parser.add_argument('--use_memory_union_mask', action='store_true', default=False)
parser.add_argument('--use_union_feature', action='store_true', default=False)
parser.add_argument('--adaptive_relation_query_num', action='store_true', default=False)
parser.add_argument('--use_relation_tgt_mask', action='store_true', default=False)
parser.add_argument('--use_relation_tgt_mask_attend_topk', default=10, type=int)
parser.add_argument('--use_prior_verb_label_mask', action='store_true', default=False)
parser.add_argument('--relation_feature_map_from', default='backbone', help='backbone | detr_encoder')
parser.add_argument('--use_query_fourier_encoding', action='store_true', default=False)
args = parser.parse_args()
args.STIP_relation_head = True
if args.output_dir:
args.output_dir += f"/{args.group_name}/{args.run_name}/"
Path(args.output_dir).mkdir(parents=True, exist_ok=True)
main(args)