-
Notifications
You must be signed in to change notification settings - Fork 0
/
improvingCNN.html
2892 lines (2809 loc) · 119 KB
/
improvingCNN.html
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Improving our Neural Network</title>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Megrim&display=swap"
rel="stylesheet"
/>
<!-- -- -->
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Quicksand:wght@300&display=swap"
rel="stylesheet"
/>
<!-- -- -->
<link
rel="stylesheet"
href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.3.1/styles/default.min.css"
/>
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.3.1/highlight.min.js"></script>
<link rel="stylesheet" href="/path/to/styles/default.css" />
<script src="/path/to/highlight.min.js"></script>
<script>
hljs.highlightAll();
</script>
<!-- -- -->
<!-- -- -->
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Alegreya+Sans+SC&display=swap"
rel="stylesheet"
/>
<!-- -- -->
<!-- -- -->
<!-- -- -->
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Fira+Sans:wght@200&display=swap"
rel="stylesheet"
/>
<!-- --- -->
</head>
<div class="navbar" id="mytopnav">
<div class="navcontainer">
  
<div class="bars">
<a
href="javascript:void(0);"
class="icon"
onclick="myFunction(); cross();"
>
<div class="line1" id="line1"></div>
<div class="line2" id="line2"></div>
<div class="line3" id="line3"></div
></a>
</div>
  
<div class="element1">
<a href="index.html" class="home"><b>ML</b>harbour</a>
</div>
        
<div class="element2"><a href="" class="a1">Learn</a></div>
  
<div class="element3"><a href="" class="a2">Blog</a></div>
  
<div class="element4"><a href="" class="a3">Resources</a></div>
  
<div class="element5"><a href="" class="a4">About</a></div>
  
