chakshu12345 commited on
Commit
d8369fc
·
1 Parent(s): 6fdab71

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +375 -0
app.py ADDED
@@ -0,0 +1,375 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """CNN(mnist).ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1_4EIIBRbLBfS5tDz6VSgPIIpSv1t03Y7
8
+
9
+ CNN are mainly used to classify the images <br>
10
+ A basic CNN requires two additional layers called convoluation and pooling before the FNN <br>
11
+ CNN involves a Kernel<br>
12
+ Kernel is sliding/convuling matrix across the image with two operations<br>
13
+ 1. element-wise multiplication
14
+ 2. summation
15
+ <br>
16
+ Now comes the pooling part mainly there are two types of pooling<br>
17
+ 1. Max pooling- getting the max element after the kernel iteration over the image
18
+ 2. average pooling- getting the average of all the elements in the matrix
19
+
20
+ Now comes stride- it means number of steps in each convulation. By default it is 1. <br>
21
+ After using stride we can see that the input size becomes lesser so we add zeros symetrically in the matrix so the output becomes the same dimension of input <br>
22
+
23
+ The dimension of the output after applying all these <br>
24
+ O=(W-K+2P)/25 + 1<br>
25
+ W=input <br>
26
+ K=kernel size <br>
27
+ P=padding=(K-1)/2<br>
28
+ S=stride
29
+
30
+ # Importing libraries
31
+ """
32
+
33
+ import torch
34
+ import torch.nn as nn
35
+ from torchvision import transforms,datasets
36
+ from torch.utils.data import dataset, DataLoader
37
+ import torchvision.datasets as dsets
38
+
39
+ """# Loading the data """
40
+
41
+ #we will be using the mnist dataset for this purpose
42
+ train_dataset = dsets.MNIST(root='./data',
43
+ train=True,
44
+ transform=transforms.ToTensor(),
45
+ download=True)
46
+
47
+ test_dataset = dsets.MNIST(root='./data',
48
+ train=False,
49
+ transform=transforms.ToTensor())
50
+
51
+ #making our dataset iterable
52
+ batch_size = 100
53
+ n_iters = 3000
54
+ num_epochs = n_iters / (len(train_dataset) / batch_size)
55
+ num_epochs = int(num_epochs)
56
+
57
+ train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
58
+ batch_size=batch_size,
59
+ shuffle=True)
60
+
61
+ test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
62
+ batch_size=batch_size,
63
+ shuffle=False)
64
+
65
+ """# Defining our model """
66
+
67
+ class CNN(nn.Module):
68
+ def __init__(self):
69
+ super(CNN,self).__init__()
70
+
71
+ #defining the layers
72
+ self.block1=nn.Sequential(nn.Conv2d(1,16,kernel_size=(5,5),stride=1,padding=2),
73
+ nn.ReLU(),
74
+ nn.MaxPool2d(kernel_size=2))
75
+ #output after this operation
76
+ #(28-5+2/1 +1 =28 then max pooling 28/2=14)
77
+
78
+ self.block2=nn.Sequential(nn.Conv2d(16,32,kernel_size=(5,5),stride=1,padding=2),
79
+ nn.ReLU(),
80
+ nn.MaxPool2d(kernel_size=2))
81
+
82
+ #output after this
83
+ #(14-5+2*2/1 +1 = 13+1=14 then 14/2= 7)
84
+
85
+ self.layer=nn.Linear(32*7*7,10)
86
+
87
+
88
+ def forward(self,x):
89
+ x=self.block1(x)
90
+ x=self.block2(x)
91
+ #flatteing the output
92
+ x = x.view(x.size(0), -1)
93
+ #now feeding inot the linear network
94
+ x = self.layer(x)
95
+
96
+ return x
97
+
98
+ #making instance
99
+ model=CNN()
100
+ print(model)
101
+
102
+ """# Training the model"""
103
+
104
+ #initialising the loss and optimizer
105
+ criterion=nn.CrossEntropyLoss()
106
+ learning_rate = 0.01
107
+ optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
108
+
109
+ print(model.parameters())
110
+
111
+ print(len(list(model.parameters())))
112
+
113
+ # Convolution 1: 16 Kernels
114
+ print(list(model.parameters())[0].size())
115
+
116
+ # Convolution 1 Bias: 16 Kernels
117
+ print(list(model.parameters())[1].size())
118
+
119
+ # Convolution 2: 32 Kernels with depth = 16
120
+ print(list(model.parameters())[2].size())
121
+
122
+ # Convolution 2 Bias: 32 Kernels with depth = 16
123
+ print(list(model.parameters())[3].size())
124
+
125
+ # Fully Connected Layer 1
126
+ print(list(model.parameters())[4].size())
127
+
128
+ # Fully Connected Layer Bias
129
+ print(list(model.parameters())[5].size())
130
+
131
+ #lets begin the training
132
+ iter=0
133
+
134
+ for epochs in range(num_epochs):
135
+ for i,(images,labels) in enumerate(train_loader):
136
+
137
+ #loading the images
138
+ images.requires_grad_()
139
+
140
+ #first clearning the parameters
141
+ optimizer.zero_grad()
142
+
143
+ #calclauting the output and loss
144
+ output=model(images)
145
+
146
+ loss=criterion(output,labels)
147
+
148
+ #backprapgating the loss
149
+ loss.backward()
150
+
151
+ #updating the parameters
152
+ optimizer.step()
153
+
154
+ iter+=1
155
+
156
+ #printing for every 500 iterations
157
+ if iter%500==0:
158
+ # Calculate Accuracy
159
+ correct = 0
160
+ total = 0
161
+
162
+ #now iterate through the test dataset
163
+
164
+ for images,labels in test_loader:
165
+ images = images.requires_grad_()
166
+ outputs = model(images)
167
+ _, predicted = torch.max(outputs.data, 1)
168
+ total += labels.size(0)
169
+ correct += (predicted == labels).sum()
170
+
171
+ accuracy = 100 * correct / total
172
+
173
+ # Print Loss
174
+ print('Iteration: {}. Loss: {}. Accuracy: {}'.format(iter, loss.item(), accuracy))
175
+
176
+ """Accuracy came out to 96.63
177
+
178
+ # Model 2
179
+
180
+ This involves average pooling layer
181
+ """
182
+
183
+ class CNN2(nn.Module):
184
+ def __init__(self):
185
+ super().__init__()
186
+
187
+ #defining the layers
188
+ self.block1=nn.Sequential(nn.Conv2d(1,16,kernel_size=(5,5),stride=1,padding=2),
189
+ nn.ReLU(),
190
+ nn.AvgPool2d(kernel_size=2))
191
+ #output after this operation
192
+ #(28-5+2/1 +1 =28 then max pooling 28/2=14)
193
+
194
+ self.block2=nn.Sequential(nn.Conv2d(16,32,kernel_size=(5,5),stride=1,padding=2),
195
+ nn.ReLU(),
196
+ nn.AvgPool2d(kernel_size=2))
197
+
198
+ #output after this
199
+ #(14-5+2*2/1 +1 = 13+1=14 then 14/2= 7)
200
+
201
+ self.layer=nn.Linear(32*7*7,10)
202
+
203
+
204
+ def forward(self,x):
205
+ x=self.block1(x)
206
+ x=self.block2(x)
207
+ #flatteing the output
208
+ x = x.view(x.size(0), -1)
209
+ #now feeding inot the linear network
210
+ x = self.layer(x)
211
+
212
+ return x
213
+
214
+ #making instance
215
+ model2=CNN2()
216
+ print(model2)
217
+
218
+ learning_rate = 0.01
219
+ optimizer = torch.optim.SGD(model2.parameters(), lr=learning_rate)
220
+
221
+ #lets begin the training
222
+ iter=0
223
+
224
+ for epochs in range(num_epochs):
225
+ for i,(images,labels) in enumerate(train_loader):
226
+
227
+ #loading the images
228
+ images.requires_grad_()
229
+
230
+ #first clearning the parameters
231
+ optimizer.zero_grad()
232
+
233
+ #calclauting the output and loss
234
+ output=model2(images)
235
+
236
+ loss=criterion(output,labels)
237
+
238
+ #backprapgating the loss
239
+ loss.backward()
240
+
241
+ #updating the parameters
242
+ optimizer.step()
243
+
244
+ iter+=1
245
+
246
+ #printing for every 500 iterations
247
+ if iter%500==0:
248
+ # Calculate Accuracy
249
+ correct = 0
250
+ total = 0
251
+
252
+ #now iterate through the test dataset
253
+
254
+ for images,labels in test_loader:
255
+ images = images.requires_grad_()
256
+ outputs = model2(images)
257
+ _, predicted = torch.max(outputs.data, 1)
258
+ total += labels.size(0)
259
+ correct += (predicted == labels).sum()
260
+
261
+ accuracy = 100 * correct / total
262
+
263
+ # Print Loss
264
+ print('Iteration: {}. Loss: {}. Accuracy: {}'.format(iter, loss.item(), accuracy))
265
+
266
+ """Accuracy came out to be 93 %
267
+
268
+ # Model 3
269
+ This involves vaild pooling which means smaller output size
270
+ """
271
+
272
+ class CNN3(nn.Module):
273
+ def __init__(self):
274
+ super(CNN3, self).__init__()
275
+
276
+ # Convolution 1
277
+ self.cnn1 = nn.Conv2d(in_channels=1, out_channels=16, kernel_size=5, stride=1, padding=0)
278
+ self.relu1 = nn.ReLU()
279
+
280
+ # Max pool 1
281
+ self.maxpool1 = nn.MaxPool2d(kernel_size=2)
282
+
283
+ # Convolution 2
284
+ self.cnn2 = nn.Conv2d(in_channels=16, out_channels=32, kernel_size=5, stride=1, padding=0)
285
+ self.relu2 = nn.ReLU()
286
+
287
+ # Max pool 2
288
+ self.maxpool2 = nn.MaxPool2d(kernel_size=2)
289
+
290
+ # Fully connected 1 (readout)
291
+ self.fc1 = nn.Linear(32 * 4 * 4, 10)
292
+
293
+ def forward(self, x):
294
+ # Convolution 1
295
+ out = self.cnn1(x)
296
+ out = self.relu1(out)
297
+
298
+ # Max pool 1
299
+ out = self.maxpool1(out)
300
+
301
+ # Convolution 2
302
+ out = self.cnn2(out)
303
+ out = self.relu2(out)
304
+
305
+ # Max pool 2
306
+ out = self.maxpool2(out)
307
+
308
+ # Resize
309
+ # Original size: (100, 32, 7, 7)
310
+ # out.size(0): 100
311
+ # New out size: (100, 32*7*7)
312
+ out = out.view(out.size(0), -1)
313
+
314
+ # Linear function (readout)
315
+ out = self.fc1(out)
316
+
317
+ return out
318
+
319
+ #making instance
320
+ model3=CNN3()
321
+ print(model3)
322
+
323
+ learning_rate = 0.01
324
+ optimizer = torch.optim.SGD(model3.parameters(), lr=learning_rate)
325
+
326
+ #lets begin the training
327
+ iter=0
328
+
329
+ for epochs in range(num_epochs):
330
+ for i,(images,labels) in enumerate(train_loader):
331
+
332
+ #loading the images
333
+ images.requires_grad_()
334
+
335
+ #first clearning the parameters
336
+ optimizer.zero_grad()
337
+
338
+ #calclauting the output and loss
339
+ output=model3(images)
340
+
341
+ loss=criterion(output,labels)
342
+
343
+ #backprapgating the loss
344
+ loss.backward()
345
+
346
+ #updating the parameters
347
+ optimizer.step()
348
+
349
+ iter+=1
350
+
351
+ #printing for every 500 iterations
352
+ if iter%500==0:
353
+ # Calculate Accuracy
354
+ correct = 0
355
+ total = 0
356
+
357
+ #now iterate through the test dataset
358
+
359
+ for images,labels in test_loader:
360
+ images = images.requires_grad_()
361
+ outputs = model3(images)
362
+ _, predicted = torch.max(outputs.data, 1)
363
+ total += labels.size(0)
364
+ correct += (predicted == labels).sum()
365
+
366
+ accuracy = 100 * correct / total
367
+
368
+ # Print Loss
369
+ print('Iteration: {}. Loss: {}. Accuracy: {}'.format(iter, loss.item(), accuracy))
370
+
371
+ """Accuracy is 96 for the model 3
372
+
373
+ We can see in the above models the model with max pooling and padding=1 gave the best accuracy
374
+ """
375
+