File size: 2,158 Bytes
4937055
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch.nn as nn
import torch.nn.functional as F


class ResBlock(nn.Module):
    def __init__(self, in_channels, out_channels):
        super(ResBlock, self).__init__()
        self.convblock1 = nn.Sequential(
            nn.Conv2d(in_channels, out_channels,kernel_size=(3,3), stride = 1, padding = 1,bias=False),
            nn.BatchNorm2d(out_channels),
            nn.ReLU(),
            nn.Conv2d(out_channels, out_channels,kernel_size=(3,3), stride = 1, padding = 1,bias=False),
            nn.BatchNorm2d(out_channels),
            nn.ReLU()
        )


    def forward(self, x):
        x = self.convblock1(x)
        return x

class MyResNet(nn.Module):
    def __init__(self):
        super(MyResNet,self).__init__()

        self.prep_layer = nn.Sequential(
        nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1,bias=True),
        nn.BatchNorm2d(64),
        nn.ReLU(),
        )

        self.layer1 = nn.Sequential(
            nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1,bias=True),
            nn.MaxPool2d(2,2),
            nn.BatchNorm2d(128),
            nn.ReLU(),
        )

        self.resblock1 = ResBlock(128, 128)

        self.layer2 = nn.Sequential(
            nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1,bias=True),
            nn.MaxPool2d(kernel_size=2),
            nn.BatchNorm2d(256),
            nn.ReLU(),
        )

        self.layer3 = nn.Sequential(
            nn.Conv2d(256, 512, kernel_size=3, stride=1, padding=1,bias=True),
            nn.MaxPool2d(kernel_size=2),
            nn.BatchNorm2d(512),
            nn.ReLU(),
        )

        self.resblock2 = ResBlock(512, 512)

        self.maxpool = nn.MaxPool2d(kernel_size=4)
        self.fc = nn.Linear(512, 10)

    def forward(self, x):
        out = self.prep_layer(x)
        out = self.layer1(out)
        res1 = self.resblock1(out)
        out = out + res1
        out = self.layer2(out)
        out = self.layer3(out)
        res2 = self.resblock2(out)
        out = out + res2
        out = self.maxpool(out)
        out = out.view(out.size(0), -1)
        out = self.fc(out)

        return F.log_softmax(out,dim = -1)