-
Notifications
You must be signed in to change notification settings - Fork 0
/
convnet.py
182 lines (142 loc) · 5.31 KB
/
convnet.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
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 progressbar
import visualize
import sys
if __name__ == "__main__":
haveCuda = torch.cuda.is_available()
# Makes multiple runs comparable
if haveCuda:
torch.cuda.manual_seed(1)
else:
torch.manual_seed(1)
# path to dataset
root = 'C:/data/' if sys.platform == 'win32' else './data'
# Create visualizer
plotter = visualize.LinePlotter("CVSDemo")
# Data augmentation
transform = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5),
(0.5, 0.5, 0.5))
])
transform_val = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5),
(0.5, 0.5, 0.5))
])
sampler = torch.utils.data.sampler.SubsetRandomSampler(range(10000))
# Datasets
trainSet = torchvision.datasets.CIFAR10(root=root,
download=True, train=True, transform=transform)
testSet = torchvision.datasets.CIFAR10(root=root,
download=True, train=False, transform=transform_val)
#Data loaders
trainLoader = torch.utils.data.DataLoader(trainSet, sampler=sampler,
batch_size=128, shuffle=False, num_workers=2)
testLoader = torch.utils.data.DataLoader(testSet, sampler=sampler,
batch_size=128, shuffle=False, num_workers=2)
classes = ('plane', 'car', 'bird', 'cat',
'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
# Define small network
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 32, 5)
self.pool = nn.MaxPool2d(4, 4)
self.conv2 = nn.Conv2d(32, 64, 7)
self.fc1 = nn.Linear(64, 128)
self.fc2 = nn.Linear(128, 10)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = F.relu(self.conv2(x))
x = x.view(-1, 64)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x
# create net
net = Net()
if haveCuda:
net = net.cuda()
# Loss, and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.01,
momentum=0.5, weight_decay=1e-3)
def train( epoch ):
# variables for loss
running_loss = 0.0
correct = 0.0
total = 0
# set the network to train (for batchnorm and dropout)
net.train()
# Create progress bar
bar = progressbar.ProgressBar(0, len(trainLoader), redirect_stdout=False)
for i, data in enumerate(trainLoader):
# get the inputs
inputs, labels = data
# wrap them in Variable
if haveCuda:
inputs, labels = inputs.cuda(), labels.cuda()
# zero the parameter gradients
optimizer.zero_grad()
# forward + backward + optimize
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
# compute statistics
running_loss += loss.item()
_, predicted = torch.max(outputs, 1)
total += labels.size(0)
correct += predicted.eq(labels).sum().item()
bar.update(i)
bar.finish()
# print and plot statistics
tr_loss = running_loss / i
tr_corr = correct / total * 100
print("Train epoch %d loss: %.3f correct: %.2f" % (epoch + 1, running_loss / i, tr_corr))
plotter.plot("Loss", "Train", epoch, tr_loss)
plotter.plot("Accuracy", "Train", epoch, tr_corr)
def val(epoch):
# variables for loss
running_loss = 0.0
correct = 0.0
total = 0
# set the network to eval (for batchnorm and dropout)
net.eval()
# Create progress bar
bar = progressbar.ProgressBar(0, len(testLoader), redirect_stdout=False)
for i, data in enumerate(testLoader, 0):
# get the inputs
inputs, labels = data
# wrap them in Variable
if haveCuda:
inputs, labels = inputs.cuda(), labels.cuda()
# forward
outputs = net(inputs)
loss = criterion(outputs, labels)
# compute statistics
running_loss += loss.item()
_, predicted = torch.max(outputs, 1)
total += labels.size(0)
correct += predicted.eq(labels).sum().item()
bar.update(i)
bar.finish()
# print and plot statistics
val_loss = running_loss / i
val_corr = correct / total * 100
print("Test epoch %d loss: %.3f correct: %.2f" % (epoch + 1, running_loss / i, val_corr))
plotter.plot("Loss", "Val", epoch, val_loss)
plotter.plot("Accuracy", "Val", epoch, val_corr)
for epoch in range(50): # loop over the dataset multiple times
train(epoch)
val(epoch)
# After 25 epochs decrease the learning rate
if epoch == 24:
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.5, weight_decay=1e-5)
print('Finished Training')