-
Notifications
You must be signed in to change notification settings - Fork 7
/
launch_train.py
372 lines (329 loc) · 12.1 KB
/
launch_train.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
# Copyright 2022 - Valeo Comfort and Driving Assistance - Gilles Puy @ valeo.ai
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import yaml
import torch
import random
import warnings
import argparse
import numpy as np
import utils.transforms as tr
from utils.metrics import SemSegLoss
from utils.scheduler import WarmupCosine
from utils.trainer import TrainingManager
from waffleiron.segmenter import Segmenter
from datasets import LIST_DATASETS, Collate
def load_model_config(file):
with open(file) as f:
config = yaml.safe_load(f)
return config
def get_train_augmentations(config):
list_of_transf = []
# Two transformations shared across all datasets
list_of_transf.append(
tr.LimitNumPoints(
dims=(0, 1, 2),
max_point=config["dataloader"]["max_points"],
random=True,
)
)
# Optional augmentations
for aug_name in config["augmentations"].keys():
if aug_name == "rotation":
for d in config["augmentations"]["rotation"][0]:
list_of_transf.append(tr.Rotation(inplace=True, dim=d))
elif aug_name == "flip_xy":
list_of_transf.append(tr.RandomApply(tr.FlipXY(inplace=True), prob=2 / 3))
elif aug_name == "scale":
dims = config["augmentations"]["scale"][0]
scale = config["augmentations"]["scale"][1]
list_of_transf.append(tr.Scale(inplace=True, dims=dims, range=scale))
elif aug_name == "instance_cutmix":
# Do nothing here, directly handled in semantic kitti dataset
continue
else:
raise ValueError("Unknown transformation")
return tr.Compose(list_of_transf)
def get_datasets(config, args):
# Shared parameters
kwargs = {
"rootdir": args.path_dataset,
"input_feat": config["embedding"]["input_feat"],
"voxel_size": config["embedding"]["voxel_size"],
"num_neighbors": config["embedding"]["neighbors"],
"dim_proj": config["waffleiron"]["dim_proj"],
"grids_shape": config["waffleiron"]["grids_size"],
"fov_xyz": config["waffleiron"]["fov_xyz"],
}
# Get datatset
DATASET = LIST_DATASETS.get(args.dataset.lower())
if DATASET is None:
raise ValueError(f"Dataset {args.dataset.lower()} not available.")
# Train dataset
train_dataset = DATASET(
phase="trainval" if args.trainval else "train",
train_augmentations=get_train_augmentations(config),
instance_cutmix=config["augmentations"]["instance_cutmix"],
**kwargs,
)
# Validation dataset
val_dataset = DATASET(
phase="val",
**kwargs,
)
return train_dataset, val_dataset
def get_dataloader(train_dataset, val_dataset, args):
if args.distributed:
train_sampler = torch.utils.data.distributed.DistributedSampler(train_dataset)
val_sampler = torch.utils.data.distributed.DistributedSampler(val_dataset)
else:
train_sampler = None
val_sampler = None
train_loader = torch.utils.data.DataLoader(
train_dataset,
batch_size=args.batch_size,
shuffle=(train_sampler is None),
num_workers=args.workers,
pin_memory=True,
sampler=train_sampler,
drop_last=True,
collate_fn=Collate(),
)
val_loader = torch.utils.data.DataLoader(
val_dataset,
batch_size=args.batch_size,
shuffle=False,
num_workers=args.workers,
pin_memory=True,
sampler=val_sampler,
drop_last=False,
collate_fn=Collate(),
)
return train_loader, val_loader, train_sampler
def get_optimizer(parameters, config):
return torch.optim.AdamW(
parameters,
lr=config["optim"]["lr"],
weight_decay=config["optim"]["weight_decay"],
)
def get_scheduler(optimizer, config, len_train_loader):
scheduler = torch.optim.lr_scheduler.LambdaLR(
optimizer,
WarmupCosine(
config["scheduler"]["epoch_warmup"] * len_train_loader,
config["scheduler"]["max_epoch"] * len_train_loader,
config["scheduler"]["min_lr"] / config["optim"]["lr"],
),
)
return scheduler
def distributed_training(gpu, ngpus_per_node, args, config):
# --- Init. distributing training
args.gpu = gpu
if args.gpu is not None:
print(f"Use GPU: {args.gpu} for training")
if args.distributed:
args.rank = args.rank * ngpus_per_node + gpu
torch.distributed.init_process_group(
backend=args.dist_backend,
init_method=args.dist_url,
world_size=args.world_size,
rank=args.rank,
)
# --- Build network
model = Segmenter(
input_channels=config["embedding"]["size_input"],
feat_channels=config["waffleiron"]["nb_channels"],
depth=config["waffleiron"]["depth"],
grid_shape=config["waffleiron"]["grids_size"],
nb_class=config["classif"]["nb_class"],
drop_path_prob=config["waffleiron"]["drop"],
)
# ---
args.batch_size = config["dataloader"]["batch_size"]
args.workers = config["dataloader"]["num_workers"]
if args.distributed:
# For multiprocessing distributed, DistributedDataParallel constructor
# should always set the single device scope, otherwise,
# DistributedDataParallel will use all available devices.
torch.cuda.set_device(args.gpu)
model.cuda(args.gpu)
# When using a single GPU per process and per
# DistributedDataParallel, we need to divide the batch size
# ourselves based on the total number of GPUs of the current node.
args.batch_size = int(config["dataloader"]["batch_size"] / ngpus_per_node)
args.workers = int(
(config["dataloader"]["num_workers"] + ngpus_per_node - 1) / ngpus_per_node
)
model = torch.nn.SyncBatchNorm.convert_sync_batchnorm(model)
model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[args.gpu])
elif args.gpu is not None:
# Training on one GPU
torch.cuda.set_device(args.gpu)
model = model.cuda(args.gpu)
else:
# DataParallel will divide and allocate batch_size to all available GPUs
model = torch.nn.DataParallel(model).cuda()
if args.gpu == 0 or args.gpu is None:
print(f"Model:\n{model}")
nb_param = sum([p.numel() for p in model.parameters()]) / 1e6
print(f"{nb_param} x 10^6 trainable parameters ")
# --- Optimizer
optim = get_optimizer(model.parameters(), config)
# --- Dataset
train_dataset, val_dataset = get_datasets(config, args)
train_loader, val_loader, train_sampler = get_dataloader(
train_dataset, val_dataset, args
)
# --- Loss function
loss = SemSegLoss(
config["classif"]["nb_class"],
lovasz_weight=config["loss"]["lovasz"],
).cuda(args.gpu)
# --- Sets the learning rate to the initial LR decayed by 10 every 30 epochs
scheduler = get_scheduler(optim, config, len(train_loader))
# --- Training
mng = TrainingManager(
model,
loss,
train_loader,
val_loader,
train_sampler,
optim,
scheduler,
config["scheduler"]["max_epoch"],
args.log_path,
args.gpu,
args.world_size,
args.fp16,
LIST_DATASETS.get(args.dataset.lower()).CLASS_NAME,
tensorboard=(not args.eval),
)
if args.restart:
mng.load_state()
if args.eval:
try:
model.compress()
except:
model.module.compress()
mng.one_epoch(training=False)
else:
mng.train()
def main(args, config):
# --- Fixed args
# Device
args.device = "cuda"
# Node rank for distributed training
args.rank = 0
# Number of nodes for distributed training'
args.world_size = 1
# URL used to set up distributed training
args.dist_url = "tcp://127.0.0.1:4444"
# Distributed backend'
args.dist_backend = "nccl"
# Distributed processing
args.distributed = args.multiprocessing_distributed
# Create log directory
os.makedirs(args.log_path, exist_ok=True)
if args.seed is not None:
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
os.environ["PYTHONHASHSEED"] = str(args.seed)
if args.gpu is not None:
args.gpu = 0
args.distributed = False
args.multiprocessing_distributed = False
warnings.warn(
"You have chosen a specific GPU. This will completely disable data parallelism."
)
# Extract instances for cutmix
if config["augmentations"]["instance_cutmix"]:
get_datasets(config, args)
ngpus_per_node = torch.cuda.device_count()
if args.multiprocessing_distributed:
# Since we have ngpus_per_node processes per node, the total world_size
# needs to be adjusted accordingly
args.world_size = ngpus_per_node * args.world_size
# Use torch.multiprocessing.spawn to launch distributed processes: the
# main_worker process function
torch.multiprocessing.spawn(
distributed_training,
nprocs=ngpus_per_node,
args=(ngpus_per_node, args, config),
)
else:
# Simply call main_worker function
distributed_training(args.gpu, ngpus_per_node, args, config)
def get_default_parser():
parser = argparse.ArgumentParser(description="Training")
parser.add_argument(
"--dataset",
type=str,
help="Path to dataset",
default="nuscenes",
)
parser.add_argument(
"--path_dataset",
type=str,
help="Path to dataset",
default="/datasets_local/nuscenes/",
)
parser.add_argument(
"--log_path", type=str, required=True, help="Path to log folder"
)
parser.add_argument(
"-r", "--restart", action="store_true", default=False, help="Restart training"
)
parser.add_argument(
"--seed", default=None, type=int, help="Seed for initializing training"
)
parser.add_argument(
"--gpu", default=None, type=int, help="Set to any number to use gpu 0"
)
parser.add_argument(
"--multiprocessing-distributed",
action="store_true",
help="Use multi-processing distributed training to launch "
"N processes per node, which has N GPUs. This is the "
"fastest way to use PyTorch for either single node or "
"multi node data parallel training",
)
parser.add_argument(
"--fp16",
action="store_true",
default=False,
help="Enable autocast for mix precision training",
)
parser.add_argument(
"--config", type=str, required=True, help="Path to model config"
)
parser.add_argument(
"--trainval",
action="store_true",
default=False,
help="Use train + val as train set",
)
parser.add_argument(
"--eval",
action="store_true",
default=False,
help="Run validation only",
)
return parser
if __name__ == "__main__":
parser = get_default_parser()
args = parser.parse_args()
config = load_model_config(args.config)
main(args, config)