-
Notifications
You must be signed in to change notification settings - Fork 9
/
training.py
224 lines (180 loc) · 7.42 KB
/
training.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# @Author: zechenghe
# @Date: 2019-01-20T16:46:24-05:00
# @Last modified by: zechenghe
# @Last modified time: 2019-02-01T14:01:19-05:00
import time
import math
import os
import numpy as np
import torch
import torchvision
import torchvision.transforms as transforms
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torch.backends.cudnn as cudnn
from net import *
from utils import *
def train(DATASET = 'CIFAR10', network = 'CIFAR10CNN', NEpochs = 200, imageWidth = 32,
imageHeight = 32, imageSize = 32*32, NChannels = 3, NClasses = 10,
BatchSize = 32, learningRate = 1e-3, NDecreaseLR = 20, eps = 1e-3,
AMSGrad = True, model_dir = "checkpoints/CIFAR10/", model_name = "ckpt.pth", gpu = True):
print "DATASET: ", DATASET
if DATASET == 'MNIST':
mu = torch.tensor([0.5], dtype=torch.float32)
sigma = torch.tensor([0.5], dtype=torch.float32)
Normalize = transforms.Normalize(mu.tolist(), sigma.tolist())
Unnormalize = transforms.Normalize((-mu / sigma).tolist(), (1.0 / sigma).tolist())
tsf = {
'train': transforms.Compose(
[
transforms.ToTensor(),
Normalize
]),
'test': transforms.Compose(
[
transforms.ToTensor(),
Normalize
])
}
trainset = torchvision.datasets.MNIST(root='./data/MNIST', train=True,
download=True, transform = tsf['train'])
testset = torchvision.datasets.MNIST(root='./data/MNIST', train=False,
download=True, transform = tsf['test'])
elif DATASET == 'CIFAR10':
mu = torch.tensor([0.485, 0.456, 0.406], dtype=torch.float32)
sigma = torch.tensor([0.229, 0.224, 0.225], dtype=torch.float32)
Normalize = transforms.Normalize(mu.tolist(), sigma.tolist())
Unnormalize = transforms.Normalize((-mu / sigma).tolist(), (1.0 / sigma).tolist())
tsf = {
'train': transforms.Compose(
[
transforms.RandomHorizontalFlip(),
transforms.RandomAffine(degrees = 10, translate = [0.1, 0.1], scale = [0.9, 1.1]),
transforms.ToTensor(),
Normalize
]),
'test': transforms.Compose(
[
transforms.ToTensor(),
Normalize
])
}
trainset = torchvision.datasets.CIFAR10(root='./data/CIFAR10', train = True,
download=True, transform = tsf['train'])
testset = torchvision.datasets.CIFAR10(root='./data/CIFAR10', train = False,
download=True, transform = tsf['test'])
netDict = {
'LeNet': LeNet,
'CIFAR10CNN': CIFAR10CNN
}
if network in netDict:
net = netDict[network](NChannels)
else:
print "Network not found"
exit(1)
print net
print "len(trainset) ", len(trainset)
print "len(testset) ", len(testset)
x_train, y_train = trainset.data, trainset.targets,
x_test, y_test = testset.data, testset.targets,
print "x_train.shape ", x_train.shape
print "x_test.shape ", x_test.shape
trainloader = torch.utils.data.DataLoader(trainset, batch_size = BatchSize,
shuffle = True, num_workers = 1)
testloader = torch.utils.data.DataLoader(testset, batch_size = 1000,
shuffle = False, num_workers = 1)
trainIter = iter(trainloader)
testIter = iter(testloader)
criterion = nn.CrossEntropyLoss()
softmax = nn.Softmax(dim=1)
if gpu:
net.cuda()
criterion.cuda()
softmax.cuda()
optimizer = optim.Adam(params = net.parameters(), lr = learningRate, eps = eps, amsgrad = AMSGrad)
NBatch = len(trainset) / BatchSize
cudnn.benchmark = True
for epoch in range(NEpochs):
lossTrain = 0.0
accTrain = 0.0
for i in range(NBatch):
try:
batchX, batchY = trainIter.next()
except StopIteration:
trainIter = iter(trainloader)
batchX, batchY = trainIter.next()
if gpu:
batchX = batchX.cuda()
batchY = batchY.cuda()
optimizer.zero_grad()
logits = net.forward(batchX)
prob = softmax(logits)
loss = criterion(logits, batchY)
loss.backward()
optimizer.step()
lossTrain += loss.cpu().detach().numpy() / NBatch
if gpu:
pred = np.argmax(prob.cpu().detach().numpy(), axis = 1)
groundTruth = batchY.cpu().detach().numpy()
else:
pred = np.argmax(prob.detach().numpy(), axis = 1)
groundTruth = batchY.detach().numpy()
acc = np.mean(pred == groundTruth)
accTrain += acc / NBatch
if (epoch + 1) % NDecreaseLR == 0:
learningRate = learningRate / 2.0
setLearningRate(optimizer, learningRate)
print "Epoch: ", epoch, "Loss: ", lossTrain, "Train accuracy: ", accTrain
accTest = evalTest(testloader, net, gpu = gpu)
if not os.path.exists(model_dir):
os.makedirs(model_dir)
torch.save(net, model_dir + model_name)
print "Model saved"
newNet = torch.load(model_dir + model_name)
newNet.eval()
accTest = evalTest(testloader, net, gpu = gpu)
print "Model restore done"
if __name__ == '__main__':
import argparse
import sys
import traceback
try:
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', type = str, default = 'CIFAR10')
parser.add_argument('--network', type = str, default = 'CIFAR10CNN')
parser.add_argument('--epochs', type = int, default = 200)
parser.add_argument('--eps', type = float, default = 1e-3)
parser.add_argument('--AMSGrad', type = bool, default = True)
parser.add_argument('--batch_size', type = int, default = 32)
parser.add_argument('--learning_rate', type = float, default = 1e-3)
parser.add_argument('--decrease_LR', type = int, default = 20)
parser.add_argument('--nogpu', dest='gpu', action='store_false')
parser.set_defaults(gpu=True)
args = parser.parse_args()
model_dir = "checkpoints/" + args.dataset + '/'
model_name = "ckpt.pth"
if args.dataset == 'MNIST':
imageWidth = 28
imageHeight = 28
imageSize = imageWidth * imageHeight
NChannels = 1
NClasses = 10
network = 'LeNet'
elif args.dataset == 'CIFAR10':
imageWidth = 32
imageHeight = 32
imageSize = imageWidth * imageHeight
NChannels = 3
NClasses = 10
network = 'CIFAR10CNN'
else:
print "No Dataset Found"
exit(0)
train(DATASET = args.dataset, network = network, NEpochs = args.epochs, imageWidth = imageWidth,
imageHeight = imageHeight, imageSize = imageSize, NChannels = NChannels, NClasses = NClasses,
BatchSize = args.batch_size, learningRate = args.learning_rate, NDecreaseLR = args.decrease_LR, eps = args.eps,
AMSGrad = args.AMSGrad, model_dir = model_dir, model_name = model_name, gpu = args.gpu)
except:
traceback.print_exc(file=sys.stdout)
sys.exit(1)