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
|
- # -*- coding: utf-8 -*-
- from download import trainset, trainloader, classes
- from model import net,criterion,optimizer
- import torch
- import os
- ########################################################################
- # 4. Train the network
- # ^^^^^^^^^^^^^^^^^^^^
- #
- # This is when things start to get interesting.
- # We simply have to loop over our data iterator, and feed the inputs to the
- # network and optimize.
- # get some random training images
- dataiter = iter(trainloader)
- images, labels = dataiter.next()
- if os.path.exists('./weights'):
- net.load_state_dict(torch.load('./weights'))
- net.train()
- for epoch in range(1): # loop over the dataset multiple times
- running_loss = 0.0
- for i, data in enumerate(trainloader, 0):
- # get the inputs
- inputs, labels = data
- # zero the parameter gradients
- optimizer.zero_grad()
- # forward + backward + optimize
- outputs = net(inputs)
- loss = criterion(outputs, labels)
- loss.backward()
- optimizer.step()
- # print statistics
- running_loss += loss.item()
- if i % 2000 == 1999: # print every 2000 mini-batches
- print('[%d, %5d] loss: %.3f' %
- (epoch + 1, i + 1, running_loss / 2000))
- running_loss = 0.0
- print('Finished Training!!!!')
- for param_tensor in net.state_dict():
- print(param_tensor, "\t", net.state_dict()[param_tensor].size())
- print("Optimizer's state_dict:")
- for var_name in optimizer.state_dict():
- print(var_name, "\t", optimizer.state_dict()[var_name])
- print('Saving weights...')
- torch.save(net.state_dict(), './weights')
- #
- # ########################################################################
- # # 5. Test the network on the test data
- # # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- # #
- # # We have trained the network for 2 passes over the training dataset.
- # # But we need to check if the network has learnt anything at all.
- # #
- # # We will check this by predicting the class label that the neural network
- # # outputs, and checking it against the ground-truth. If the prediction is
- # # correct, we add the sample to the list of correct predictions.
- # #
- # # Okay, first step. Let us display an image from the test set to get familiar.
- #
- # dataiter = iter(testloader)
- # images, labels = dataiter.next()
- #
- # # print images
- # imshow(torchvision.utils.make_grid(images))
- # print('GroundTruth: ', ' '.join('%5s' % classes[labels[j]] for j in range(4)))
- #
- # ########################################################################
- # # Okay, now let us see what the neural network thinks these examples above are:
- #
- # outputs = net(images)
- #
- # ########################################################################
- # # The outputs are energies for the 10 classes.
- # # The higher the energy for a class, the more the network
- # # thinks that the image is of the particular class.
- # # So, let's get the index of the highest energy:
- # _, predicted = torch.max(outputs, 1)
- #
- # print('Predicted: ', ' '.join('%5s' % classes[predicted[j]]
- # for j in range(4)))
- #
- # ########################################################################
- # # The results seem pretty good.
- # #
- # # Let us look at how the network performs on the whole dataset.
- #
- # correct = 0
- # total = 0
- # with torch.no_grad():
- # for data in testloader:
- # images, labels = data
- # outputs = net(images)
- # _, predicted = torch.max(outputs.data, 1)
- # total += labels.size(0)
- # correct += (predicted == labels).sum().item()
- #
- # print('Accuracy of the network on the 10000 test images: %d %%' % (
- # 100 * correct / total))
- #
- # ########################################################################
- # # That looks waaay better than chance, which is 10% accuracy (randomly picking
- # # a class out of 10 classes).
- # # Seems like the network learnt something.
- # #
- # # Hmmm, what are the classes that performed well, and the classes that did
- # # not perform well:
- #
- # class_correct = list(0. for i in range(10))
- # class_total = list(0. for i in range(10))
- # with torch.no_grad():
- # for data in testloader:
- # images, labels = data
- # outputs = net(images)
- # _, predicted = torch.max(outputs, 1)
- # c = (predicted == labels).squeeze()
- # for i in range(4):
- # label = labels[i]
- # class_correct[label] += c[i].item()
- # class_total[label] += 1
- #
- #
- # for i in range(10):
- # print('Accuracy of %5s : %2d %%' % (
- # classes[i], 100 * class_correct[i] / class_total[i]))
|