forked from liux0614/yolo_nano
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
112 lines (94 loc) · 3.87 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
import os
import time
import json
import torch
from torch import nn
from torch import optim
from adabound import AdaBound
from models.get_model import get_model
from data.get_dataset import get_train_dataset, get_val_dataset, get_test_dataset
from transforms.get_transforms import get_train_transforms, get_val_transforms, get_test_transforms
from utils.opts import Opt
from utils.logger import Logger
from utils.visualizer import Visualizer
from train import train
from val import val
from test import test
if __name__ == "__main__":
opt = Opt().parse()
########################################
# Model #
########################################
torch.manual_seed(opt.manual_seed)
if opt.no_vis:
visualizer = None
else:
visualizer = Visualizer(opt)
model = get_model(opt)
if opt.optimizer == 'Adam':
optimizer = torch.optim.Adam(
model.parameters(), lr=opt.lr, weight_decay=opt.weight_decay)
elif opt.optimizer == 'AdaBound':
optimizer = AdaBound(
model.parameters(), lr=opt.lr, final_lr=0.1, weight_decay=opt.weight_decay)
elif opt.optimizer == 'SGD':
optimizer = torch.optim.SGD(
model.parameters(), lr=opt.lr, momentum=opt.momentum, weight_decay=opt.weight_decay)
else:
NotImplementedError("Only Adam and SGD are supported")
########################################
# Transforms #
########################################
if not opt.no_train:
train_transforms = get_train_transforms(opt)
train_dataset = get_train_dataset(opt, train_transforms)
train_dataloader = torch.utils.data.DataLoader(
train_dataset,
batch_size=opt.batch_size,
shuffle=True,
num_workers=opt.num_threads,
collate_fn=train_dataset.collate_fn
)
train_logger = Logger(os.path.join(opt.checkpoint_path, 'train.log'))
if not opt.no_val:
val_transforms = get_val_transforms(opt)
val_dataset = get_val_dataset(opt, val_transforms)
val_dataloader = torch.utils.data.DataLoader(
val_dataset,
batch_size=opt.batch_size,
shuffle=False,
num_workers=opt.num_threads,
collate_fn=val_dataset.collate_fn
)
val_logger = Logger(os.path.join(opt.checkpoint_path, 'val.log'))
if opt.test:
test_transforms = get_test_transforms(opt)
test_dataset = get_test_dataset(opt, test_transforms)
test_dataloader = torch.utils.data.DataLoader(
test_dataset,
batch_size=opt.batch_size,
shuffle=False,
num_workers=opt.num_threads,
collate_fn=test_dataset.collate_fn
)
test_logger = Logger(os.path.join(opt.checkpoint_path, 'test.log'))
if opt.resume_path:
print('loading checkpoint {}'.format(opt.resume_path))
checkpoint = torch.load(opt.resume_path)
assert opt.model == checkpoint['model']
opt.begin_epoch = checkpoint['epoch']
model.load_state_dict(checkpoint['state_dict'])
if not opt.no_train:
optimizer.load_state_dict(checkpoint['optimizer'])
########################################
# Train, Val, Test #
########################################
for epoch in range(opt.begin_epoch, opt.num_epochs + 1):
if not opt.no_train:
print("\n---- Training Model ----")
train(model, optimizer, train_dataloader, epoch, opt, train_logger, visualizer)
if not opt.no_val and (epoch+1) % opt.val_interval == 0:
print("\n---- Evaluating Model ----")
val(model, val_dataloader, epoch, opt, val_logger, visualizer)
if opt.test:
test(model, test_dataloader, epoch, opt, test_logger, visualizer)