This repository has been archived by the owner on Jan 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathtrain.py
259 lines (210 loc) · 7.7 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
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
# Copyright (c) 2020 Uber Technologies, Inc.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
os.umask(0)
os.environ["MKL_NUM_THREADS"] = "1"
os.environ["NUMEXPR_NUM_THREADS"] = "1"
os.environ["OMP_NUM_THREADS"] = "1"
import argparse
import numpy as np
import random
import sys
import time
import shutil
from importlib import import_module
from numbers import Number
from tqdm import tqdm
import torch
from torch.utils.data import Sampler, DataLoader
import horovod.torch as hvd
from torch.utils.data.distributed import DistributedSampler
from utils import Logger, load_pretrain
from mpi4py import MPI
comm = MPI.COMM_WORLD
hvd.init()
torch.cuda.set_device(hvd.local_rank())
root_path = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, root_path)
parser = argparse.ArgumentParser(description="Fuse Detection in Pytorch")
parser.add_argument(
"-m", "--model", default="lanegcn", type=str, metavar="MODEL", help="model name"
)
parser.add_argument("--eval", action="store_true")
parser.add_argument(
"--resume", default="", type=str, metavar="RESUME", help="checkpoint path"
)
parser.add_argument(
"--weight", default="", type=str, metavar="WEIGHT", help="checkpoint path"
)
def main():
seed = hvd.rank()
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
# Import all settings for experiment.
args = parser.parse_args()
model = import_module(args.model)
config, Dataset, collate_fn, net, loss, post_process, opt = model.get_model()
if config["horovod"]:
opt.opt = hvd.DistributedOptimizer(
opt.opt, named_parameters=net.named_parameters()
)
if args.resume or args.weight:
ckpt_path = args.resume or args.weight
if not os.path.isabs(ckpt_path):
ckpt_path = os.path.join(config["save_dir"], ckpt_path)
ckpt = torch.load(ckpt_path, map_location=lambda storage, loc: storage)
load_pretrain(net, ckpt["state_dict"])
if args.resume:
config["epoch"] = ckpt["epoch"]
opt.load_state_dict(ckpt["opt_state"])
if args.eval:
# Data loader for evaluation
dataset = Dataset(config["val_split"], config, train=False)
val_sampler = DistributedSampler(
dataset, num_replicas=hvd.size(), rank=hvd.rank()
)
val_loader = DataLoader(
dataset,
batch_size=config["val_batch_size"],
num_workers=config["val_workers"],
sampler=val_sampler,
collate_fn=collate_fn,
pin_memory=True,
)
hvd.broadcast_parameters(net.state_dict(), root_rank=0)
val(config, val_loader, net, loss, post_process, 999)
return
# Create log and copy all code
save_dir = config["save_dir"]
log = os.path.join(save_dir, "log")
if hvd.rank() == 0:
if not os.path.exists(save_dir):
os.makedirs(save_dir)
sys.stdout = Logger(log)
src_dirs = [root_path]
dst_dirs = [os.path.join(save_dir, "files")]
for src_dir, dst_dir in zip(src_dirs, dst_dirs):
files = [f for f in os.listdir(src_dir) if f.endswith(".py")]
if not os.path.exists(dst_dir):
os.makedirs(dst_dir)
for f in files:
shutil.copy(os.path.join(src_dir, f), os.path.join(dst_dir, f))
# Data loader for training
dataset = Dataset(config["train_split"], config, train=True)
train_sampler = DistributedSampler(
dataset, num_replicas=hvd.size(), rank=hvd.rank()
)
train_loader = DataLoader(
dataset,
batch_size=config["batch_size"],
num_workers=config["workers"],
sampler=train_sampler,
collate_fn=collate_fn,
pin_memory=True,
worker_init_fn=worker_init_fn,
drop_last=True,
)
# Data loader for evaluation
dataset = Dataset(config["val_split"], config, train=False)
val_sampler = DistributedSampler(dataset, num_replicas=hvd.size(), rank=hvd.rank())
val_loader = DataLoader(
dataset,
batch_size=config["val_batch_size"],
num_workers=config["val_workers"],
sampler=val_sampler,
collate_fn=collate_fn,
pin_memory=True,
)
hvd.broadcast_parameters(net.state_dict(), root_rank=0)
hvd.broadcast_optimizer_state(opt.opt, root_rank=0)
epoch = config["epoch"]
remaining_epochs = int(np.ceil(config["num_epochs"] - epoch))
for i in range(remaining_epochs):
train(epoch + i, config, train_loader, net, loss, post_process, opt, val_loader)
def worker_init_fn(pid):
np_seed = hvd.rank() * 1024 + int(pid)
np.random.seed(np_seed)
random_seed = np.random.randint(2 ** 32 - 1)
random.seed(random_seed)
def train(epoch, config, train_loader, net, loss, post_process, opt, val_loader=None):
train_loader.sampler.set_epoch(int(epoch))
net.train()
num_batches = len(train_loader)
epoch_per_batch = 1.0 / num_batches
save_iters = int(np.ceil(config["save_freq"] * num_batches))
display_iters = int(
config["display_iters"] / (hvd.size() * config["batch_size"])
)
val_iters = int(config["val_iters"] / (hvd.size() * config["batch_size"]))
start_time = time.time()
metrics = dict()
for i, data in tqdm(enumerate(train_loader),disable=hvd.rank()):
epoch += epoch_per_batch
data = dict(data)
output = net(data)
loss_out = loss(output, data)
post_out = post_process(output, data)
post_process.append(metrics, loss_out, post_out)
opt.zero_grad()
loss_out["loss"].backward()
lr = opt.step(epoch)
num_iters = int(np.round(epoch * num_batches))
if hvd.rank() == 0 and (
num_iters % save_iters == 0 or epoch >= config["num_epochs"]
):
save_ckpt(net, opt, config["save_dir"], epoch)
if num_iters % display_iters == 0:
dt = time.time() - start_time
metrics = sync(metrics)
if hvd.rank() == 0:
post_process.display(metrics, dt, epoch, lr)
start_time = time.time()
metrics = dict()
if num_iters % val_iters == 0:
val(config, val_loader, net, loss, post_process, epoch)
if epoch >= config["num_epochs"]:
val(config, val_loader, net, loss, post_process, epoch)
return
def val(config, data_loader, net, loss, post_process, epoch):
net.eval()
start_time = time.time()
metrics = dict()
for i, data in enumerate(data_loader):
data = dict(data)
with torch.no_grad():
output = net(data)
loss_out = loss(output, data)
post_out = post_process(output, data)
post_process.append(metrics, loss_out, post_out)
dt = time.time() - start_time
metrics = sync(metrics)
if hvd.rank() == 0:
post_process.display(metrics, dt, epoch)
net.train()
def save_ckpt(net, opt, save_dir, epoch):
if not os.path.exists(save_dir):
os.makedirs(save_dir)
state_dict = net.state_dict()
for key in state_dict.keys():
state_dict[key] = state_dict[key].cpu()
save_name = "%3.3f.ckpt" % epoch
torch.save(
{"epoch": epoch, "state_dict": state_dict, "opt_state": opt.opt.state_dict()},
os.path.join(save_dir, save_name),
)
def sync(data):
data_list = comm.allgather(data)
data = dict()
for key in data_list[0]:
if isinstance(data_list[0][key], list):
data[key] = []
else:
data[key] = 0
for i in range(len(data_list)):
data[key] += data_list[i][key]
return data
if __name__ == "__main__":
main()