-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtrain.py
134 lines (107 loc) · 4.02 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
import argparse
import datetime
import json
import os
import shutil
import sys
import cv2
import numpy as np
import torch
import yaml
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
from dataset.nersemble import NeRSembleData
from joint_trainer import JointTrainer
from hair_trainer import HairTrainer
from utils import seed_everything
parser = argparse.ArgumentParser(description="Training")
parser.add_argument("--config_path", default="./params.yaml", type=str)
parser.add_argument("--workspace", type=str, required=True)
parser.add_argument("--version", type=str, required=True)
parser.add_argument("--extra_config", type=str, default="{}", required=False)
parser.add_argument("--time", action="store_true")
args = parser.parse_args()
# Load config yaml file and pre-process params
with open(args.config_path, "r") as f:
config = yaml.safe_load(f)
extra_config = json.loads(args.extra_config)
for k in extra_config.keys():
assert k in config, k
config.update(extra_config)
config["bald"] = args.config_path.split("/")[-2] in ["218"]
# Dump tmp config file
tmp_config_path = os.path.join(os.path.dirname(args.config_path), "params_tmp.yaml")
with open(tmp_config_path, "w") as f:
print("Dumping extra config file...")
yaml.dump(config, f)
# pre-process params
config["current_epoch"] = 0
# Config gpu
torch.cuda.set_device(0)
def get_dataset(logger, datatype="nersemble"):
data_dict = {"nersemble": NeRSembleData}
assert datatype in data_dict.keys(), "Not Supported Datatype: {}".format(datatype)
Data = data_dict[datatype]
# get dataset
batch_size = config["data.per_gpu_batch_size"]
train_set = Data(config)
if logger:
logger.info("number of train images: {}".format(len(train_set)))
train_loader = DataLoader(
train_set,
batch_size=batch_size,
shuffle=True,
num_workers=config["data.num_workers"],
drop_last=False,
)
val_set = Data(config, split="val")
if logger:
logger.info("number of val images: {}".format(len(val_set)))
val_loader = DataLoader(
val_set,
batch_size=batch_size,
shuffle=False,
num_workers=config["data.num_workers"],
drop_last=True,
)
return train_loader, val_loader, train_set.radius, train_set.load_all_flame_params()
def train():
seed_everything(42)
# Enable cudnn benchmark for speed optimization
torch.backends.cudnn.benchmark = True
torch.backends.cudnn.enabled = True
# Config logging and tb writer
logger = None
import logging
# logging to file and stdout
config["log_file"] = os.path.join(args.workspace, args.version, "training.log")
logger = logging.getLogger("MeGA")
file_handler = logging.FileHandler(config["log_file"])
stream_handler = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter("[%(asctime)s %(filename)s] %(message)s")
file_handler.setFormatter(formatter)
stream_handler.setFormatter(formatter)
logger.handlers = [file_handler, stream_handler]
logger.setLevel(logging.INFO)
logger.propagate = False
logger.info("Training config: {}".format(config))
# tensorboard summary_writer
config["tb_writer"] = SummaryWriter(log_dir=config["local_workspace"])
config["logger"] = logger
# Init data loader
config["datatype"] = args.config_path.split("/")[-3]
train_loader, val_loader, radius, all_flame_params = get_dataset(logger, config["datatype"])
Trainer = HairTrainer if config["pipe.neutral_hair"] else JointTrainer
trainer = Trainer(config, logger, radius, all_flame_params=all_flame_params)
trainer.train(train_loader, val_loader, show_time=args.time)
def main():
workspace = os.path.join(args.workspace, args.version)
config["local_workspace"] = workspace
# Create sub working dir
if not os.path.exists(workspace):
os.makedirs(workspace)
shutil.copy(tmp_config_path, os.path.join(workspace, "params.yaml"))
# Start training
train()
if __name__ == "__main__":
main()