-
Notifications
You must be signed in to change notification settings - Fork 12
/
train.py
217 lines (192 loc) · 9.01 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
from __future__ import division
import sys
import os
import random
import argparse
from pathlib import Path
import torch
from torch.utils import data
import numpy as np
from tqdm import tqdm
from models import get_model
from datasets import get_dataset
from loss import *
from utils import *
def train(args):
# prepare datasets
if args.dataset == 'i19S':
datasetSs = get_dataset('7S')
datasetTs = get_dataset('12S')
datasetSs = datasetSs(args.data_path, args.dataset, model=args.model,
aug=args.aug)
datasetTs = datasetTs(args.data_path, args.dataset, model=args.model,
aug=args.aug)
dataset = data.ConcatDataset([datasetSs,datasetTs])
else:
if args.dataset in ['7S', 'i7S']:
dataset = get_dataset('7S')
if args.dataset in ['12S', 'i12S']:
dataset = get_dataset('12S')
if args.dataset == 'Cambridge':
dataset = get_dataset('Cambridge')
dataset = dataset(args.data_path, args.dataset, args.scene,
model=args.model, aug=args.aug)
trainloader = data.DataLoader(dataset, batch_size=args.batch_size,
num_workers=4, shuffle=True)
# loss
reg_loss = EuclideanLoss()
if args.model == 'hscnet':
cls_loss = CELoss()
if args.dataset in ['i7S', 'i12S', 'i19S']:
w1, w2, w3 = 1, 1, 100000
else:
w1, w2, w3 = 1, 1, 10
# prepare model and optimizer
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = get_model(args.model, args.dataset)
model.init_weights()
model.to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=args.init_lr, eps=1e-8,
betas=(0.9, 0.999))
# resume from existing or start a new session
if args.resume is not None:
if os.path.isfile(args.resume):
print("Loading model and optimizer from checkpoint '{}'".format\
(args.resume))
checkpoint = torch.load(args.resume, map_location=device)
model.load_state_dict(checkpoint['model_state'])
optimizer.load_state_dict(checkpoint['optimizer_state'])
print("Loaded checkpoint '{}' (epoch{})".format(args.resume,
checkpoint['epoch']))
save_path = Path(args.resume)
args.save_path = save_path.parent
start_epoch = checkpoint['epoch'] + 1
else:
print("No checkpoint found at '{}'".format(args.resume))
sys.exit()
else:
if args.dataset in ['i7S', 'i12S', 'i19S']:
model_id = "{}-{}-initlr{}-iters{}-bsize{}-aug{}-{}".format(\
args.dataset, args.model, args.init_lr, args.n_iter,
args.batch_size, int(args.aug), args.train_id)
else:
model_id = "{}-{}-{}-initlr{}-iters{}-bsize{}-aug{}-{}".format(\
args.dataset, args.scene.replace('/','.'),
args.model, args.init_lr, args.n_iter, args.batch_size,
int(args.aug), args.train_id)
save_path = Path(model_id)
args.save_path = 'checkpoints'/save_path
args.save_path.mkdir(parents=True, exist_ok=True)
start_epoch = 1
# start training
args.n_epoch = int(np.ceil(args.n_iter * args.batch_size / len(dataset)))
for epoch in range(start_epoch, args.n_epoch+1):
lr = adjust_lr(optimizer, args.init_lr, (epoch - 1)
* np.ceil(len(dataset) / args.batch_size),
args.n_iter)
model.train()
train_loss_list = []
coord_loss_list = []
if args.model == 'hscnet':
lbl_1_loss_list = []
lbl_2_loss_list = []
for _, (img, coord, mask, lbl_1, lbl_2, lbl_1_oh,
lbl_2_oh) in enumerate(tqdm(trainloader)):
if mask.sum() == 0:
continue
optimizer.zero_grad()
img = img.to(device)
coord = coord.to(device)
mask = mask.to(device)
if args.model == 'hscnet':
lbl_1 = lbl_1.to(device)
lbl_2 = lbl_2.to(device)
lbl_1_oh = lbl_1_oh.to(device)
lbl_2_oh = lbl_2_oh.to(device)
coord_pred, lbl_2_pred, lbl_1_pred = model(img,lbl_1_oh,
lbl_2_oh)
lbl_1_loss = cls_loss(lbl_1_pred, lbl_1, mask)
lbl_2_loss = cls_loss(lbl_2_pred, lbl_2, mask)
coord_loss = reg_loss(coord_pred, coord, mask)
train_loss = w3*coord_loss + w1*lbl_1_loss + w2*lbl_2_loss
else:
coord_pred = model(img)
coord_loss = reg_loss(coord_pred, coord, mask)
train_loss = coord_loss
coord_loss_list.append(coord_loss.item())
if args.model == 'hscnet':
lbl_1_loss_list.append(lbl_1_loss.item())
lbl_2_loss_list.append(lbl_2_loss.item())
train_loss_list.append(train_loss.item())
train_loss.backward()
optimizer.step()
with open(args.save_path/args.log_summary, 'a') as logfile:
if args.model == 'hscnet':
logtt = 'Epoch {}/{} - lr: {} - reg_loss: {} - cls_loss_1: {}' \
' - cls_loss_2: {} - train_loss: {} \n'.format(
epoch, args.n_epoch, lr, np.mean(coord_loss_list),
np.mean(lbl_1_loss_list), np.mean(lbl_2_loss_list),
np.mean(train_loss_list))
else:
logtt = 'Epoch {}/{} - lr: {} - reg_loss: {} - train_loss: {}' \
'\n'.format(
epoch, args.n_epoch, lr, np.mean(coord_loss_list),
np.mean(train_loss_list))
print(logtt)
logfile.write(logtt)
if epoch % int(np.floor(args.n_epoch / 5.)) == 0:
save_state(args.save_path, epoch, model, optimizer)
save_state(args.save_path, epoch, model, optimizer)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Hscnet")
parser.add_argument('--model', nargs='?', type=str, default='hscnet',
choices=('hscnet', 'scrnet'),
help='Model to use [\'hscnet, scrnet\']')
parser.add_argument('--dataset', nargs='?', type=str, default='7S',
choices=('7S', '12S', 'i7S', 'i12S', 'i19S',
'Cambridge'), help='Dataset to use')
parser.add_argument('--scene', nargs='?', type=str, default='heads',
help='Scene')
parser.add_argument('--n_iter', nargs='?', type=int, default=900000,
help='# of iterations (to reproduce the results from ' \
'the paper, 300K for 7S and 12S, 600K for ' \
'Cambridge, 900K for the combined scenes)')
parser.add_argument('--init_lr', nargs='?', type=float, default=1e-4,
help='Initial learning rate')
parser.add_argument('--batch_size', nargs='?', type=int, default=1,
help='Batch size')
parser.add_argument('--aug', nargs='?', type=str2bool, default=True,
help='w/ or w/o data augmentation')
parser.add_argument('--resume', nargs='?', type=str, default=None,
help='Path to saved model to resume from')
parser.add_argument('--data_path', required=True, type=str,
help='Path to dataset')
parser.add_argument('--log-summary', default='progress_log_summary.txt',
metavar='PATH',
help='txt where to save per-epoch stats')
parser.add_argument('--train_id', nargs='?', type=str, default='',
help='An identifier string')
args = parser.parse_args()
if args.dataset == '7S':
if args.scene not in ['chess', 'heads', 'fire', 'office', 'pumpkin',
'redkitchen','stairs']:
print('Selected scene is not valid.')
sys.exit()
if args.dataset == '12S':
if args.scene not in ['apt1/kitchen', 'apt1/living', 'apt2/bed',
'apt2/kitchen', 'apt2/living', 'apt2/luke',
'office1/gates362', 'office1/gates381',
'office1/lounge', 'office1/manolis',
'office2/5a', 'office2/5b']:
print('Selected scene is not valid.')
sys.exit()
if args.dataset == 'Cambridge':
if args.scene not in ['GreatCourt', 'KingsCollege', 'OldHospital',
'ShopFacade', 'StMarysChurch']:
print('Selected scene is not valid.')
sys.exit()
seed = 0
np.random.seed(seed)
torch.manual_seed(seed)
random.seed(seed)
train(args)