-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.py
192 lines (161 loc) · 6.7 KB
/
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
"""
Utilities for training, testing and caching results
for HICO-DET and V-COCO evaluations
Fred Zhang <frederic.zhang@anu.edu.au>
The Australian National University
Microsoft Research Asia
"""
import os
import sys
import torch
import random
import warnings
import argparse
import numpy as np
import torch.distributed as dist
import torch.multiprocessing as mp
from torch.utils.data import DataLoader, DistributedSampler
from pvic import build_detector
from utils import custom_collate, CustomisedDLE, DataFactory
from configs import base_detector_args, advanced_detector_args
warnings.filterwarnings("ignore")
def main(rank, args):
dist.init_process_group(
backend="nccl",
init_method="env://",
world_size=args.world_size,
rank=rank
)
# Fix seed
seed = args.seed + dist.get_rank()
torch.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
torch.cuda.set_device(rank)
trainset = DataFactory(
name=args.dataset, partition=args.partitions[0],
data_root=args.data_root
)
testset = DataFactory(
name=args.dataset, partition=args.partitions[1],
data_root=args.data_root
)
train_loader = DataLoader(
dataset=trainset,
collate_fn=custom_collate, batch_size=args.batch_size // args.world_size,
num_workers=args.num_workers, pin_memory=True,
sampler=DistributedSampler(
trainset, num_replicas=args.world_size,
rank=rank, drop_last=True)
)
test_loader = DataLoader(
dataset=testset,
collate_fn=custom_collate, batch_size=args.batch_size // args.world_size,
num_workers=args.num_workers, pin_memory=True,
sampler=DistributedSampler(
testset, num_replicas=args.world_size,
rank=rank, drop_last=True)
)
if args.dataset == 'hicodet':
object_to_target = train_loader.dataset.dataset.object_to_verb
args.num_verbs = 117
elif args.dataset == 'vcoco':
object_to_target = list(train_loader.dataset.dataset.object_to_action.values())
args.num_verbs = 24
model = build_detector(args, object_to_target)
if os.path.exists(args.resume):
print(f"=> Rank {rank}: PViC loaded from saved checkpoint {args.resume}.")
checkpoint = torch.load(args.resume, map_location='cpu')
model.load_state_dict(checkpoint['model_state_dict'])
else:
print(f"=> Rank {rank}: PViC randomly initialised.")
engine = CustomisedDLE(model, train_loader, test_loader, args)
if args.cache:
if args.dataset == 'hicodet':
engine.cache_hico(test_loader, args.output_dir)
elif args.dataset == 'vcoco':
engine.cache_vcoco(test_loader, args.output_dir)
return
if args.eval:
if args.dataset == 'vcoco':
"""
NOTE This evaluation results on V-COCO do not necessarily follow the
protocol as the official evaluation code, and so are only used for
diagnostic purposes.
"""
ap = engine.test_vcoco()
if rank == 0:
print(f"The mAP is {ap.mean():.4f}.")
return
else:
ap = engine.test_hico()
if rank == 0:
# Fetch indices for rare and non-rare classes
rare = trainset.dataset.rare
non_rare = trainset.dataset.non_rare
print(
f"The mAP is {ap.mean():.4f},"
f" rare: {ap[rare].mean():.4f},"
f" none-rare: {ap[non_rare].mean():.4f}"
)
return
model.freeze_detector()
param_dicts = [{"params": [p for p in model.parameters() if p.requires_grad]}]
optim = torch.optim.AdamW(param_dicts, lr=args.lr_head, weight_decay=args.weight_decay)
lr_scheduler = torch.optim.lr_scheduler.StepLR(optim, args.lr_drop, gamma=args.lr_drop_factor)
# Override optimiser and learning rate scheduler
engine.update_state_key(optimizer=optim, lr_scheduler=lr_scheduler)
engine(args.epochs)
@torch.no_grad()
def sanity_check(args):
dataset = DataFactory(name='hicodet', partition=args.partitions[0], data_root=args.data_root)
args.num_verbs = 117
args.num_triplets = 600
object_to_target = dataset.dataset.object_to_verb
model = build_detector(args, object_to_target)
if args.eval:
model.eval()
if os.path.exists(args.resume):
ckpt = torch.load(args.resume, map_location='cpu')
print(f"Loading checkpoints from {args.resume}.")
model.load_state_dict(ckpt['model_state_dict'])
image, target = dataset[998]
outputs = model([image], targets=[target])
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('--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('--resume', default='', help='Resume from a model')
parser.add_argument('--use-wandb', default=False, action='store_true')
parser.add_argument('--port', default='1234', type=str)
parser.add_argument('--seed', default=140, type=int)
parser.add_argument('--world-size', default=8, type=int)
parser.add_argument('--eval', action='store_true')
parser.add_argument('--cache', action='store_true')
parser.add_argument('--sanity', action='store_true')
args = parser.parse_args()
print(args)
if args.sanity:
sanity_check(args)
sys.exit()
if not args.use_wandb:
os.environ["WANDB_MODE"] = "disabled"
os.environ["MASTER_ADDR"] = "localhost"
os.environ["MASTER_PORT"] = args.port
mp.spawn(main, nprocs=args.world_size, args=(args,))