-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
292 lines (241 loc) · 9.16 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
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
import os, argparse, importlib
import numpy as np
import torch
import random
from engine import do_train
from datasets import build_dataset
from models import build_assistant
from torch.multiprocessing import set_start_method
from utils.io import resume_if_possible
from utils.dist import init_distributed, is_distributed, get_rank
def make_args_parser():
parser = argparse.ArgumentParser("Train Parameters of the Baseline Model")
##### Optimizer #####
parser.add_argument("--base_lr", default=5e-4, type=float)
parser.add_argument("--final_lr", default=1e-6, type=float)
parser.add_argument("--lr_scheduler", default="cosine", type=str)
parser.add_argument("--weight_decay", default=0.1, type=float)
parser.add_argument("--optimizer", default="AdamW", type=str)
parser.add_argument(
"--clip_gradient", default=0.1, type=float,
help="Max L2 norm of the gradient"
)
parser.add_argument("--warm_lr", default=1e-6, type=float)
parser.add_argument("--warm_lr_epochs", default=9, type=int)
parser.add_argument("--pretrained_params_lr", default=None, type=float)
##### Model #####
# input based parameters
parser.add_argument("--use_color", default=False, action="store_true")
parser.add_argument("--use_normal", default=False, action="store_true")
parser.add_argument("--no_height", default=False, action="store_true")
parser.add_argument("--use_multiview", default=False, action="store_true")
# model type
parser.add_argument(
"--vision_encoder", default="pointnet2",
choices = ['transformer', 'pointnet2'],
help="folder of the vision_encoder: transformer-based or pointnet2-based"
)
parser.add_argument(
"--task_decoder", default=None, type=str, help="folder of the backbone"
)
parser.add_argument(
'--base_llm_path', default="llama2", type=str,
help="should be one of `opt` or `llama2`"
)
parser.add_argument("--pretrained_path", default=None, type=str)
parser.add_argument("--image_encoder", default=None, type=str)
## other parameters
parser.add_argument("--use_pretrained", default=False, action="store_true")
parser.add_argument(
"--freeze_encoder", default=False, action='store_true',
help="freeze scene encoder"
)
parser.add_argument(
"--freeze_decoder", default=False, action='store_true',
help="freeze the llm"
)
parser.add_argument(
"--preenc_npoints", type=int, default=256,
help="Number of points before encoding"
)
parser.add_argument(
"--enc_type", type=str, default='masked',
help="Type of encoder"
)
parser.add_argument(
"--enc_nlayers", type=int, default=3,
help="Number of layers in the encoder"
)
parser.add_argument(
"--enc_dim", type=int, default=256,
help="Dimension of encoder layers"
)
parser.add_argument(
"--enc_ffn_dim", type=int, default=128,
help="Dimension of encoder's feedforward network"
)
parser.add_argument(
"--enc_dropout", type=float, default=0.1,
help="Dropout rate in encoder"
)
parser.add_argument(
"--enc_nhead", type=int, default=4,
help="Number of heads in encoder's multi-head attention"
)
parser.add_argument(
"--enc_activation", type=str, default='relu',
help="Activation function in encoder"
)
parser.add_argument(
"--use_beam_search", default=False, action='store_true',
help='whether use beam search during caption generation.'
)
parser.add_argument(
"--max_des_len", default=256, type=int,
help="maximum length of object descriptions."
)
##### Dataset #####
parser.add_argument(
"--dataset", default='m3dbench',
help="dataset file which stores `dataset` and `dataset_config` class",
)
parser.add_argument(
"--num_points", default=40000, type=int,
help="num of points."
)
parser.add_argument(
"--num_points_object", default=1024, type=int,
help="num of points for each object."
)
parser.add_argument(
"--k_sentence_per_scene", default=None, type=int,
help="k sentences per scene for training caption model",
)
parser.add_argument("--dataset_num_workers", default=4, type=int)
parser.add_argument("--batchsize_per_gpu", default=8, type=int)
##### Training #####
parser.add_argument("--train_input", default=None, type=str)
parser.add_argument("--start_epoch", default=-1, type=int)
parser.add_argument("--max_epoch", default=1080, type=int)
parser.add_argument("--eval_every_iteration", default=2000, type=int)
parser.add_argument("--seed", default=42, type=int)
##### Testing #####
parser.add_argument("--eval_input", default=None, type=str)
parser.add_argument("--test_only", default=False, action="store_true")
parser.add_argument(
"--test_min_iou", default=0.50, type=float,
help='minimum iou for evaluating dense caption performance'
)
parser.add_argument(
"--criterion", default='CiDEr', type=str,
help='metrics for saving the best model'
)
parser.add_argument("--test_ckpt", default="", type=str)
##### I/O #####
parser.add_argument("--checkpoint_dir", default=None, type=str)
parser.add_argument("--log_every", default=10, type=int)
##### Distributed #####
parser.add_argument("--ngpus", default=8, type=int, help='number of gpus')
parser.add_argument("--dist_url", default='tcp://localhost:12345', type=str)
args = parser.parse_args()
args.use_height = not args.no_height
return args
def setup_seeds(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def main(local_rank, args):
setup_seeds(args.seed)
if args.ngpus > 1:
init_distributed(
local_rank,
global_rank=local_rank,
world_size=args.ngpus,
dist_url=args.dist_url,
dist_backend="nccl",
)
torch.cuda.set_device(local_rank)
np.random.seed(args.seed + local_rank)
torch.cuda.manual_seed_all(args.seed + local_rank + get_rank())
if args.checkpoint_dir is not None:
pass
elif args.test_ckpt is not None:
args.checkpoint_dir = os.path.dirname(args.test_ckpt)
print(f'testing directory: {args.checkpoint_dir}')
else:
raise AssertionError(
'Either checkpoint_dir or test_ckpt should be presented!'
)
os.makedirs(args.checkpoint_dir, exist_ok=True)
### build datasets and dataloaders
dataset_config, datasets, dataloaders = build_dataset(args)
model = build_assistant(args, dataset_config, datasets['train']).cuda()
model = model.cuda(local_rank)
model_no_ddp = model
if is_distributed():
model = torch.nn.SyncBatchNorm.convert_sync_batchnorm(model)
model = torch.nn.parallel.DistributedDataParallel(
model, device_ids=[local_rank]
)
# testing phase
if args.test_only:
checkpoint = torch.load(args.test_ckpt, map_location=torch.device("cpu"))
model_no_ddp.load_state_dict(checkpoint["model"], strict=False)
dataloaders['test'].dataset.eval_func(
args,
-1,
model,
dataset_config,
dataloaders['test']
)
# training phase
else:
assert (
args.checkpoint_dir is not None
), "Please specify a checkpoint dir using --checkpoint_dir"
os.makedirs(args.checkpoint_dir, exist_ok=True)
if args.optimizer == 'AdamW':
optimizer = torch.optim.AdamW(
filter(lambda params: params.requires_grad, model_no_ddp.parameters()),
lr=args.base_lr,
weight_decay=args.weight_decay
)
elif args.optimizer == 'SGD':
optimizer = torch.optim.SGD(
filter(lambda params: params.requires_grad, model_no_ddp.parameters()),
lr=args.base_lr,
weight_decay=args.weight_decay
)
else:
raise NotImplementedError
loaded_epoch, best_val_metrics = resume_if_possible(
args.checkpoint_dir, model_no_ddp, optimizer
)
args.start_epoch = loaded_epoch + 1
do_train(
args,
model,
model_no_ddp,
optimizer,
dataset_config,
dataloaders,
best_val_metrics,
)
def launch_distributed(args):
world_size = args.ngpus
if world_size == 1:
main(local_rank=0, args=args)
else:
torch.multiprocessing.spawn(main, nprocs=world_size, args=(args,))
if __name__ == "__main__":
args = make_args_parser()
os.environ['PYTHONWARNINGS']='ignore:semaphore_tracker:UserWarning'
try:
set_start_method("spawn")
except RuntimeError:
pass
launch_distributed(args)