Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

test.py 2.3 KB

You have to be logged in to leave a comment. Sign In
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
  1. from download import trainset, trainloader, testset, testloader, classes
  2. from model import net,criterion,optimizer
  3. import torch
  4. import os
  5. dataiter = iter(testloader)
  6. images, labels = dataiter.next()
  7. if os.path.exists('./weights'):
  8. net.load_state_dict(torch.load('./weights'))
  9. net.eval()
  10. #
  11. # # print images
  12. # imshow(torchvision.utils.make_grid(images))
  13. # print('GroundTruth: ', ' '.join('%5s' % classes[labels[j]] for j in range(4)))
  14. ########################################################################
  15. # Okay, now let us see what the neural network thinks these examples above are:
  16. outputs = net(images)
  17. ########################################################################
  18. # The outputs are energies for the 10 classes.
  19. # The higher the energy for a class, the more the network
  20. # thinks that the image is of the particular class.
  21. # So, let's get the index of the highest energy:
  22. _, predicted = torch.max(outputs, 1)
  23. ########################################################################
  24. # The results seem pretty good.
  25. #
  26. # Let us look at how the network performs on the whole dataset.
  27. correct = 0
  28. total = 0
  29. with torch.no_grad():
  30. for data in testloader:
  31. images, labels = data
  32. outputs = net(images)
  33. _, predicted = torch.max(outputs.data, 1)
  34. total += labels.size(0)
  35. correct += (predicted == labels).sum().item()
  36. print('Accuracy of the network on the 10000 test images: %d %%' % (
  37. 100 * correct / total))
  38. ########################################################################
  39. # That looks waaay better than chance, which is 10% accuracy (randomly picking
  40. # a class out of 10 classes).
  41. # Seems like the network learnt something.
  42. #
  43. # Hmmm, what are the classes that performed well, and the classes that did
  44. # not perform well:
  45. class_correct = list(0. for i in range(10))
  46. class_total = list(0. for i in range(10))
  47. with torch.no_grad():
  48. for data in testloader:
  49. images, labels = data
  50. outputs = net(images)
  51. _, predicted = torch.max(outputs, 1)
  52. c = (predicted == labels).squeeze()
  53. for i in range(4):
  54. label = labels[i]
  55. class_correct[label] += c[i].item()
  56. class_total[label] += 1
  57. for i in range(10):
  58. print('Accuracy of %5s : %2d %%' % (
  59. classes[i], 100 * class_correct[i] / class_total[i]))
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...