forked from Pilhyeon/BaSNet-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
84 lines (64 loc) · 2.95 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
import pdb
import numpy as np
import torch.utils.data as data
import utils
from options import *
from config import *
from train import *
from test import *
from model import *
from tensorboard_logger import Logger
from thumos_features import *
if __name__ == "__main__":
args = parse_args()
if args.debug:
pdb.set_trace()
config = Config(args)
worker_init_fn = None
if config.seed >= 0:
utils.set_seed(config.seed)
worker_init_fn = np.random.seed(config.seed)
net = BaS_Net(config.len_feature, config.num_classes, config.num_segments)
net = net.cuda()
train_loader = data.DataLoader(
ThumosFeature(data_path=config.data_path, mode='train',
modal=config.modal, feature_fps=config.feature_fps,
num_segments=config.num_segments, len_feature=config.len_feature,
seed=config.seed, sampling='random'),
batch_size=config.batch_size,
shuffle=True, num_workers=config.num_workers,
worker_init_fn=worker_init_fn)
test_loader = data.DataLoader(
ThumosFeature(data_path=config.data_path, mode='test',
modal=config.modal, feature_fps=config.feature_fps,
num_segments=config.num_segments, len_feature=config.len_feature,
seed=config.seed, sampling='uniform'),
batch_size=1,
shuffle=False, num_workers=config.num_workers,
worker_init_fn=worker_init_fn)
test_info = {"step": [], "test_acc": [], "average_mAP": [],
best_mAP = -1
criterion = BaS_Net_loss(config.alpha)
optimizer = torch.optim.Adam(net.parameters(), lr=config.lr[0],
betas=(0.9, 0.999), weight_decay=0.0005)
logger = Logger(config.log_path)
loader_iter = iter(train_loader)
for step in tqdm(
range(1, config.num_iters + 1),
total = config.num_iters,
dynamic_ncols = True
):
if step > 1 and config.lr[step - 1] != config.lr[step - 2]:
for param_group in optimizer.param_groups:
param_group["lr"] = config.lr[step - 1]
train(net, train_loader, loader_iter, optimizer, criterion, logger, step)
test(net, config, logger, test_loader, test_info, step)
if test_info["average_mAP"][-1] > best_mAP:
best_mAP = test_info["average_mAP"][-1]
utils.save_best_record_thumos(test_info,
os.path.join(config.output_path, "best_record_seed_{}.txt".format(config.seed)))
torch.save(net.state_dict(), os.path.join(args.model_path, \
"BaS_Net_model_seed_{}.pkl".format(config.seed)))