main.py 3.68 KB
Newer Older
Hang Zhang's avatar
Hang Zhang committed
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
##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
## 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 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 model.encodenet import Net
from utils import *

# global variable
best_pred = 0.0
acclist = []

def main():
	# init the args
	args = Options().parse()
	args.cuda = not args.no_cuda and torch.cuda.is_available()
	torch.manual_seed(args.seed)
	if args.cuda:
		torch.cuda.manual_seed(args.seed)
	# init dataloader
	if args.dataset == 'cifar':
		from dataset.cifar import Dataloder
		train_loader, test_loader = Dataloder(args).getloader()
	else:
		raise ValueError('Unknow dataset!')

	model = Net()

	if args.cuda:
		model.cuda()

	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']
			best_pred = checkpoint['best_pred']
			acclist = checkpoint['acclist']
			model.load_state_dict(checkpoint['state_dict'])
			print("=> loaded checkpoint '{}' (epoch {})"
				.format(args.resume, checkpoint['epoch']))
		else:
			print("=> no resume checkpoint found at '{}'".format(args.resume))

	criterion = nn.CrossEntropyLoss()
	# TODO make weight_decay oen of args
	optimizer = optim.SGD(model.parameters(), lr=args.lr, momentum=
			args.momentum, weight_decay=1e-4)

	def train(epoch):
		model.train()
		global best_pred
		train_loss, correct, total = 0,0,0
		adjust_learning_rate(optimizer, epoch, best_pred, args)
		for batch_idx, (data, target) in enumerate(train_loader):
			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)
			progress_bar(batch_idx, len(train_loader), 
				'Loss: %.3f | Acc: %.3f%% (%d/%d)' % (train_loss/(batch_idx+1), 
				100.*correct/total, correct, total))

	def test(epoch):
		model.eval()
		global best_pred
		global acclist
		test_loss, correct, total = 0,0,0
		acc = 0.0
		is_best = False
		# for data, target in test_loader:
		for batch_idx, (data, target) in enumerate(test_loader):
			if args.cuda:
				data, target = data.cuda(), target.cuda()
			data, target = Variable(data, volatile=True), Variable(target)
			output = model(data)
			test_loss += criterion(output, target).data[0]
			# get the index of the max log-probability
			pred = output.data.max(1)[1] 
			correct += pred.eq(target.data).cpu().sum()
			total += target.size(0)

			acc = 100.*correct/total
			progress_bar(batch_idx, len(test_loader), 
				'Loss: %.3f | Acc: %.3f%% (%d/%d)'% (test_loss/(batch_idx+1), 
				acc, correct, total))
		# save checkpoint
		acclist += [acc]
		if acc > best_pred:
			best_pred = acc
			is_best = True
		save_checkpoint({
			'epoch': epoch,
			'state_dict': model.state_dict(),
			'best_pred': best_pred,
			'acclist':acclist,
			}, args=args, is_best=is_best)

	# TODO add plot curve

	for epoch in range(args.start_epoch, args.epochs + 1):
		train(epoch)
		# FIXME this is a bug somewhere not in the code
		test(epoch)


if __name__ == "__main__":
	main()