Spaces:
Runtime error
Runtime error
theschoolofai
commited on
Commit
·
edc362c
1
Parent(s):
1aa932f
Create resnet.py
Browse files
resnet.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
ResNet in PyTorch.
|
3 |
+
For Pre-activation ResNet, see 'preact_resnet.py'.
|
4 |
+
|
5 |
+
Reference:
|
6 |
+
[1] Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun
|
7 |
+
Deep Residual Learning for Image Recognition. arXiv:1512.03385
|
8 |
+
"""
|
9 |
+
import torch.nn as nn
|
10 |
+
import torch.nn.functional as F
|
11 |
+
|
12 |
+
|
13 |
+
class BasicBlock(nn.Module):
|
14 |
+
expansion = 1
|
15 |
+
|
16 |
+
def __init__(self, in_planes, planes, stride=1):
|
17 |
+
super(BasicBlock, self).__init__()
|
18 |
+
self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
|
19 |
+
self.bn1 = nn.BatchNorm2d(planes)
|
20 |
+
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=1, padding=1, bias=False)
|
21 |
+
self.bn2 = nn.BatchNorm2d(planes)
|
22 |
+
|
23 |
+
self.shortcut = nn.Sequential()
|
24 |
+
if stride != 1 or in_planes != self.expansion*planes:
|
25 |
+
self.shortcut = nn.Sequential(
|
26 |
+
nn.Conv2d(in_planes, self.expansion*planes, kernel_size=1, stride=stride, bias=False),
|
27 |
+
nn.BatchNorm2d(self.expansion*planes)
|
28 |
+
)
|
29 |
+
|
30 |
+
def forward(self, x):
|
31 |
+
out = F.relu(self.bn1(self.conv1(x)))
|
32 |
+
out = self.bn2(self.conv2(out))
|
33 |
+
out += self.shortcut(x)
|
34 |
+
out = F.relu(out)
|
35 |
+
return out
|
36 |
+
|
37 |
+
|
38 |
+
class ResNet(nn.Module):
|
39 |
+
def __init__(self, block, num_blocks, num_classes=10):
|
40 |
+
super(ResNet, self).__init__()
|
41 |
+
self.in_planes = 64
|
42 |
+
|
43 |
+
self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False)
|
44 |
+
self.bn1 = nn.BatchNorm2d(64)
|
45 |
+
self.layer1 = self._make_layer(block, 64, num_blocks[0], stride=1)
|
46 |
+
self.layer2 = self._make_layer(block, 128, num_blocks[1], stride=2)
|
47 |
+
self.layer3 = self._make_layer(block, 256, num_blocks[2], stride=2)
|
48 |
+
self.layer4 = self._make_layer(block, 512, num_blocks[3], stride=2)
|
49 |
+
self.linear = nn.Linear(512*block.expansion, num_classes)
|
50 |
+
|
51 |
+
def _make_layer(self, block, planes, num_blocks, stride):
|
52 |
+
strides = [stride] + [1]*(num_blocks-1)
|
53 |
+
layers = []
|
54 |
+
for stride in strides:
|
55 |
+
layers.append(block(self.in_planes, planes, stride))
|
56 |
+
self.in_planes = planes * block.expansion
|
57 |
+
return nn.Sequential(*layers)
|
58 |
+
|
59 |
+
def forward(self, x):
|
60 |
+
out = F.relu(self.bn1(self.conv1(x)))
|
61 |
+
out = self.layer1(out)
|
62 |
+
out = self.layer2(out)
|
63 |
+
out = self.layer3(out)
|
64 |
+
out = self.layer4(out)
|
65 |
+
out = F.avg_pool2d(out, 4)
|
66 |
+
out = out.view(out.size(0), -1)
|
67 |
+
out = self.linear(out)
|
68 |
+
return out
|
69 |
+
|
70 |
+
|
71 |
+
def ResNet18():
|
72 |
+
return ResNet(BasicBlock, [2, 2, 2, 2])
|
73 |
+
|
74 |
+
|
75 |
+
def ResNet34():
|
76 |
+
return ResNet(BasicBlock, [3, 4, 6, 3])
|