-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresnet50.py
111 lines (93 loc) · 3.79 KB
/
resnet50.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
import torch
import torch.nn as nn
class ResidualBlock(nn.Module):
def __init__(self, in_channels, mid_channels, downsample=None, stride=1):
super().__init__()
self.expansion = 4
self.conv1 = nn.Conv2d(in_channels=in_channels,
out_channels=mid_channels,
kernel_size=(1, 1),
stride=1,
padding=0,
bias=False)
self.bn1 = nn.BatchNorm2d(mid_channels)
self.conv2 = nn.Conv2d(in_channels=mid_channels,
out_channels=mid_channels,
kernel_size=(3, 3),
stride=stride,
padding=1,
bias=False)
self.bn2 = nn.BatchNorm2d(mid_channels)
self.conv3 = nn.Conv2d(in_channels=mid_channels,
out_channels=mid_channels*self.expansion,
kernel_size=(1, 1),
stride=1,
padding=0,
bias=False)
self.bn3 = nn.BatchNorm2d(mid_channels*self.expansion)
self.relu = nn.ReLU()
self.stride = stride
self.downsample = downsample
def forward(self, x):
residual = x.clone()
x = self.relu(self.bn1(self.conv1(x)))
x = self.relu(self.bn2(self.conv2(x)))
x = self.bn3(self.conv3(x))
if self.downsample is not None:
residual = self.downsample(residual)
x += residual
x = self.relu(x)
return x
class ResNet(nn.Module):
def __init__(self, block, layers, input_channels, num_classes):
super(ResNet, self).__init__()
self.in_channels = 64
self.conv = nn.Conv2d(in_channels=input_channels,
out_channels=64,
kernel_size=(7, 7),
stride=2,
padding=3,
bias=False)
self.bn = nn.BatchNorm2d(64)
self.relu = nn.ReLU()
self.maxpool = nn.MaxPool2d(kernel_size=(3, 3), stride=2, padding=1)
self.layer1 = self._make_layer(block, layers[0], mid_channels=64, stride=1)
self.layer2 = self._make_layer(block, layers[1], mid_channels=128, stride=2)
self.layer3 = self._make_layer(block, layers[2], mid_channels=256, stride=2)
self.layer4 = self._make_layer(block, layers[3], mid_channels=512, stride=2)
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.fc = nn.Linear(512*4, num_classes)
def forward(self, x):
x = self.maxpool(self.relu(self.bn(self.conv(x))))
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.avgpool(x)
x = torch.flatten(x, 1)
x = self.fc(x)
return x
def _make_layer(self, block, blocks_num, mid_channels, stride):
downsample = None
layers = []
if stride != 1 or self.in_channels != mid_channels*4:
downsample = nn.Sequential(
nn.Conv2d(in_channels=self.in_channels,
out_channels=mid_channels*4,
kernel_size=(1, 1),
stride=stride,
bias=False),
nn.BatchNorm2d(mid_channels*4)
)
layers.append(block(in_channels=self.in_channels,
mid_channels=mid_channels,
downsample=downsample,
stride=stride))
self.in_channels = mid_channels*4
for i in range(blocks_num-1):
layers.append(block(in_channels=self.in_channels, mid_channels=mid_channels))
return nn.Sequential(*layers)
x = torch.rand(3, 3, 224, 224)
model = ResNet(ResidualBlock, [3, 4, 6, 3], 3, 10)
y = model(x)
y