-
Notifications
You must be signed in to change notification settings - Fork 22
/
main.py
116 lines (89 loc) · 3.84 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
113
114
115
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import os
from config import load_config
from preprocess import load_data
from model import LambdaResNet18, get_n_params
def save_checkpoint(best_acc, model, optimizer, args, epoch):
print('Best Model Saving...')
if args.device_num > 1:
model_state_dict = model.module.state_dict()
else:
model_state_dict = model.state_dict()
torch.save({
'model_state_dict': model_state_dict,
'global_epoch': epoch,
'optimizer_state_dict': optimizer.state_dict(),
'best_acc': best_acc,
}, os.path.join('checkpoints', 'checkpoint_model_best.pth'))
def _train(epoch, train_loader, model, optimizer, criterion, args):
model.train()
losses = 0.
acc = 0.
total = 0.
for idx, (data, target) in enumerate(train_loader):
if args.cuda:
data, target = data.cuda(), target.cuda()
output = model(data)
_, pred = F.softmax(output, dim=-1).max(1)
acc += pred.eq(target).sum().item()
total += target.size(0)
optimizer.zero_grad()
loss = criterion(output, target)
losses += loss
loss.backward()
if args.gradient_clip > 0:
torch.nn.utils.clip_grad_norm_(model.parameters(), args.gradient_clip)
optimizer.step()
if idx % args.print_intervals == 0 and idx != 0:
print('[Epoch: {0:4d}], Loss: {1:.3f}, Acc: {2:.3f}, Correct {3} / Total {4}'.format(epoch,
losses / (idx + 1),
acc / total * 100.,
acc, total))
def _eval(epoch, test_loader, model, args):
model.eval()
acc = 0.
with torch.no_grad():
for data, target in test_loader:
if args.cuda:
data, target = data.cuda(), target.cuda()
output = model(data)
_, pred = F.softmax(output, dim=-1).max(1)
acc += pred.eq(target).sum().item()
print('[Epoch: {0:4d}], Acc: {1:.3f}'.format(epoch, acc / len(test_loader.dataset) * 100.))
return acc / len(test_loader.dataset) * 100.
def main(args):
train_loader, test_loader = load_data(args)
model = LambdaResNet18()
print('Model Parameters: {}'.format(get_n_params(model)))
optimizer = optim.SGD(model.parameters(), lr=args.lr, weight_decay=args.weight_decay, momentum=args.momentum)
if not os.path.isdir('checkpoints'):
os.mkdir('checkpoints')
if args.checkpoints is not None:
checkpoints = torch.load(os.path.join('checkpoints', args.checkpoints))
model.load_state_dict(checkpoints['model_state_dict'])
optimizer.load_state_dict(checkpoints['optimizer_state_dict'])
start_epoch = checkpoints['global_epoch']
else:
start_epoch = 1
if args.cuda:
model = model.cuda()
if not args.evaluation:
criterion = nn.CrossEntropyLoss()
lr_scheduler = optim.lr_scheduler.CosineAnnealingWarmRestarts(optimizer, T_0=10, T_mult=2, eta_min=0.0001)
global_acc = 0.
for epoch in range(start_epoch, args.epochs + 1):
_train(epoch, train_loader, model, optimizer, criterion, args)
best_acc = _eval(epoch, test_loader, model, args)
if global_acc < best_acc:
global_acc = best_acc
save_checkpoint(best_acc, model, optimizer, args, epoch)
lr_scheduler.step()
print('Current Learning Rate: {}'.format(lr_scheduler.get_last_lr()))
else:
_eval(start_epoch, test_loader, model, args)
if __name__ == '__main__':
args = load_config()
main(args)