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 131
/
train1.py
213 lines (168 loc) · 6.1 KB
/
train1.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
import os
import argparse
import numpy as np
import random
import sys
import time
import shutil
from importlib import import_module
from numbers import Number
import torch
from torch.utils.data import Sampler, DataLoader
from utils import Logger, load_pretrain
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="model", 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 = 0
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 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_loader = DataLoader(
dataset,
batch_size=config["val_batch_size"],
num_workers=config["val_workers"],
# sampler=val_sampler,
shuffle=True,
collate_fn=collate_fn,
pin_memory=True,
)
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 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_loader = DataLoader(
dataset,
batch_size=config["batch_size"],
num_workers=config["workers"],
shuffle=True,
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_loader = DataLoader(
dataset,
batch_size=config["val_batch_size"],
num_workers=config["val_workers"],
shuffle=True,
collate_fn=collate_fn,
pin_memory=True,
)
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 = 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"] / (config["batch_size"])
)
val_iters = int(config["val_iters"] / (config["batch_size"]))
start_time = time.time()
metrics = dict()
for i, data in enumerate(train_loader):
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 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)
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.state_dict()},
os.path.join(save_dir, save_name),
)
if __name__ == "__main__":
main()