-
Notifications
You must be signed in to change notification settings - Fork 4
/
train.py
293 lines (246 loc) · 9.67 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import argparse
import time
import numpy as np
import torch
from torch.utils.tensorboard import SummaryWriter
from torch.utils.data import DataLoader
import models
from eval.evaluate import recall_atN
from loader_loss.pairwise_margin import (
PairwiseData,
PairwiseMarginLoss,
hard_mining,
train_loader_generator,
)
from loader_loss.testdata import get_test_set
from utils import (
get_logger,
load_config,
count_parameters,
get_current_lr,
load_checkpoint,
save_checkpoint,
)
# parameter parser
parser = argparse.ArgumentParser(description="PyTorch Training")
parser.add_argument("--work-path", required=True, type=str, help="working directory")
parser.add_argument("--lr", type=float, help="assign an new learning rate.")
parser.add_argument("--best_prec", type=float, help="assign an new best_prec.")
parser.add_argument("--resume", action="store_true", help="resume from checkpoint")
args = parser.parse_args()
# logger, to record messages
logger = get_logger(
log_file_name=args.work_path + "/log.txt", log_level="DEBUG", logger_name="CIFAR"
)
# tensorBoard writter
writer = SummaryWriter(log_dir=args.work_path + "/event")
# load config from yaml file
config = load_config(args.work_path + "/config.yaml")
logger.info(config)
# variables for recording
best_prec = 0 #
#############################################################
# The following variables are used GLOBALLY
# tools: ``args``, ``logger``, ``writer``, ``config``
# records: ``best_prec``
#############################################################
def test(test_loader, net, optimizer, epoch, device):
"""Test/Evaluate the network performance (Recall @1). Also save the model."""
global best_prec
net.eval() # change to 'evaluate' stage
features = []
logger.info(" === Validation ===")
# No need for gradient in evaluation stage
# Calculate features for all items in the test set
with torch.no_grad():
for image, pc in test_loader:
image, pc = image.to(device), pc.to(device)
batch_feature = net(image, pc)
batch_feature = batch_feature.detach().cpu().numpy()
features.append(batch_feature)
features = np.vstack(features)
# get some variables
pos_items = test_loader.dataset.get_pos_items()
num_of_each_run = test_loader.dataset.get_num_of_each_run() # [100, 102, ...]
sum_num_of_each_run = [
sum(num_of_each_run[:i]) for i in range(len(num_of_each_run))
] # [0, 100, 202, ...]
run_num = len(num_of_each_run)
# compute evaluation metric
recall_1s = []
meanAPs = []
pairs = ((i, j) for i in range(run_num) for j in range(i + 1, run_num))
for i, j in pairs:
st1 = sum_num_of_each_run[i]
st2 = sum_num_of_each_run[j]
end1 = st1 + num_of_each_run[i]
end2 = st2 + num_of_each_run[j]
feature_of_two_run = np.vstack((features[st1:end1], features[st2:end2]))
pos_items_of_two_run = pos_items[(i, j)]
recall_1 = recall_atN(
feature_of_two_run, pos_items_of_two_run, N=1, Lp=config.loss.Lp
)
recall_1s.append(recall_1)
# show and record test results
recall_1 = np.mean(recall_1s)
meanAP = np.mean(meanAPs)
logger.info(f" == test recall@1: {recall_1:.4f}")
writer.add_scalar("test_recall_1", recall_1)
# judge best testing
is_best = recall_1 > best_prec
if is_best:
best_prec = recall_1
# Save checkpoint.
state = {
"state_dict": net.state_dict(),
"best_prec": best_prec,
"last_epoch": epoch,
"optimizer": optimizer.state_dict(),
}
save_checkpoint(state, is_best, args.work_path + "/" + config.ckpt_name)
logger.info(
f" == save checkpoint, recall@1={recall_1:.4}, is_best={is_best}, "
f"best={best_prec:.4} =="
)
net.train() # change to 'train' stage
return recall_1
def train(loaders, net, criterion, optimizer, lr_scheduler, epoch, device):
"""Train the network for one epoch."""
train_loader, test_loader = loaders
start_time = time.time()
net.train() # change to 'train' stage
train_loss_sum = 0
batch_total_num = len(train_loader)
logger.info(f" === start Epoch: [{epoch + 1}/{config.epochs}] ===")
# use batch to iterate through the dataset (one epoch)
for batch_index, pairs in enumerate(train_loader):
# ========== Hard mining ==========
if config.hardM.enabled:
if batch_index == 0:
hard_mining_loader = train_loader_generator(train_loader)
if pairs["pos_pair"][0][0].shape[0] != config.train_batch_size:
continue # Skip the last batch
if batch_index % config.hardM.hardM_freq == 0:
hard_pair, hard_loss = hard_mining(
hard_mining_loader, net, criterion, device, config
)
logger.info(
f" == sampling hard sample, top hard loss={hard_loss:.3f} =="
)
pairs["hard_pair"] = hard_pair
# ========== Forward ==========
loss = 0.0
for pair_key, pair_data in pairs.items():
y = 1 if pair_key == "pos_pair" else -1
x = [] # store x1 and x2, features representation of pairs
# each pair contains two X=(image, pc)
for image, pc in pair_data:
image, pc = image.to(device), pc.to(device)
x.append(net(image, pc))
loss += criterion(*x, y) # of size [N]
loss = torch.mean(loss)
# ========== Backward & Update ==========
optimizer.zero_grad() # zero the gradient buffers
loss.backward() # backward
optimizer.step() # update weight
# ========== Counting and Statistics ==========
train_loss_sum += loss.item()
# ========== Show Infomation ==========
if batch_index % config.show_freq == 0:
logger.info(
f" == for step: [{batch_index+1:5}/{batch_total_num:5}], "
f"train loss: {loss.item():.3f} | "
f"lr: {get_current_lr(optimizer):.5f}"
)
# ========== Eval & Save Checkpoint ==========
if (batch_index + 1) % config.eval_freq == 0:
recall_1 = test(test_loader, net, optimizer, epoch, device)
lr_scheduler.step(recall_1) # adjust learning rate if no improvement
writer.add_scalar("learning_rate", get_current_lr(optimizer))
# <-- end for batch
# record time for one epoch, train loss and train accuracy
train_loss_avg = train_loss_sum / batch_total_num
logger.info(f" == cost time: {time.time() - start_time:.4f}s")
logger.info(f" == average train loss: {train_loss_avg:.3f}")
writer.add_scalar("train_loss", train_loss_avg, global_step=epoch)
return train_loss_avg
def main():
global best_prec
logger.info("\n\n\n" + "=" * 15 + " New Run " + "=" * 15)
# define netowrk
net = models.get_model(config)
logger.info(net)
logger.info(f" == total parameters: {count_parameters(net)} ==")
# CPU or GPU
device = "cuda" if config.use_gpu else "cpu"
logger.info(f" == will be trained on device: {device} ==")
if device == "cuda": # data parallel for multiple-GPU
net = torch.nn.DataParallel(net)
torch.backends.cudnn.benchmark = True
net.to(device)
# define loss and optimizer
criterion = PairwiseMarginLoss(config.loss.a, config.loss.m, config.loss.Lp)
optimizer = torch.optim.Adam(
net.parameters(),
config.optimize.base_lr,
betas=config.optimize.betas,
weight_decay=config.optimize.weight_decay,
amsgrad=config.optimize.amsgrad,
)
lr_scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(
optimizer,
mode="max",
factor=config.lr_scheduler.factor,
patience=config.lr_scheduler.patience,
cooldown=config.lr_scheduler.cooldown,
min_lr=config.optimize.base_lr / 10,
)
# resume from a checkpoint
last_epoch = -1
ckpt_file_name = args.work_path + "/" + config.ckpt_name
if args.resume:
best_prec, last_epoch = load_checkpoint(ckpt_file_name, net, optimizer)
lr_scheduler.step(best_prec)
# overwrite learning rate
if args.lr is not None:
logger.info(f"learning rate is overwritten to {args.lr:.5}")
for param_group in optimizer.param_groups:
param_group["lr"] = args.lr
# overwrite best prediction
if args.best_prec is not None:
logger.info(f"best_prec is overwritten to {args.best_prec:.4}")
best_prec = args.best_prec
# load training and testing data loader
train_loader = DataLoader(
PairwiseData(config=config),
batch_size=config.train_batch_size,
shuffle=True,
num_workers=config.workers,
drop_last=True,
)
test_loader = DataLoader(
get_test_set(config),
batch_size=config.test_batch_size,
shuffle=False,
num_workers=config.workers,
)
# start training --->
logger.info("============== Start Training ==============\n")
for epoch in range(last_epoch + 1, config.epochs):
random_int = torch.randint(200, 7000, (1,)).item()
logger.info(f" === random number for dataloader this epoch: {random_int} ===")
train_loader.dataset.shuffle_data(random_int)
train(
(train_loader, test_loader),
net,
criterion,
optimizer,
lr_scheduler,
epoch,
device,
)
# ---> training finished
logger.info(f"======== Training Finished. best_test_acc: {best_prec:.3%} ========")
writer.close()
if __name__ == "__main__":
main()