-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresnet.py
120 lines (99 loc) · 3.7 KB
/
resnet.py
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
'''ResNet18/34/50/101/152 in Pytorch.'''
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
def conv3x3(in_planes, out_planes, stride=1):
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False)
class BasicBlock(nn.Module):
expansion = 1
def __init__(self, in_planes, planes, stride=1, shortcut=None):
super(BasicBlock, self).__init__()
self.layers = nn.Sequential(
conv3x3(in_planes, planes, stride),
nn.BatchNorm2d(planes),
nn.ReLU(True),
conv3x3(planes, planes),
nn.BatchNorm2d(planes),
)
self.shortcut = shortcut
def forward(self, x):
residual = x
y = self.layers(x)
if self.shortcut:
residual = self.shortcut(x)
y += residual
y = F.relu(y)
return y
class Bottleneck(nn.Module):
expansion = 4
def __init__(self, in_planes, planes, stride=1, shortcut=None):
super(Bottleneck, self).__init__()
self.layers = nn.Sequential(
nn.Conv2d(in_planes, planes, kernel_size=1, bias=False),
nn.BatchNorm2d(planes),
nn.ReLU(True),
nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False),
nn.BatchNorm2d(planes),
nn.ReLU(True),
nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False),
nn.BatchNorm2d(planes * 4),
)
self.shortcut = shortcut
def forward(self, x):
residual = x
y = self.layers(x)
if self.shortcut:
residual = self.shortcut(x)
y += residual
y = F.relu(y)
return y
class ResNet(nn.Module):
def __init__(self, block, nblocks, num_classes=10):
super(ResNet, self).__init__()
self.in_planes = 64
self.pre_layers = nn.Sequential(
conv3x3(1,64), # Change input channels to 1 for grayscale MNIST images
nn.BatchNorm2d(64),
nn.ReLU(True),
)
self.layer1 = self._make_layer(block, 64, nblocks[0])
self.layer2 = self._make_layer(block, 128, nblocks[1], stride=2)
self.layer3 = self._make_layer(block, 256, nblocks[2], stride=2)
self.layer4 = self._make_layer(block, 512, nblocks[3], stride=2)
self.avgpool = nn.AdaptiveAvgPool2d((1,1)) # Update to AdaptiveAvgPool2d for varying input sizes
self.fc = nn.Linear(512*block.expansion, num_classes)
def _make_layer(self, block, planes, nblocks, stride=1):
shortcut = None
if stride != 1 or self.in_planes != planes * block.expansion:
shortcut = nn.Sequential(
nn.Conv2d(self.in_planes, planes * block.expansion,
kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(planes * block.expansion),
)
layers = []
layers.append(block(self.in_planes, planes, stride, shortcut))
self.in_planes = planes * block.expansion
for i in range(1, nblocks):
layers.append(block(self.in_planes, planes))
return nn.Sequential(*layers)
def forward(self, x):
x = self.pre_layers(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.avgpool(x)
x = x.view(x.size(0), -1)
x = self.fc(x)
return x
def resnet18():
return ResNet(BasicBlock, [2,2,2,2])
def resnet34():
return ResNet(BasicBlock, [3,4,6,3])
def resnet50():
return ResNet(Bottleneck, [3,4,6,3])
def resnet101():
return ResNet(Bottleneck, [3,4,23,3])
def resnet152():
return ResNet(Bottleneck, [3,8,36,3])