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

download.py 3.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
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
  1. # -*- coding: utf-8 -*-
  2. """
  3. Training a Classifier
  4. =====================
  5. This is it. You have seen how to define neural networks, compute loss and make
  6. updates to the weights of the network.
  7. Now you might be thinking,
  8. What about data?
  9. ----------------
  10. Generally, when you have to deal with image, text, audio or video data,
  11. you can use standard python packages that load data into a numpy array.
  12. Then you can convert this array into a ``torch.*Tensor``.
  13. - For images, packages such as Pillow, OpenCV are useful
  14. - For audio, packages such as scipy and librosa
  15. - For text, either raw Python or Cython based loading, or NLTK and
  16. SpaCy are useful
  17. Specifically for vision, we have created a package called
  18. ``torchvision``, that has data loaders for common datasets such as
  19. Imagenet, CIFAR10, MNIST, etc. and data transformers for images, viz.,
  20. ``torchvision.datasets`` and ``torch.utils.data.DataLoader``.
  21. This provides a huge convenience and avoids writing boilerplate code.
  22. For this tutorial, we will use the CIFAR10 dataset.
  23. It has the classes: ‘airplane’, ‘automobile’, ‘bird’, ‘cat’, ‘deer’,
  24. ‘dog’, ‘frog’, ‘horse’, ‘ship’, ‘truck’. The images in CIFAR-10 are of
  25. size 3x32x32, i.e. 3-channel color images of 32x32 pixels in size.
  26. .. figure:: /_static/img/cifar10.png
  27. :alt: cifar10
  28. cifar10
  29. Training an image classifier
  30. ----------------------------
  31. We will do the following steps in order:
  32. 1. Load and normalizing the CIFAR10 training and test datasets using
  33. ``torchvision``
  34. 2. Define a Convolutional Neural Network
  35. 3. Define a loss function
  36. 4. Train the network on the training data
  37. 5. Test the network on the test data
  38. 1. Loading and normalizing CIFAR10
  39. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  40. Using ``torchvision``, it’s extremely easy to load CIFAR10.
  41. """
  42. import torch
  43. import torchvision
  44. import torchvision.transforms as transforms
  45. ########################################################################
  46. # The output of torchvision datasets are PILImage images of range [0, 1].
  47. # We transform them to Tensors of normalized range [-1, 1].
  48. transform = transforms.Compose(
  49. [transforms.ToTensor(),
  50. transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
  51. trainset = torchvision.datasets.CIFAR10(root='./data', train=True,
  52. download=True, transform=transform)
  53. trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,
  54. shuffle=True, num_workers=2)
  55. testset = torchvision.datasets.CIFAR10(root='./data', train=False,
  56. download=True, transform=transform)
  57. testloader = torch.utils.data.DataLoader(testset, batch_size=4,
  58. shuffle=False, num_workers=2)
  59. classes = ('plane', 'car', 'bird', 'cat',
  60. 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
  61. ########################################################################
  62. # Let us show some of the training images, for fun.
  63. #
  64. # import matplotlib.pyplot as plt
  65. # import numpy as np
  66. #
  67. # # functions to show an image
  68. #
  69. #
  70. # def imshow(img):
  71. # img = img / 2 + 0.5 # unnormalize
  72. # npimg = img.numpy()
  73. # plt.imshow(np.transpose(npimg, (1, 2, 0)))
  74. # plt.show()
  75. # show images
  76. # imshow(torchvision.utils.make_grid(images))
  77. # print labels
  78. # print(' '.join('%5s' % classes[labels[j]] for j in range(4)))
Tip!

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

Comments

Loading...