-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_experiments.py
157 lines (122 loc) · 4.96 KB
/
run_experiments.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
# Common
import os
import os.path as osp
import datetime
import argparse
import warnings
import socket
import torch
import torch.optim as optim
from torch.utils.tensorboard import SummaryWriter
import importlib
import wandb
from network.minkUnet import MinkUNet34
from utils import common as com
from utils.logger_FADA import setup_logger
# config file
from configs.config_base import cfg_from_yaml_file
from easydict import EasyDict
warnings.filterwarnings("ignore")
from git import Repo
def change_Config_DEBUG(cfg):
cfg.TRAIN.T_VAL_ITER = cfg.DEBUG.T_VAL_ITER
cfg.TRAIN.S_VAL_ITER = cfg.DEBUG.S_VAL_ITER
cfg.TRAIN.LOG_PERIOD = cfg.DEBUG.LOG_PERIOD
cfg.TRAIN.PREHEAT_STEPS = cfg.DEBUG.PREHEAT_STEPS
cfg.TRAIN.EXP_NAME = cfg.DEBUG.EXP_NAME
return cfg
def parse_args():
parser = argparse.ArgumentParser(description='PGDA training')
parser.add_argument(
'--cfg',
dest='config_file',
default='configs/SynLiDAR2SemanticKITTI/CosMix.yaml',
metavar='FILE',
help='path to config file',
type=str,
)
args = parser.parse_args()
return args
def main():
com.make_reproducible() # freeze all seeds
args = parse_args()
# load the configuration
cfg = EasyDict()
cfg.OUTPUT_DIR = './workspace/'
cfg_from_yaml_file(args.config_file, cfg)
cfg.TRAIN.config_file = args.config_file
curPath = os.path.abspath(os.path.dirname(__file__))
cfg.TRAIN.CURPATH = curPath
repo = Repo(curPath)
print(repo.active_branch) # current branch
wb_note = '*Path: ' + str(curPath) + ' **Git branch: ' + str(repo.active_branch)
# set GPU
os.environ["CUDA_VISIBLE_DEVICES"] = str(cfg.TRAIN.GPU_ID)
# mkdir for logs and checkpoints
time_now = datetime.datetime.now().strftime("%m-%d-%H_%M")
# Init wandb and logger
if cfg.TRAIN.DEBUG:
os.environ['WANDB_MODE'] = 'dryrun'
cfg = change_Config_DEBUG(cfg)
# Init save floder
cfg.TRAIN.MODEL_DIR = osp.join(cfg.OUTPUT_DIR, cfg.TRAIN.PROJECT_NAME, 'checkpoints', cfg.TRAIN.EXP_NAME, time_now)
os.makedirs(cfg.TRAIN.MODEL_DIR, exist_ok=True)
cfg.TRAIN.LOG_DIR = osp.join(cfg.OUTPUT_DIR, cfg.TRAIN.PROJECT_NAME, 'logs', cfg.TRAIN.EXP_NAME, time_now)
os.makedirs(cfg.TRAIN.LOG_DIR, exist_ok=True)
cfg.TRAIN.TB_DIR = osp.join(cfg.OUTPUT_DIR, cfg.TRAIN.PROJECT_NAME, 'tb_dirs', cfg.TRAIN.EXP_NAME, time_now)
hostname = socket.gethostname()
cfg.TRAIN.HOSTNAME = hostname
cfg.TRAIN.WANDB_ID = str(wandb.util.generate_id())
# WANDB initialization
wandb.init(name=cfg.TRAIN.EXP_NAME, notes=wb_note,
project=cfg.TRAIN.PROJECT_NAME,
entity='zhiminyuan', id=cfg.TRAIN.WANDB_ID)
wandb.config.update(cfg, allow_val_change=True)
print(cfg)
# Init logger and tensorboard
logger = setup_logger("Trainer", cfg) # Init Logging
logger.info('this Experiment is: \n %s \n \n ' % wb_note)
tf_writer = SummaryWriter(cfg.TRAIN.TB_DIR)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print("Host: {}, GPU: {}, wandb_ID: {}".format(hostname, cfg.TRAIN.GPU_ID, cfg.TRAIN.WANDB_ID))
# init network G and D
net = MinkUNet34(cfg.MODEL_G).to(device)
if cfg.OPTIMIZER.TYPE == "Adam":
G_optim = optim.Adam(net.parameters(),
lr=cfg.OPTIMIZER.LEARNING_RATE_G)
elif cfg.OPTIMIZER.TYPE == "SGD":
G_optim = optim.SGD(net.parameters(),
lr=cfg.OPTIMIZER.LEARNING_RATE_G,
momentum=0.98, weight_decay=0.0001, nesterov=True)
else:
raise ValueError("Optimizer type not supported")
# load pretrained model
print('Start loading pretrained model')
checkpoint = torch.load(cfg.TRAIN.PRETRAINPATH, map_location=torch.device('cpu'))
print('*** using preTrain model: %s ***' % cfg.TRAIN.PRETRAINPATH)
pretrained_dict = checkpoint['model_state_dict']
# Update parameters for G now
model_dict = net.state_dict()
model_dict.update(pretrained_dict)
net.load_state_dict(model_dict)
if cfg.MEAN_TEACHER.use_mt:
print("Create ema-net..")
# init ema-network. This model is utilized to generate pseudo-label.
ema_net = MinkUNet34(cfg.MODEL_G).to(device)
# Update parameters for G old
model_dict = ema_net.state_dict()
model_dict.update(pretrained_dict)
ema_net.load_state_dict(model_dict)
print('finish update pretrained parameters')
else:
ema_net = None
torch.backends.cudnn.enabled = False
torch.backends.cudnn.benchmark = True
trainer_file = importlib.import_module('trainers.' + cfg.TRAIN.STAGE)
trainer = trainer_file.Trainer(cfg,
net, ema_net,
G_optim,
logger, tf_writer, device)
trainer.train()
if __name__ == '__main__':
main()