Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ai-forever
GitHub Repository: ai-forever/sber-swap
Path: blob/main/network/resnet.py
879 views
1
import torch.nn as nn
2
import math
3
4
5
def conv3x3(in_planes, out_planes, stride=1):
6
"""3x3 convolution with padding"""
7
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
8
padding=1, bias=False)
9
10
11
class BasicBlock(nn.Module):
12
expansion = 1
13
14
def __init__(self, inplanes, planes, stride=1, downsample=None):
15
super(BasicBlock, self).__init__()
16
self.conv1 = conv3x3(inplanes, planes, stride)
17
self.bn1 = nn.BatchNorm2d(planes)
18
self.relu = nn.ReLU(inplace=True)
19
self.conv2 = conv3x3(planes, planes)
20
self.bn2 = nn.BatchNorm2d(planes)
21
self.downsample = downsample
22
self.stride = stride
23
24
def forward(self, x):
25
residual = x
26
27
out = self.conv1(x)
28
out = self.bn1(out)
29
out = self.relu(out)
30
31
out = self.conv2(out)
32
out = self.bn2(out)
33
34
if self.downsample is not None:
35
residual = self.downsample(x)
36
37
out += residual
38
out = self.relu(out)
39
40
return out
41
42
43
class Bottleneck(nn.Module):
44
expansion = 4
45
46
def __init__(self, inplanes, planes, stride=1, downsample=None):
47
super(Bottleneck, self).__init__()
48
self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, stride=stride, bias=False)
49
self.bn1 = nn.BatchNorm2d(planes)
50
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=1, padding=1, bias=False)
51
self.bn2 = nn.BatchNorm2d(planes)
52
self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False)
53
self.bn3 = nn.BatchNorm2d(planes * 4)
54
self.relu = nn.ReLU(inplace=True)
55
self.downsample = downsample
56
self.stride = stride
57
58
def forward(self, x):
59
residual = x
60
61
out = self.conv1(x)
62
out = self.bn1(out)
63
out = self.relu(out)
64
65
out = self.conv2(out)
66
out = self.bn2(out)
67
out = self.relu(out)
68
69
out = self.conv3(out)
70
out = self.bn3(out)
71
72
if self.downsample is not None:
73
residual = self.downsample(x)
74
75
out += residual
76
out = self.relu(out)
77
78
return out
79
80
81
class ResNet(nn.Module):
82
83
def __init__(self, block, layers, num_classes=1000, include_top=True):
84
self.inplanes = 64
85
super(ResNet, self).__init__()
86
self.include_top = include_top
87
88
self.conv0 = nn.Conv2d(3, 64, kernel_size=7, stride=1, padding=3, bias=False)
89
self.bn0 = nn.BatchNorm2d(64)
90
self.relu0 = nn.ReLU(inplace=True)
91
92
self.conv1 = nn.Conv2d(64, 64, kernel_size=7, stride=2, padding=3, bias=False)
93
self.bn1 = nn.BatchNorm2d(64)
94
self.relu = nn.ReLU(inplace=True)
95
96
self.layer1 = self._make_layer(block, 32, layers[0], stride=2)
97
self.layer2 = self._make_layer(block, 64, layers[1], stride=2)
98
self.layer3 = self._make_layer(block, 128, layers[2], stride=2)
99
self.layer4 = self._make_layer(block, 256, layers[3], stride=2)
100
self.layer5 = self._make_layer(block, 512, layers[4], stride=2)
101
self.layer6 = self._make_layer(block, 256, layers[5], stride=2)
102
103
for m in self.modules():
104
if isinstance(m, nn.Conv2d):
105
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
106
m.weight.data.normal_(0, math.sqrt(2. / n))
107
elif isinstance(m, nn.BatchNorm2d):
108
m.weight.data.fill_(1)
109
m.bias.data.zero_()
110
111
def _make_layer(self, block, planes, blocks, stride=1):
112
downsample = None
113
if stride != 1 or self.inplanes != planes * block.expansion:
114
downsample = nn.Sequential(
115
nn.Conv2d(self.inplanes, planes * block.expansion,
116
kernel_size=1, stride=stride, bias=False),
117
nn.BatchNorm2d(planes * block.expansion),
118
)
119
120
layers = []
121
layers.append(block(self.inplanes, planes, stride, downsample))
122
self.inplanes = planes * block.expansion
123
for i in range(1, blocks):
124
layers.append(block(self.inplanes, planes))
125
126
return nn.Sequential(*layers)
127
128
def forward(self, x):
129
x0 = self.conv0(x)
130
x0 = self.bn0(x0)
131
x0 = self.relu0(x0)
132
133
x1 = self.conv1(x0)
134
x1 = self.bn1(x1)
135
x1 = self.relu(x1)
136
137
x2 = self.layer1(x1)
138
x3 = self.layer2(x2)
139
x4 = self.layer3(x3)
140
x5 = self.layer4(x4)
141
x6 = self.layer5(x5)
142
x7 = self.layer6(x6)
143
144
return x7, x6, x5, x4, x3, x2, x1, x0
145
146
147
def MLAttrEncoderResnet(**kwargs):
148
model = ResNet(Bottleneck, [2, 2, 2, 2, 2, 2], **kwargs)
149
return model
150