-
Notifications
You must be signed in to change notification settings - Fork 0
/
04_simple_cnn.py
executable file
·91 lines (72 loc) · 3.17 KB
/
04_simple_cnn.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
#!/usr/bin/env python3
import torch
import argparse
import torchvision
from torchvision import transforms
from models.basic_models import Net
import torch.nn as nn
from torch.autograd import Variable
import torchvision.datasets as dsets
import matplotlib.pyplot as plt
from other.utils import save_model
MNIST_DATA = '../data/MNIST'
def train():
for epoch in range(args.epochs):
for i, (images, labels) in enumerate(train_loader):
images = Variable(images)
labels = Variable(labels)
optimizer.zero_grad()
outputs = model(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
print('Epoch [%d/%d], Step [%d/%d], Loss: %.4f' % (
epoch + 1, args.epochs, i + 1, len(train_dataset) // args.batch_size,
loss.data[0]))
#save_model(model, '../data/models/mnist_model.pkl')
def test():
correct = 0
total = 0
for images, labels in test_loader:
images = Variable(images, volatile=True)
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum()
#print('Accuracy of the network on the %d test images: %d %%' % (len(test_loader.dataset), 100 * correct / total))
#
# for i in range(len(labels)):
#
# # Print Incorrect Images
# if labels[i] != predicted[i]:
# current_image = images.data.numpy()[i].reshape(-1, 28)
# plt.clf()
# plt.imshow(current_image, cmap='gray_r', )
# plt.show(block=False)
# plt.pause(0.10)
# print('True Label {}, Predict Label {}'.format(labels[i], predicted[i]))
print('Accuracy of the network on the %d test images: %d %%' % (total, 100 * correct / total))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='PyTorch FeedForward Example')
parser.add_argument('--epochs', type=int, default=1, help='number of epochs to train')
parser.add_argument('--lr', type=float, default=0.01, help='learning rate')
parser.add_argument('--batch_size', type=int, default=128, help='batch size')
args = parser.parse_args()
train_dataset = dsets.MNIST(root=MNIST_DATA,
train=True,
transform=transforms.Compose([transforms.ToTensor()]),
download=True)
test_dataset = dsets.MNIST(root=MNIST_DATA,
train=False,
transform=transforms.Compose([transforms.ToTensor()]))
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=64,
shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
batch_size=64,
shuffle=False)
model = Net()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=args.lr)
train()
test()