-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrain.py
152 lines (130 loc) · 4.75 KB
/
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
# -*- encoding: utf-8 -*-
# auth: Fuchen Long
# mail: [email protected]
# date: 2022/01/08
# desc: train file for PointClustering
import open3d as o3d # prevent loading error
import argparse
import sys
import os
import json
import logging
import random
import numpy as np
import torch
import torch.distributed as dist
import torch.backends.cudnn as cudnn
from omegaconf import OmegaConf
from easydict import EasyDict as edict
import util.multiprocessing as mpu
from util.logger import setup_logger
from dataloader.data_loaders import make_data_loader
from trainer import *
random.seed(0)
np.random.seed(0)
torch.manual_seed(0)
torch.cuda.manual_seed(0)
torch.cuda.manual_seed_all(0)
def get_trainer(trainer):
if trainer == 'PointClusteringTrainer':
return PointClusteringTrainer
else:
raise ValueError(f'Trainer {trainer} not found')
# ---- Not included ----
#elif trainer == 'HardestContrastiveLossTrainer':
# return HardestContrastiveLossTrainer
#elif trainer == 'PointNCELossTrainer':
# return PointNCELossTrainer
#elif trainer == 'PointNCELossRanSacTrainer':
# return PointNCELossRanSacTrainer
#elif trainer == 'PointNCEClusterTrainer':
# return PointNCEClusterTrainer
#elif trainer == 'PointClusterSegTrainer':
# return PointClusterSegTrainer
#elif trainer == 'PointClusterPairSegTrainer':
# return PointClusterPairSegTrainer
#elif trainer == 'PointClusterPairAugCropSegTrainer':
# return PointClusterPairAugCropSegTrainer
#elif trainer == 'PointClusterPairDBSCANTrainer':
# return PointClusterPairDBSCANTrainer
#elif trainer == 'PointProtoDBSCANTrainer':
# return PointProtoDBSCANTrainer
#elif trainer == 'PointProtoCoDBSCANTrainer':
# return PointProtoCoDBSCANTrainer
#elif trainer == 'PointProtoCoDBSCANTrainerV2':
# return PointProtoCoDBSCANTrainerV2
#elif trainer == 'PointProtoCoDBSCANCrossTrainer':
# return PointProtoCoDBSCANCrossTrainer
#elif trainer == 'VoxelProtoCoDBSCANTrainer':
# return VoxelProtoCoDBSCANTrainer
#elif trainer == 'VoxelProtoCoDBSCANCrossTrainer':
# return VoxelProtoCoDBSCANCrossTrainer
#elif trainer == 'PointInsNCETrainer':
# return PointInsNCETrainer
#elif trainer == 'SoftmaxLossTrainer':
# return SoftmaxLossTrainer
#elif trainer == 'LinearSVMTrainer':
# return LinearSVMTrainer
#elif trainer == 'MultiShapeCrossEntropyLossTrainer':
# return MultiShapeCrossEntropyLossTrainer
#elif trainer == 'MaskedCrossEntropyLossTrainer':
# return MaskedCrossEntropyLossTrainer
#elif trainer == 'WeightedCrossEntropyLossTrainer':
# return WeightedCrossEntropyLossTrainer
#elif trainer == 'VoxelCrossEntropyLossTrainer':
# return VoxelCrossEntropyLossTrainer
#elif trainer == 'ChamferDistanceLossTrainer':
# return ChamferDistanceLossTrainer
#elif trainer == 'ChamferDistanceLossUE4Trainer':
# return ChamferDistanceLossUE4Trainer
#elif trainer == 'ChamferDistanceLossShapeNet55Trainer':
# return ChamferDistanceLossShapeNet55Trainer
#elif trainer == 'ChamferDistanceRawPointsCompletor':
# return ChamferDistanceRawPointsCompletor
def parse_option():
parser = argparse.ArgumentParser('training')
parser.add_argument('--config_file', type=str, required=True, help='path of config file (yaml)')
parser.add_argument('--local_rank', type=int, help='local rank for DistributedDataParallel')
args = parser.parse_args()
if args.config_file:
config = OmegaConf.load(args.config_file)
return args, config
def main(config, logger):
train_loader = make_data_loader(
config,
config.trainer.batch_size,
phase = config.trainer.train_phase,
num_threads=config.misc.num_workers,
logger=logger)
Trainer = get_trainer(config.trainer.trainer)
if config.pretrain:
trainer = Trainer(
config=config,
data_loader=train_loader,
logger=logger,)
else:
val_loader = make_data_loader(
config,
config.trainer.batch_size,
phase=config.trainer.val_phase,
num_threads=config.misc.num_workers,
logger=logger)
trainer = Trainer(
config=config,
data_loader = train_loader,
val_data_loader = val_loader,
logger=logger,)
trainer.train()
if __name__ == "__main__":
opt, config = parse_option()
torch.cuda.set_device(opt.local_rank)
torch.distributed.init_process_group(backend='nccl', init_method='env://')
cudnn.benchmark = True
os.makedirs(config.misc.output_dir, exist_ok=True)
logger = setup_logger(output=config.misc.output_dir, distributed_rank=dist.get_rank(), name="dbpcl")
if dist.get_rank() == 0:
path = os.path.join(config.misc.output_dir, "train_val_3d.config.json")
with open(path, 'w') as f:
json.dump(vars(opt), f, indent=2)
logger.info("Full config saved to {}".format(path))
main(config, logger)