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

cifar10-cnn.py 4.5 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
102
103
104
105
106
107
108
109
110
111
112
113
  1. #%%
  2. import torch
  3. import torchvision # includes datasets
  4. import torchvision.transforms as transforms
  5. #%%
  6. trainset = torchvision.datasets.CIFAR10(root='./datasets', train=True, download=True, transform=transforms.ToTensor())
  7. #%%
  8. trainset
  9. #%%
  10. trainloader = torch.utils.data.DataLoader(trainset, batch_size=8, shuffle=True, num_workers=2) # provides iterators of the dataset; shuffling helps prevent picking up arbitrary patterns due to order of data being inputed
  11. #%%
  12. testset = torchvision.datasets.CIFAR10(root='./datasets', train=False, download=True, transform=transforms.ToTensor())
  13. testset
  14. #%%
  15. testloader = torch.utils.data.DataLoader(testset, batch_size=8, shuffle=False, num_workers=2)
  16. #%%
  17. labels = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
  18. #%%
  19. import matplotlib.pyplot as plt
  20. import numpy as np
  21. images_batch, labels_batch = iter(trainloader).next()
  22. images_batch.shape
  23. #%%
  24. img = torchvision.utils.make_grid(images_batch) # use torchvision utility to make a grid of images in this batch
  25. img.shape # 8 images side-by-side with 2 pixels padding: 32+2*2=36
  26. #%%
  27. # to display the images using matplotlib we need the channels to be the last dimension
  28. np.transpose(img, (1, 2, 0)).shape # height, width, number of channels
  29. plt.imshow(np.transpose(img, (1, 2, 0))) # temporary reshaping
  30. plt.axis('off')
  31. plt.show()
  32. #%%
  33. import torch.nn as nn
  34. in_size = 3
  35. hid1_size = 16 # no. channels output by the first convolution layer
  36. hid2_size = 32 # feature maps
  37. out_size = len(labels) # no. of categories in this classification, 10
  38. k_conv_size = 5 # 5x5 convolution kernel for the convolutional layers
  39. #%%
  40. class ConvNet(nn.Module):
  41. def __init__(self):
  42. super(ConvNet, self).__init__()
  43. # channels need only be specified in the input and output
  44. # the size of the inputs, including the batch size will be automatically inferred by the convolutional layer
  45. self.layer1 = nn.Sequential(
  46. nn.Conv2d(in_size, hid1_size, k_conv_size), # 3 input features and produces 16 output features
  47. nn.BatchNorm2d(hid1_size), # normalize the outputs of this layer for one batch so they have 0 mean and unit variance; only need the no. of channels - the batch size, height and width of the input can be inferred
  48. nn.ReLU(),
  49. nn.MaxPool2d(kernel_size=2)) # 2x2 kernel
  50. self.layer2 = nn.Sequential(
  51. nn.Conv2d(hid1_size, hid2_size, k_conv_size), # size of output from previous layer as input here
  52. nn.BatchNorm2d(hid2_size),
  53. nn.ReLU(),
  54. nn.MaxPool2d(kernel_size=2)) # pooling layers do not change the number of input features
  55. self.fc = nn.Linear(hid2_size * k_conv_size * k_conv_size, out_size) # 32 x 5 x 5 input is smaller because of the pooling;
  56. # number of feature maps or convolutional layers X
  57. # size of image after passing through 2 convolutional and 2 pooling layers
  58. def forward(self, x):
  59. out = self.layer1(x)
  60. out = self.layer2(out)
  61. out = out.reshape(out.size(0), -1) # reshape the output so each image is represented as 1D vector to feed into the linear layer
  62. out = self.fc(out)
  63. return out
  64. #%%
  65. model = ConvNet()
  66. learning_rate = 0.001
  67. criterion = nn.CrossEntropyLoss() # the distance between probability distributions, usually the loss function used in classification models
  68. optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
  69. #%%
  70. total_step = len(trainloader)
  71. num_epochs = 5
  72. for epoch in range(num_epochs):
  73. for i, (images, labels) in enumerate(trainloader): # one batch of images at a time
  74. outputs = model(images)
  75. loss = criterion(outputs, labels) # calculate loss using the cross entropy loss function
  76. optimizer.zero_grad() # zero out gradient of optimizer before backward pass
  77. loss.backward()
  78. optimizer.step() # to update the model parameters
  79. if (i + 1) % 2000 == 0:
  80. print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, i+1, total_step, loss.item()))
  81. #%%
  82. model.eval()
  83. with torch.no_grad():
  84. correct = 0
  85. total = 0
  86. for images, labels in testloader:
  87. outputs = model(images)
  88. _, predicted = torch.max(outputs.data, 1) # find the output with the highest probability
  89. total += labels.size(0)
  90. correct += (predicted == labels).sum().item()
  91. print('Accuracy of the model on the 10000 test images: {}%'.format(100 * correct / total))
  92. #%%
Tip!

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

Comments

Loading...