-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtrain_net.py
119 lines (95 loc) · 3.42 KB
/
train_net.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
# Copyright 2019 Stanislav Pidhorskyi
#
# 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 sys
import argparse
import logging
import torch
import torch.multiprocessing as mp
from torch.utils.collect_env import get_pretty_env_info
from defaults import get_cfg_defaults
from StyleGAN import train
from torch import distributed
def setup(rank, world_size):
os.environ['MASTER_ADDR'] = 'localhost'
os.environ['MASTER_PORT'] = '12355'
# initialize the process group
distributed.init_process_group("nccl", rank=rank, world_size=world_size)
def cleanup():
distributed.destroy_process_group()
def train_net(rank, world_size, args):
if world_size > 1:
setup(rank, world_size)
torch.cuda.set_device(rank)
cfg = get_cfg_defaults()
cfg.merge_from_file(args.config_file)
cfg.merge_from_list(args.opts)
cfg.freeze()
logger = logging.getLogger("logger")
logger.setLevel(logging.DEBUG)
output_dir = cfg.OUTPUT_DIR
os.makedirs(output_dir, exist_ok=True)
if rank == 0:
ch = logging.StreamHandler(stream=sys.stdout)
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s %(name)s %(levelname)s: %(message)s")
ch.setFormatter(formatter)
logger.addHandler(ch)
fh = logging.FileHandler(os.path.join(output_dir, 'log.txt'))
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
logger.addHandler(fh)
logger.info(args)
logger.info("Using {} GPUs".format(world_size))
logger.info("Loaded configuration file {}".format(args.config_file))
with open(args.config_file, "r") as cf:
config_str = "\n" + cf.read()
logger.info(config_str)
logger.info("Running with config:\n{}".format(cfg))
torch.set_default_tensor_type('torch.cuda.FloatTensor')
args.distributed = world_size > 1
train(cfg, rank, world_size, args.distributed, logger)
def run(fn, world_size):
parser = argparse.ArgumentParser(description="Adversarial, hierarchical style VAE")
parser.add_argument(
"--config-file",
default="configs/experiment_celeba.yaml",
metavar="FILE",
help="path to config file",
type=str,
)
parser.add_argument(
"opts",
help="Modify config options using the command-line",
default=None,
nargs=argparse.REMAINDER,
)
args = parser.parse_args()
try:
if world_size > 1:
mp.spawn(fn,
args=(world_size, args),
nprocs=world_size,
join=True)
else:
train_net(0, world_size, args)
finally:
if world_size > 1:
cleanup()
if __name__ == '__main__':
import os
#os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
#os.environ["CUDA_VISIBLE_DEVICES"] = "1"
run(train_net, torch.cuda.device_count())