<div class="element6"><a href="" class="a5">Contact</a></div>
<!-- <div class="engine">
<script async src="https://cse.google.com/cse.js?cx=38c22d203e37ec2d0"></script>
<div class="gcse-search"></div></div> -->
</div>
</div>
<h1 class="head1">How TO/NOT TO improve your Neural Networks</h1>
<br />
<p class="para1">
Now that we have a working Baseline Model from the previous tutorial and
data ready to feed to our model all in place, lets improve the performance
of our model. <br />
In this tutorial we will understand ways to improve our CNN and understand
different ways of building a Neural Network and observe how it affects the
error rate of our network.
</p>
<br /><br />
<center>
<a
href="https://colab.research.google.com/drive/18QdCMurLp5DViBKni_wS7OR0IMeYVpvf?usp=sharing"
target="blank"
>
<img
class="img"
src="https://colab.research.google.com/assets/colab-badge.svg"
alt="Open In Colab"
height="35px"
/>
</a>
</center>
<br />
<br />
<p class="para2">
<b> Importing required libraries.</b>
</p>
<pre><code class="language-python" id="code">
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
from torchvision import datasets
import torchvision.transforms as transforms
import torchvision.datasets as datasets
from torch.utils.data import DataLoader
import random
import numpy
import matplotlib.pyplot as plt
from torchsummary import summary
</code></pre>
<br />
<p class="para3">
<b>Downloading the Data (CIFAR-10 dataset).</b>
</p>
<pre><code class="language-python" id="code">
data_path = '/data'
train_data = datasets.CIFAR10(data_path, train = True, download = True, transform=transforms.ToTensor())
test_data = datasets.CIFAR10(data_path, train = False, download = True, transform=transforms.ToTensor())
</code></pre>
<pre><code class="language-plaintext" id="code">
Output: Downloading https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz to /data/cifar-10-python.tar.gz
||||||||||||||| 170499072/? [00:04<00:00, 44184988.52it/s]
Extracting /data/cifar-10-python.tar.gz to /data
Files already downloaded and verified
</code></pre>
<br />
<p class="para4">
<b>Normalizing the Data.</b>
</p>
<pre><code class="language-python" id="code">
data_path = '/data'
train_data = datasets.CIFAR10(data_path, train = True, download = True,
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), #mean for each colour channel
(0.247, 0.243, 0.261)) #standard deviation for each colour channel
]))
test_data = datasets.CIFAR10(data_path, train = False, download = True,
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), #mean for each colour channel
(0.247, 0.243, 0.261)) #standard deviation for each colour channel
]))
</code></pre>
<br />
<p class="para5">
<b>Defining our classes list.</b>
</p>
<pre><code class="language-python" id="code">
classes = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
# 0 1 2 3 4 5 6 7 8 9
</code></pre>
<br />
<p class="para6">
<b>Specifying our Data loaders.</b>
</p>
<pre><code class="language-python" id="code">
train_loader = DataLoader(train_data, batch_size=32, shuffle=True)
test_loader = DataLoader(test_data, batch_size=32, shuffle=False)
</code></pre>
<br />
<p class="para7">
<b>Setting up our device (GPU/CPU)</b>, since we are going to train a couple
of Deep Neural Networks, we are going to need quite the computation power to
boost training of our Models.
</p>
<pre><code class="language-python" id="code">
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
device
</code></pre>
<pre><code class="language-plaintext" id="code">
Output: device(type='cuda', index=0)
</code></pre>
<p class="para8">
<b>Defining the training function</b> (updates mentioned with #1...)<br />
<br /><b>[1]</b> We define two empty lists (loss_per_iteration and
accuracy_per_iteration) to store the loss and accuracy values at every epoch
so that we are able to plot them later to visualise our Model's learning
curve, we just updated the previously used training function to make it more
capable.<br />
<b>[2]</b> After our Model finishes training for one epoch, we make a loop
that calculates the accuracy of the model on our test set on what it has
learnt till that instant. At every epoch, it is like calculating the
accuracy for a separate model as the model updates its parameters at every
epoch. So the acccuracy calculated when our model completes and stops
training would NOT be equal to the accuracy we calculate and store here at
every epoch. Calculating accuracy at every epoch gives us a basic idea of
where our model is heading to. Final accuracy is calculated after the
training is finished only. <br />
<b>[3]</b> Printing the loss and Test Accuracy at every epoch, :.5f and :.4f
are a type of python string formatting option, :.nf will print the value
only upto n decimal places, n being a real number. <br />
<b>[4]</b> Appending the loss and accuracy value to the lists we defined in
[1]. We will use them after training each model to plot their accuracy and
loss trend w.r.t. epochs.
</p>
<pre><code class="language-python" id="code">
def train_batch(epochs, model, criterion, optimizer, train_loader):
loss_per_iteration = [] #1
accuracy_per_iteration = [] #1
total = 0
correct = 0
for i in range(epochs):
train_loss = 0.0
for inputs, labels in train_loader:
inputs, labels = inputs.to(device), labels.to(device)
outputs = model(inputs)
loss = criterion(outputs, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
train_loss = train_loss + loss.item()
with torch.no_grad(): #2
for test_data in test_loader:
test_inputs, test_labels = test_data
test_inputs, test_labels = test_inputs.to(device), test_labels.to(device)
test_outputs = model(test_inputs)
_, predicted = torch.max(test_outputs, 1)
total += test_labels.size(0)
correct += (predicted == test_labels).sum().item()
test_accuracy = 100 * correct / total
print(f"Epoch: {i}/{epochs} Loss: {train_loss/len(train_loader):.5f} Test Accuracy: {100* correct/total:.4f} %") #3
loss_per_iteration.append(train_loss/len(train_loader)) #4
accuracy_per_iteration.append(test_accuracy) #4
</code></pre>
<p class="para9">
Building our first Model, Modela. Remember our baseline model containing two
convolutional layers from the last tutorial which just achieved a accuracy
of 65% in 200 epochs. Modela has two more Convolution layers, a slight more
feature, with ReLU activation and MaxPool pooling. You will observe the
difference in the learning curve while training. Our first model (65% acc.)
was underfitting to our data because it just had 2 layers, so we defintely
needed more layers in it.
</p>
<pre><code class="language-python" id="code">
class Modela(nn.Module):
def __init__(self):
super().__init__()
self.model = nn.Sequential(
nn.Conv2d(3, 256, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Conv2d(256, 128, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Conv2d(128, 64, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Conv2d(64, 32, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Flatten(),
nn.Linear(8*4*4, 32),
nn.ReLU(),
nn.Linear(32, 10)
)
def forward(self, x):
return self.model(x)
</code></pre>
<pre><code class="language-python" id="code">
modela = Modela()
modela.to(device)
summary(modela, (3, 32, 32))
</code></pre>
<pre><code class="language-plaintext" id="code">
Output: ----------------------------------------------------------------
Layer (type) Output Shape Param #
================================================================
Conv2d-1 [-1, 256, 32, 32] 7,168
ReLU-2 [-1, 256, 32, 32] 0
MaxPool2d-3 [-1, 256, 16, 16] 0
Conv2d-4 [-1, 128, 16, 16] 295,040
ReLU-5 [-1, 128, 16, 16] 0
MaxPool2d-6 [-1, 128, 8, 8] 0
Conv2d-7 [-1, 64, 8, 8] 73,792
ReLU-8 [-1, 64, 8, 8] 0
MaxPool2d-9 [-1, 64, 4, 4] 0
Conv2d-10 [-1, 32, 4, 4] 18,464
ReLU-11 [-1, 32, 4, 4] 0
MaxPool2d-12 [-1, 32, 2, 2] 0
Flatten-13 [-1, 128] 0
Linear-14 [-1, 32] 4,128
ReLU-15 [-1, 32] 0
Linear-16 [-1, 10] 330
================================================================
Total params: 398,922
Trainable params: 398,922
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.01
Forward/backward pass size (MB): 5.14
Params size (MB): 1.52
Estimated Total Size (MB): 6.68
----------------------------------------------------------------
</code></pre>
<pre><code class="language-python" id="code">
print(modela)
</code></pre>
<pre><code class="language-plaintext" id="code">
Output: Modela(
(model): Sequential(
(0): Conv2d(3, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(1): ReLU()
(2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(3): Conv2d(256, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(4): ReLU()
(5): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(6): Conv2d(128, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(7): ReLU()
(8): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(9): Conv2d(64, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(10): ReLU()
(11): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(12): Flatten(start_dim=1, end_dim=-1)
(13): Linear(in_features=128, out_features=32, bias=True)
(14): ReLU()
(15): Linear(in_features=32, out_features=10, bias=True)
)
)
</code></pre>
<p class="para10">
Training our Modela for 50 epochs, now the question Why 50? We could have
achieved higher accuracy if we doubled the epochs. That is correct but
actually no, when we train our Modelb you will know why. Our motive is to
select the best components for our Neural Network that are time efficient as
well as which increase our model's performance. Modelb gives a huge boost as
we change a major component in it.
</p>
<pre><code class="language-python" id="code">
train_loader = torch.utils.data.DataLoader(train_data, batch_size=32, shuffle=True)
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(modela.parameters(), lr =0.001)
train_batch(epochs=50,
model=modela,
criterion=criterion,
optimizer=optimizer,
train_loader = train_loader)
</code></pre>
<pre><code class="language-plaintext" id="code1">
Output: Epoch: 0/50 Loss: 2.30717 Test Accuracy: 10.7600 %
Epoch: 1/50 Loss: 2.30113 Test Accuracy: 11.0300 %
Epoch: 2/50 Loss: 2.28893 Test Accuracy: 12.5900 %
Epoch: 3/50 Loss: 2.25722 Test Accuracy: 13.6800 %
Epoch: 4/50 Loss: 2.18725 Test Accuracy: 15.6200 %
Epoch: 5/50 Loss: 2.07771 Test Accuracy: 17.5733 %
Epoch: 6/50 Loss: 1.97359 Test Accuracy: 19.4400 %
Epoch: 7/50 Loss: 1.88935 Test Accuracy: 21.2700 %
Epoch: 8/50 Loss: 1.79328 Test Accuracy: 22.8922 %
Epoch: 9/50 Loss: 1.70969 Test Accuracy: 24.4680 %
Epoch: 10/50 Loss: 1.64999 Test Accuracy: 25.9318 %
Epoch: 11/50 Loss: 1.60060 Test Accuracy: 27.1825 %
Epoch: 12/50 Loss: 1.55486 Test Accuracy: 28.4354 %
Epoch: 13/50 Loss: 1.51618 Test Accuracy: 29.5393 %
Epoch: 14/50 Loss: 1.48198 Test Accuracy: 30.7007 %
Epoch: 15/50 Loss: 1.45080 Test Accuracy: 31.7512 %
Epoch: 16/50 Loss: 1.42088 Test Accuracy: 32.7206 %
Epoch: 17/50 Loss: 1.39426 Test Accuracy: 33.6761 %
Epoch: 18/50 Loss: 1.36771 Test Accuracy: 34.5263 %
Epoch: 19/50 Loss: 1.34319 Test Accuracy: 35.2320 %
Epoch: 20/50 Loss: 1.32084 Test Accuracy: 35.9624 %
Epoch: 21/50 Loss: 1.29592 Test Accuracy: 36.5114 %
Epoch: 22/50 Loss: 1.27484 Test Accuracy: 37.2965 %
Epoch: 23/50 Loss: 1.25240 Test Accuracy: 38.0696 %
Epoch: 24/50 Loss: 1.23215 Test Accuracy: 38.7176 %
Epoch: 25/50 Loss: 1.21420 Test Accuracy: 39.3508 %
Epoch: 26/50 Loss: 1.19325 Test Accuracy: 39.9563 %
Epoch: 27/50 Loss: 1.17882 Test Accuracy: 40.5843 %
Epoch: 28/50 Loss: 1.15966 Test Accuracy: 41.1645 %
Epoch: 29/50 Loss: 1.14259 Test Accuracy: 41.6883 %
Epoch: 30/50 Loss: 1.12625 Test Accuracy: 42.2416 %
Epoch: 31/50 Loss: 1.10901 Test Accuracy: 42.7575 %
Epoch: 32/50 Loss: 1.09510 Test Accuracy: 43.2882 %
Epoch: 33/50 Loss: 1.07852 Test Accuracy: 43.6847 %
Epoch: 34/50 Loss: 1.06106 Test Accuracy: 44.2046 %
Epoch: 35/50 Loss: 1.04505 Test Accuracy: 44.6883 %
Epoch: 36/50 Loss: 1.03017 Test Accuracy: 45.1557 %
Epoch: 37/50 Loss: 1.01369 Test Accuracy: 45.6116 %
Epoch: 38/50 Loss: 1.00098 Test Accuracy: 46.0190 %
Epoch: 39/50 Loss: 0.98689 Test Accuracy: 46.4607 %
Epoch: 40/50 Loss: 0.97189 Test Accuracy: 46.8885 %
Epoch: 41/50 Loss: 0.96021 Test Accuracy: 47.2757 %
Epoch: 42/50 Loss: 0.94511 Test Accuracy: 47.6926 %
Epoch: 43/50 Loss: 0.93303 Test Accuracy: 48.1007 %
Epoch: 44/50 Loss: 0.91979 Test Accuracy: 48.4369 %
Epoch: 45/50 Loss: 0.90829 Test Accuracy: 48.8200 %
Epoch: 46/50 Loss: 0.89641 Test Accuracy: 49.1898 %
Epoch: 47/50 Loss: 0.88585 Test Accuracy: 49.5546 %
Epoch: 48/50 Loss: 0.87218 Test Accuracy: 49.9135 %
Epoch: 49/50 Loss: 0.86385 Test Accuracy: 50.2644 %
</code></pre>
<pre><code class="language-python" id="code">
plot_a_sgd = plt.figure(figsize=(12, 2.5), dpi=200)
plt.subplot(1, 2, 1)
plt.plot(loss_per_iteration)
plt.title('Loss vs Epochs')
plt.xlabel('Epochs')
plt.ylabel('CrossEntropyLoss')
plt.subplot(1, 2, 2)
plot = plt.plot(accuracy_per_iteration)
plt.title('Test Accuracy vs Epochs')
plt.xlabel('Epochs')
plt.ylabel('Test Accuracy (%)')
plt.suptitle("Model a SGD")
plt.show()
</code></pre>
<img src="images/modelasgd.png" alt="" class="img1" />
<pre><code class="language-python" id="code">
total = 0
correct = 0
with torch.no_grad():
for data in train_loader:
inputs, labels = data
inputs, labels = inputs.to(device), labels.to(device)
outputs = modela(inputs)
_, predicted = torch.max(outputs, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print(f'Accuracy of the network on the 50000 train images: {100 * correct / total} %')
</code></pre>
<pre><code class="language-plaintext" id="code">
Output: Accuracy of the network on the 50000 train images: 70.43 %
</code></pre>
<pre><code class="language-python" id="code">
total = 0
correct = 0
with torch.no_grad():
for data in test_loader:
inputs, labels = data
inputs, labels = inputs.to(device), labels.to(device)
outputs = modela(inputs)
_, predicted = torch.max(outputs, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print(f'Accuracy of the network on the 10000 test images: {100 * correct / total} %')
</code></pre>
<pre><code class="language-plaintext" id="code">
Output: Accuracy of the network on the 10000 test images: 67.46 %
</code></pre>
<p class="para11">
<b>Modela attains train accuracy of 70.43% and test accuracy of 67.46%</b>,
still 2% higher than our baseline model in just 1/5 of the epochs, crazy
right? Things get more interesting as we get near to the final model. Lets
move on to Modelb.
</p>
<br />
<!-- =================================================================================== -->
<h3 class="head2">Changing the Optimizer (Modela)</h3>
<p class="para12">
Modelb has the same architecture as Modela but instead of SGD we will use
Adam as the optimizer. Adam is now the most used optimizer in
State-of-the-art Neural Networks because of its efficiency. We will be using
Adam in almost every neural network we build. <br />
More on the Adam optimizer <a href="">here</a>.
</p>
<pre><code class="language-python" id="code">
#73.64% Accuracy MODELA BUT ADAM OPTIMIZER
class Modela(nn.Module):
def __init__(self):
super().__init__()
self.model = nn.Sequential(
nn.Conv2d(3, 256, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Conv2d(256, 128, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Conv2d(128, 64, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Conv2d(64, 32, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Flatten(),
nn.Linear(8*4*4, 32),
nn.ReLU(),
nn.Linear(32, 10)
)
def forward(self, x):
return self.model(x)
</code></pre>
<pre><code class="language-python" id="code">
modela = Modela()
modela.to(device)
summary(modela, (3, 32, 32))
</code></pre>
<pre><code class="language-plaintext" id="code">
Output: ----------------------------------------------------------------
Layer (type) Output Shape Param #
================================================================
Conv2d-1 [-1, 256, 32, 32] 7,168
ReLU-2 [-1, 256, 32, 32] 0
MaxPool2d-3 [-1, 256, 16, 16] 0
Conv2d-4 [-1, 128, 16, 16] 295,040
ReLU-5 [-1, 128, 16, 16] 0
MaxPool2d-6 [-1, 128, 8, 8] 0
Conv2d-7 [-1, 64, 8, 8] 73,792
ReLU-8 [-1, 64, 8, 8] 0
MaxPool2d-9 [-1, 64, 4, 4] 0
Conv2d-10 [-1, 32, 4, 4] 18,464
ReLU-11 [-1, 32, 4, 4] 0
MaxPool2d-12 [-1, 32, 2, 2] 0
Flatten-13 [-1, 128] 0
Linear-14 [-1, 32] 4,128
ReLU-15 [-1, 32] 0
Linear-16 [-1, 10] 330
================================================================
Total params: 398,922
Trainable params: 398,922
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.01
Forward/backward pass size (MB): 5.14
Params size (MB): 1.52
Estimated Total Size (MB): 6.68
----------------------------------------------------------------
</code></pre>
<pre><code class="language-python" id="code">
#SAME MODELA BUT ADAM OPTIMIZER
train_loader = torch.utils.data.DataLoader(train_data, batch_size=32, shuffle=True)
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(modela.parameters(), lr =0.001)
train_batch(epochs=50,
model=modela,
criterion=criterion,
optimizer=optimizer,
train_loader = train_loader)
</code></pre>
<p class="para13">
Modela v/s Modelb, the starting accuracies speak for how efficient Adam is.
60% accuracy in first epoch compared to 10% in the basline model with SGD,
the reason we trained Modela for 50 epochs is because Adam compared to SGD
has a better learning curve. 67% compared to 73% accuracy on the same model
architecture by just changing the optimizer? That is the reason we will use
Adam, Adam is also used in most of the State-of-the-art neural networks
because of its efficiency.
</p>
<pre><code class="language-plaintext" id="code2">
Output: Epoch: 0/50 Loss: 1.44383 Test Accuracy: 60.5800 %
Epoch: 1/50 Loss: 0.99512 Test Accuracy: 64.1250 %
Epoch: 2/50 Loss: 0.83377 Test Accuracy: 66.3700 %
Epoch: 3/50 Loss: 0.73864 Test Accuracy: 67.4975 %
Epoch: 4/50 Loss: 0.67610 Test Accuracy: 68.3360 %
Epoch: 5/50 Loss: 0.62083 Test Accuracy: 69.2517 %
Epoch: 6/50 Loss: 0.57070 Test Accuracy: 69.9243 %
Epoch: 7/50 Loss: 0.53658 Test Accuracy: 70.2850 %
Epoch: 8/50 Loss: 0.50229 Test Accuracy: 70.7567 %
Epoch: 9/50 Loss: 0.47105 Test Accuracy: 71.1090 %
Epoch: 10/50 Loss: 0.44249 Test Accuracy: 71.4518 %
Epoch: 11/50 Loss: 0.41670 Test Accuracy: 71.7692 %
Epoch: 12/50 Loss: 0.39067 Test Accuracy: 71.9538 %
Epoch: 13/50 Loss: 0.36982 Test Accuracy: 72.1229 %
Epoch: 14/50 Loss: 0.35069 Test Accuracy: 72.1720 %
Epoch: 15/50 Loss: 0.32729 Test Accuracy: 72.2775 %
Epoch: 16/50 Loss: 0.31490 Test Accuracy: 72.3935 %
Epoch: 17/50 Loss: 0.29130 Test Accuracy: 72.4839 %
Epoch: 18/50 Loss: 0.28546 Test Accuracy: 72.5432 %
Epoch: 19/50 Loss: 0.26507 Test Accuracy: 72.6285 %
Epoch: 20/50 Loss: 0.25761 Test Accuracy: 72.6900 %
Epoch: 21/50 Loss: 0.24456 Test Accuracy: 72.7132 %
Epoch: 22/50 Loss: 0.23624 Test Accuracy: 72.7722 %
Epoch: 23/50 Loss: 0.22570 Test Accuracy: 72.7942 %
Epoch: 24/50 Loss: 0.21323 Test Accuracy: 72.8564 %
Epoch: 25/50 Loss: 0.20614 Test Accuracy: 72.8904 %
Epoch: 26/50 Loss: 0.20047 Test Accuracy: 72.8922 %
Epoch: 27/50 Loss: 0.19946 Test Accuracy: 72.9207 %
Epoch: 28/50 Loss: 0.18562 Test Accuracy: 72.9659 %
Epoch: 29/50 Loss: 0.18026 Test Accuracy: 72.9650 %
Epoch: 30/50 Loss: 0.17899 Test Accuracy: 72.9994 %
Epoch: 31/50 Loss: 0.16957 Test Accuracy: 73.0103 %
Epoch: 32/50 Loss: 0.16360 Test Accuracy: 73.0158 %
Epoch: 33/50 Loss: 0.16416 Test Accuracy: 73.0279 %
Epoch: 34/50 Loss: 0.15463 Test Accuracy: 73.0374 %
Epoch: 35/50 Loss: 0.15540 Test Accuracy: 73.0253 %
Epoch: 36/50 Loss: 0.15469 Test Accuracy: 73.0351 %
Epoch: 37/50 Loss: 0.14358 Test Accuracy: 73.0529 %
Epoch: 38/50 Loss: 0.14039 Test Accuracy: 73.0490 %
Epoch: 39/50 Loss: 0.14621 Test Accuracy: 73.0510 %
Epoch: 40/50 Loss: 0.13653 Test Accuracy: 73.0693 %
Epoch: 41/50 Loss: 0.13917 Test Accuracy: 73.0757 %
Epoch: 42/50 Loss: 0.13884 Test Accuracy: 73.0860 %
Epoch: 43/50 Loss: 0.13093 Test Accuracy: 73.1000 %
Epoch: 44/50 Loss: 0.13365 Test Accuracy: 73.0929 %
Epoch: 45/50 Loss: 0.12886 Test Accuracy: 73.1015 %
Epoch: 46/50 Loss: 0.13103 Test Accuracy: 73.0909 %
Epoch: 47/50 Loss: 0.11922 Test Accuracy: 73.0919 %
Epoch: 48/50 Loss: 0.12159 Test Accuracy: 73.1016 %
Epoch: 49/50 Loss: 0.12454 Test Accuracy: 73.1124 %
</code></pre>
<pre><code class="language-python" id="code">
plot_a_adam = plt.figure(figsize=(12, 2.5), dpi=200)
plt.subplot(1, 2, 1)
plt.plot(loss_per_iteration)
plt.title('Loss vs Epochs')
plt.xlabel('Epochs')
plt.ylabel('CrossEntropyLoss')
plt.subplot(1, 2, 2)
plot = plt.plot(accuracy_per_iteration)
plt.title('Test Accuracy vs Epochs')
plt.xlabel('Epochs')
plt.ylabel('Test Accuracy (%)')
plt.suptitle("Model a Adam")
plt.show()
</code></pre>
<img src="images/modelaadam.png" alt="" class="img2" />
<pre><code class="language-python" id="code">
total = 0
correct = 0
with torch.no_grad():
for data in train_loader:
inputs, labels = data
inputs, labels = inputs.to(device), labels.to(device)
outputs = modela(inputs)
_, predicted = torch.max(outputs, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print(f'Accuracy of the network on the 50000 train images: {100 * correct / total} %')
</code></pre>
<pre><code class="language-plaintext" id="code">
Output: Accuracy of the network on the 50000 train images: 96.446 %
</code></pre>
<pre><code class="language-python" id="code">
total = 0
correct = 0
with torch.no_grad():
for data in test_loader:
inputs, labels = data
inputs, labels = inputs.to(device), labels.to(device)
outputs = modela(inputs)
_, predicted = torch.max(outputs, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print(f'Accuracy of the network on the 10000 test images: {100 * correct / total} %')
</code></pre>
<pre><code class="language-plaintext" id="code">
Output: Accuracy of the network on the 10000 test images: 73.64 %
</code></pre>
<p class="para14">
<b
>Modela with Adam optimizer achieves a train accuracy of 96.446% and a
test accuracy of 73.64%</b
>, 7% higher than Modela with SGD optimizer.
</p>
<br />
<h3 class="head3">What Overfitting looks like.. (Modelb)</h3>
<p class="para14">
Modelb has eight convolution layers divided into blocks which are then
maxpooled, Modelb has 1.7 million parameters that it has to update at every
iteration, but this time our model performs even worse, and yes in reality
you will come across overfitting problems with your model so you should know
how it looks like. Our model should not be overpowered or underpowered,
should be about just right for the task. Sometimes our model learns the
training too well so it is unable to recognise the pattern on the data it
has never seen before. <br />
In Modelb we remove the two maxpooling layers and add four more
convolutional layers, because addding layers is good right? Well we have a
limit for everything and as you progress through your machine learning
experiments, you figure out the most optimal methods to design your own
models.
</p>
<pre><code class="language-python" id="code">
class Modelb(nn.Module): #reducing maxpool
def __init__(self):
super().__init__()
self.model = nn.Sequential(
nn.Conv2d(3, 512, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(512, 256, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(256, 128, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(128, 64, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Conv2d(64, 32, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(32, 16, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(16, 8, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(8, 4, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Flatten(),
nn.Linear(256, 512),
nn.ReLU(),
nn.Linear(512, 10)
)
def forward(self, x):
return self.model(x)
</code></pre>
<pre><code class="language-python" id="code">
modelb = Modelb()
modelb.to(device)
summary(modelb, (3, 32, 32))
</code></pre>
<pre><code class="language-plaintext" id="code">
Output: ----------------------------------------------------------------
Layer (type) Output Shape Param #
================================================================
Conv2d-1 [-1, 512, 32, 32] 14,336
ReLU-2 [-1, 512, 32, 32] 0
Conv2d-3 [-1, 256, 32, 32] 1,179,904
ReLU-4 [-1, 256, 32, 32] 0
Conv2d-5 [-1, 128, 32, 32] 295,040
ReLU-6 [-1, 128, 32, 32] 0
Conv2d-7 [-1, 64, 32, 32] 73,792
ReLU-8 [-1, 64, 32, 32] 0
MaxPool2d-9 [-1, 64, 16, 16] 0
Conv2d-10 [-1, 32, 16, 16] 18,464
ReLU-11 [-1, 32, 16, 16] 0
Conv2d-12 [-1, 16, 16, 16] 4,624
ReLU-13 [-1, 16, 16, 16] 0
Conv2d-14 [-1, 8, 16, 16] 1,160
ReLU-15 [-1, 8, 16, 16] 0
Conv2d-16 [-1, 4, 16, 16] 292
ReLU-17 [-1, 4, 16, 16] 0
MaxPool2d-18 [-1, 4, 8, 8] 0
Flatten-19 [-1, 256] 0
Linear-20 [-1, 512] 131,584
ReLU-21 [-1, 512] 0
Linear-22 [-1, 10] 5,130
================================================================
Total params: 1,724,326
Trainable params: 1,724,326
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.01
Forward/backward pass size (MB): 15.37
Params size (MB): 6.58
Estimated Total Size (MB): 21.96
----------------------------------------------------------------
</code></pre>
<pre><code class="language-python" id="code">
print(modelb)
</code></pre>
<pre><code class="language-python" id="code">
Output: Modelb(
(model): Sequential(
(0): Conv2d(3, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(1): ReLU()
(2): Conv2d(512, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(3): ReLU()
(4): Conv2d(256, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(5): ReLU()
(6): Conv2d(128, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(7): ReLU()
(8): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(9): Conv2d(64, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(10): ReLU()
(11): Conv2d(32, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(12): ReLU()
(13): Conv2d(16, 8, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(14): ReLU()
(15): Conv2d(8, 4, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(16): ReLU()
(17): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(18): Flatten(start_dim=1, end_dim=-1)
(19): Linear(in_features=256, out_features=512, bias=True)
(20): ReLU()
(21): Linear(in_features=512, out_features=10, bias=True)
)
)
</code></pre>
<pre><code class="language-python" id="code">
train_loader = torch.utils.data.DataLoader(train_data, batch_size=32, shuffle=True)
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(modelb.parameters(), lr =0.001)
train_batch(epochs=40,
model=modelb,
criterion=criterion,
optimizer=optimizer,
train_loader = train_loader)
</code></pre>
<pre><code class="language-plaintext" id="code3">
Output: Epoch: 0/40 Loss: 1.64300 Test Accuracy: 49.2400 %
Epoch: 1/40 Loss: 1.32124 Test Accuracy: 52.3350 %
Epoch: 2/40 Loss: 1.16530 Test Accuracy: 53.9733 %
Epoch: 3/40 Loss: 1.04714 Test Accuracy: 55.9625 %
Epoch: 4/40 Loss: 0.96013 Test Accuracy: 57.3780 %
Epoch: 5/40 Loss: 0.88864 Test Accuracy: 58.3017 %
Epoch: 6/40 Loss: 0.82391 Test Accuracy: 59.2943 %
Epoch: 7/40 Loss: 0.76314 Test Accuracy: 60.0675 %
Epoch: 8/40 Loss: 0.70994 Test Accuracy: 60.6244 %
Epoch: 9/40 Loss: 0.65724 Test Accuracy: 61.0390 %
Epoch: 10/40 Loss: 0.60698 Test Accuracy: 61.3300 %
Epoch: 11/40 Loss: 0.56651 Test Accuracy: 61.6308 %
Epoch: 12/40 Loss: 0.52612 Test Accuracy: 61.8669 %
Epoch: 13/40 Loss: 0.49681 Test Accuracy: 62.0271 %
Epoch: 14/40 Loss: 0.45514 Test Accuracy: 62.1687 %
Epoch: 15/40 Loss: 0.41876 Test Accuracy: 62.2413 %
Epoch: 16/40 Loss: 0.39649 Test Accuracy: 62.3065 %
Epoch: 17/40 Loss: 0.37796 Test Accuracy: 62.3578 %
Epoch: 18/40 Loss: 0.35628 Test Accuracy: 62.4142 %
Epoch: 19/40 Loss: 0.34020 Test Accuracy: 62.4335 %
Epoch: 20/40 Loss: 0.32570 Test Accuracy: 62.4776 %
Epoch: 21/40 Loss: 0.30255 Test Accuracy: 62.5282 %
Epoch: 22/40 Loss: 0.29994 Test Accuracy: 62.5552 %
Epoch: 23/40 Loss: 0.28614 Test Accuracy: 62.5679 %
Epoch: 24/40 Loss: 0.27348 Test Accuracy: 62.5628 %
Epoch: 25/40 Loss: 0.25925 Test Accuracy: 62.5838 %
Epoch: 26/40 Loss: 0.25451 Test Accuracy: 62.6093 %
Epoch: 27/40 Loss: 0.24946 Test Accuracy: 62.5989 %
Epoch: 28/40 Loss: 0.24059 Test Accuracy: 62.5748 %
Epoch: 29/40 Loss: 0.23164 Test Accuracy: 62.5820 %
Epoch: 30/40 Loss: 0.23531 Test Accuracy: 62.5939 %
Epoch: 31/40 Loss: 0.22422 Test Accuracy: 62.6119 %
Epoch: 32/40 Loss: 0.20911 Test Accuracy: 62.6152 %
Epoch: 33/40 Loss: 0.21300 Test Accuracy: 62.6315 %
Epoch: 34/40 Loss: 0.21215 Test Accuracy: 62.6217 %
Epoch: 35/40 Loss: 0.20219 Test Accuracy: 62.6236 %
Epoch: 36/40 Loss: 0.20352 Test Accuracy: 62.6238 %
Epoch: 37/40 Loss: 0.20606 Test Accuracy: 62.6153 %
Epoch: 38/40 Loss: 0.19133 Test Accuracy: 62.6046 %
Epoch: 39/40 Loss: 0.19103 Test Accuracy: 62.5870 %
</code></pre>
<pre><code class="language-python" id="code">
plot_b = plt.figure(figsize=(12, 2.5), dpi=200)
plt.subplot(1, 2, 1)
plt.plot(loss_per_iteration)
plt.title('Loss vs Epochs')
plt.xlabel('Epochs')
plt.ylabel('CrossEntropyLoss')
plt.subplot(1, 2, 2)
plot = plt.plot(accuracy_per_iteration)
plt.title('Test Accuracy vs Epochs')
plt.xlabel('Epochs')
plt.ylabel('Test Accuracy (%)')
plt.suptitle("Model b")
plt.show()
</code></pre>
<img src="images/modelb.png" alt="" class="img3" />
<pre><code class="language-python" id="code">
total = 0
correct = 0
with torch.no_grad():
for data in test_loader:
inputs, labels = data
inputs, labels = inputs.to(device), labels.to(device)
outputs = modelb(inputs)
_, predicted = torch.max(outputs, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
from sklearn.metrics import f1_score
f1_score = f1_score(labels.cpu(), predicted.cpu(), average='micro')
print(f'Accuracy of the network on the 10000 test images: {100 * correct / total} %')
print(f'f1_score: {f1_score}')
</code></pre>
<pre><code class="language-python" id="code">
Output: Accuracy of the network on the 10000 test images: 61.9 %
f1_score: 0.5625
</code></pre>
<pre><code class="language-python" id="code">
total = 0
correct = 0
with torch.no_grad():
for data in train_loader:
inputs, labels = data
inputs, labels = inputs.to(device), labels.to(device)
outputs = modelb(inputs)
_, predicted = torch.max(outputs, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print(f'Accuracy of the network on the 50000 train images: {100 * correct // total} %')
</code></pre>
<pre><code class="language-python" id="code">
Output: Accuracy of the network on the 50000 train images: 94 %
</code></pre>
<p class="para15">
<b>Modelb attains a train accuracy of 94% and test accuracy of 61.9%</b>,
the difference between the two indicates overfitting. Maybe because we
reduced the MaxPool layers and added a bit more features than we should have
? Lets find out.
</p>
<br />
<!-- ============================================================================ -->
<h3 class="head4">Using Dropout to reduce overfitting (Modelc)</h3>
<p class="para15">
In Modelc we reduce the number of features to half, which reduces the model
parameters to ~600k. nn.Dropout() randomly zeroes some elements that are
given to it as a input, which reduces the size of the network by ramoving
random nodes(neurons) at each iteration, which changes some layers output
size randomly which forces its nearby neurons to updates their weights to
correct themselves, reducing overfitting, hence performing a slightly better
than before.
</p>
<pre><code class="language-python" id="code">
class Modelc(nn.Module):
def __init__(self):
super().__init__()
self.model = nn.Sequential(
nn.Conv2d(3, 128, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(128, 128, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(128, 128, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(128, 64, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Dropout(),
nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Dropout(),
nn.Flatten(),
nn.Linear(4096, 32),
nn.ReLU(),
nn.Linear(32, 10)
)
def forward(self, x):
return self.model(x)
</code></pre>
<pre><code class="language-python" id="code">
modelc = Modelc()
modelc.to(device)
summary(modelc, (3, 32, 32))
</code></pre>
<pre><code class="language-plaintext" id="code">
Output: ----------------------------------------------------------------
Layer (type) Output Shape Param #
================================================================
Conv2d-1 [-1, 128, 32, 32] 3,584
ReLU-2 [-1, 128, 32, 32] 0
Conv2d-3 [-1, 128, 32, 32] 147,584
ReLU-4 [-1, 128, 32, 32] 0
Conv2d-5 [-1, 128, 32, 32] 147,584
ReLU-6 [-1, 128, 32, 32] 0
Conv2d-7 [-1, 64, 32, 32] 73,792
ReLU-8 [-1, 64, 32, 32] 0
MaxPool2d-9 [-1, 64, 16, 16] 0
Dropout-10 [-1, 64, 16, 16] 0
Conv2d-11 [-1, 64, 16, 16] 36,928
ReLU-12 [-1, 64, 16, 16] 0
Conv2d-13 [-1, 64, 16, 16] 36,928
ReLU-14 [-1, 64, 16, 16] 0
Conv2d-15 [-1, 64, 16, 16] 36,928
ReLU-16 [-1, 64, 16, 16] 0
Conv2d-17 [-1, 64, 16, 16] 36,928
ReLU-18 [-1, 64, 16, 16] 0
MaxPool2d-19 [-1, 64, 8, 8] 0
Dropout-20 [-1, 64, 8, 8] 0
Flatten-21 [-1, 4096] 0
Linear-22 [-1, 32] 131,104
ReLU-23 [-1, 32] 0