-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
53 lines (40 loc) · 1.59 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
from __future__ import print_function
import argparse
import os
import torch
from model import Model
from video_dataset import Dataset
from test import test
from train import train
import utils
from tensorboard_logger import Logger
import options
torch.set_default_tensor_type('torch.cuda.FloatTensor')
import torch.optim as optim
if __name__ == '__main__':
print('Started')
args = options.parser.parse_args()
torch.manual_seed(args.seed)
torch.cuda.manual_seed_all(args.seed)
device = torch.device("cuda") if args.cuda else torch.device("cpu")
dataset = Dataset(args)
os.system('mkdir -p ./ckpt/')
os.system('mkdir -p ./logs/' + args.model_name)
logger = Logger('./logs/' + args.model_name)
model = Model(dataset.feature_size, dataset.num_class, args, dataset.labels101to20).to(device)
if args.pretrained_ckpt is not None:
model.load_state_dict(torch.load(args.pretrained_ckpt))
print(model)
best_acc = 0
optimizer = optim.Adam(model.parameters(), lr=args.lr, weight_decay=0.0005)
for itr in range(args.max_iter):
train(itr, dataset, args, model, optimizer, logger, device)
if itr % args.test_iter == 0 and itr>0:
if itr == args.test_iter:
utils.write_summary(args.dataset_name + args.model_name, args.summary)
acc = test(itr, dataset, args, model, logger, device)
print(args.summary)
if acc > best_acc:
torch.save(model.state_dict(), './ckpt/' + args.model_name + '.pkl')
best_acc = acc
print('Done')