forked from zhanghang1989/PyTorch-Encoding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
172 lines (156 loc) · 5.86 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
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
##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
## Created by: Hang Zhang
## ECE Department, Rutgers University
## Email: zhang.hang@rutgers.edu
## Copyright (c) 2017
##
## This source code is licensed under the MIT-style license found in the
## LICENSE file in the root directory of this source tree
##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
from __future__ import print_function
import os
import matplotlib.pyplot as plot
import importlib
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.autograd import Variable
from option import Options
from encoding.utils import *
from tqdm import tqdm
# global variable
best_pred = 100.0
errlist_train = []
errlist_val = []
def main():
# init the args
global best_pred, errlist_train, errlist_val
args = Options().parse()
args.cuda = not args.no_cuda and torch.cuda.is_available()
torch.manual_seed(args.seed)
# plot
if args.plot:
print('=>Enabling matplotlib for display:')
plot.ion()
plot.show()
if args.cuda:
torch.cuda.manual_seed(args.seed)
# init dataloader
dataset = importlib.import_module('dataset.'+args.dataset)
Dataloder = dataset.Dataloder
train_loader, test_loader = Dataloder(args).getloader()
# init the model
models = importlib.import_module('model.'+args.model)
model = models.Net(args)
print(model)
# criterion and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = get_optimizer(args, model, False)
if args.cuda:
model.cuda()
# Please use CUDA_VISIBLE_DEVICES to control the number of gpus
model = torch.nn.DataParallel(model)
# check point
if args.resume is not None:
if os.path.isfile(args.resume):
print("=> loading checkpoint '{}'".format(args.resume))
checkpoint = torch.load(args.resume)
args.start_epoch = checkpoint['epoch'] +1
best_pred = checkpoint['best_pred']
errlist_train = checkpoint['errlist_train']
errlist_val = checkpoint['errlist_val']
model.load_state_dict(checkpoint['state_dict'])
optimizer.load_state_dict(checkpoint['optimizer'])
print("=> loaded checkpoint '{}' (epoch {})"
.format(args.resume, checkpoint['epoch']))
else:
raise RuntimeError ("=> no resume checkpoint found at '{}'".\
format(args.resume))
scheduler = LR_Scheduler(args, len(train_loader))
def train(epoch):
model.train()
global best_pred, errlist_train
train_loss, correct, total = 0,0,0
#adjust_learning_rate(optimizer, args, epoch, best_pred)
tbar = tqdm(train_loader, desc='\r')
for batch_idx, (data, target) in enumerate(tbar):
scheduler(optimizer, batch_idx, epoch, best_pred)
if args.cuda:
data, target = data.cuda(), target.cuda()
data, target = Variable(data), Variable(target)
optimizer.zero_grad()
output = model(data)
loss = criterion(output, target)
loss.backward()
optimizer.step()
train_loss += loss.data[0]
pred = output.data.max(1)[1]
correct += pred.eq(target.data).cpu().sum()
total += target.size(0)
err = 100-100.*correct/total
tbar.set_description('\rLoss: %.3f | Err: %.3f%% (%d/%d)' % \
(train_loss/(batch_idx+1), err, total-correct, total))
errlist_train += [err]
def test(epoch):
model.eval()
global best_pred, errlist_train, errlist_val
test_loss, correct, total = 0,0,0
is_best = False
tbar = tqdm(test_loader, desc='\r')
for batch_idx, (data, target) in enumerate(tbar):
if args.cuda:
data, target = data.cuda(), target.cuda()
data, target = Variable(data), Variable(target)
with torch.no_grad():
output = model(data)
test_loss += criterion(output, target).data.item()
# get the index of the max log-probability
pred = output.data.max(1)[1]
correct += pred.eq(target.data).cpu().sum().item()
total += target.size(0)
err = 100-100.0*correct/total
tbar.set_description('Loss: %.3f | Err: %.3f%% (%d/%d)'% \
(test_loss/(batch_idx+1), err, total-correct, total))
if args.eval:
print('Error rate is %.3f'%err)
return
# save checkpoint
errlist_val += [err]
if err < best_pred:
best_pred = err
is_best = True
save_checkpoint({
'epoch': epoch,
'state_dict': model.state_dict(),
'optimizer': optimizer.state_dict(),
'best_pred': best_pred,
'errlist_train':errlist_train,
'errlist_val':errlist_val,
}, args=args, is_best=is_best)
if args.plot:
plot.clf()
plot.xlabel('Epoches: ')
plot.ylabel('Error Rate: %')
plot.plot(errlist_train, label='train')
plot.plot(errlist_val, label='val')
plot.legend(loc='upper left')
plot.draw()
plot.pause(0.001)
if args.eval:
test(args.start_epoch)
return
for epoch in range(args.start_epoch, args.epochs + 1):
train(epoch)
test(epoch)
# save train_val curve to a file
if args.plot:
plot.clf()
plot.xlabel('Epoches: ')
plot.ylabel('Error Rate: %')
plot.plot(errlist_train, label='train')
plot.plot(errlist_val, label='val')
plot.savefig("runs/%s/%s/"%(args.dataset, args.checkname)
+'train_val.jpg')
if __name__ == "__main__":
main()