-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
213 lines (190 loc) · 7.65 KB
/
main.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, sys
import gc
import torch.nn.parallel
import torch.optim
import torch.utils.data
from tensorboardX import SummaryWriter
import numpy as np
import shutil
import data_loader
import nets
from utils.helper import query_yes_no, load_config
from iterater import iterater
from eval import test, val
import yaml
def main():
# ensure numba JIT is on
if 'NUMBA_DISABLE_JIT' in os.environ:
del os.environ['NUMBA_DISABLE_JIT']
# parse arguments
global args
with open(sys.argv[1], 'r') as stream:
args = yaml.safe_load(stream)
if args['resume'] is not False:
args = load_config(args['resume'], args)
cuda = torch.cuda.is_available()
if cuda :
import torch.backends.cudnn as cudnn
cudnn.benchmark = True
device = torch.device("cuda")
else:
device = torch.device("cpu")
print("=> using '{}' for computation.".format(device))
# -------------------- logging args --------------------
print("=> checking ckpt dir...")
if args['test'] is False and os.path.exists(args['ckpt_dir']):
if args['resume'] is False:
to_continue = query_yes_no(
'=> Attention!!! ckpt_dir {' + args['ckpt_dir'] + '} already exists!\n'
+ '=> Whether to continue?',
default=None)
if to_continue:
for root, dirs, files in os.walk(args['ckpt_dir'], topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
else:
sys.exit(1)
elif args['pre_trained'] is not False:
to_continue = query_yes_no(
'=> Attention!!! ckpt_dir {' + args['ckpt_dir'] +
'} already exists! Whether to continue?',
default=None)
if to_continue:
for root, dirs, files in os.walk(args['ckpt_dir'], topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
else:
sys.exit(1)
if not args['test']:
os.makedirs(args['ckpt_dir'], mode=0o777, exist_ok=True)
shutil.copyfile(sys.argv[1], os.path.join(args['ckpt_dir'], 'config.yaml'))
summary = SummaryWriter(args['ckpt_dir'])
# -------------------- dataset & loader --------------------
loader = {}
if not args['test']:
train_dataset = data_loader.__dict__[args['dataset']](
split='train',
args=args
)
train_loader = torch.utils.data.DataLoader(
train_dataset,
batch_size=args['batch_size'],
shuffle=True,
num_workers=args['workers'],
pin_memory=True,
worker_init_fn=lambda x: np.random.seed((torch.initial_seed()) % (2 ** 32))
)
loader['train'] = train_loader
val_dataset = data_loader.__dict__[args['dataset']](
split='valid',
args=args
)
val_loader = torch.utils.data.DataLoader(
val_dataset,
batch_size=args['batch_size'],
shuffle=False,
num_workers=args['workers'],
pin_memory=True,
worker_init_fn=lambda x: np.random.seed((torch.initial_seed()) % (2 ** 32))
)
loader['validate'] = val_loader
elif args['test'] == 'valid' or args['test'] == 'test':
test_dataset = data_loader.__dict__[args['dataset']](
split=args['test'],
args=args
)
# print('val_dataset: ' + str(val_dataset))
test_loader = torch.utils.data.DataLoader(
test_dataset,
batch_size=args['batch_size'],
shuffle=False,
num_workers=args['workers'],
pin_memory=True,
worker_init_fn=lambda x: np.random.seed((torch.initial_seed()) % (2 ** 32))
)
# -------------------- create model --------------------
print("=> creating model and optimizer ... ", end='')
model = nets.__dict__[args['arch']](args).to(device)
if not args['test']:
model_named_params = [
p for _, p in model.named_parameters() if p.requires_grad
]
optimizer = torch.optim.Adam(model_named_params,
lr=args['lr'],
weight_decay=args['weight_decay'])
# define loss functions
# criterion = nets.__dict__[args['loss']]()
criterion = nets.Criterion(args)
print("=> completed.")
print("=> total parameters: {:.5f}M".format(
sum(p.numel() for p in model.parameters())/1000000.0))
model = torch.nn.DataParallel(model)
# -------------------- resume --------------------
if args['test']:
if os.path.isfile(args['resume']):
print("=> loading checkpoint '{}'".format(args['resume']))
checkpoint = torch.load(args['resume'])
model.load_state_dict(checkpoint['state_dict'], strict=True)
print("=> completed.")
print("=> start iter {}, min loss {}"
.format(checkpoint['iter'], checkpoint['min_loss']))
args['iter'] = checkpoint['iter']
if args['test'] == 'valid':
val(test_loader, model, args)
elif args['test'] == 'test':
test(test_loader, model, args)
return
else:
print("=> no checkpoint found at '{}'".format(args['resume']))
return
elif args['resume']:
if os.path.isfile(args['resume']):
print("=> loading checkpoint '{}'".format(args['resume']))
checkpoint = torch.load(args['resume'])
args['iter'] = checkpoint['iter']
model.load_state_dict(checkpoint['state_dict'], strict=True)
optimizer.load_state_dict(checkpoint['optimizer'])
print("=> completed.")
print("=> start iter {}, min loss {}"
.format(checkpoint['iter'], checkpoint['min_loss']))
else:
print("=> no checkpoint found at '{}'".format(args['resume']))
return
elif args['pre_trained']:
if os.path.isfile(args['pre_trained']):
print("=> loading checkpoint '{}'".format(args['pre_trained']))
checkpoint = torch.load(args['pre_trained'])
pretrained_dict = checkpoint['state_dict']
model_dict = model.state_dict()
pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}
model_dict.update(pretrained_dict)
model.load_state_dict(pretrained_dict, strict=False)
print("=> completed.")
else:
print("=> no checkpoint found at '{}'".format(args['pre_trained']))
return
# -------------------- main loop --------------------
it_dict = {}
if args['resume']:
it_dict['iter'] = args['iter'] + 1
it_dict['min_train_loss'] = None
it_dict['best_train_iter'] = None
it_dict['min_val_loss'] = checkpoint['min_loss']
it_dict['best_val_iter'] = args['iter']
else:
it_dict['iter'] = 0
it_dict['min_train_loss'] = None
it_dict['best_train_iter'] = None
it_dict['min_val_loss'] = None
it_dict['best_val_iter'] = None
while it_dict['iter'] < args['epochs'] * len(loader['train']):
it_dict = \
iterater(loader, model, criterion, optimizer, args, summary, it_dict)
gc.collect()
return
if __name__ == '__main__':
main()