-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResNet.py
More file actions
96 lines (76 loc) · 3.76 KB
/
ResNet.py
File metadata and controls
96 lines (76 loc) · 3.76 KB
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
import torch
import torch.nn as nn
class block(nn.Module):
def __init__(self, in_channels, out_channels, identity_downsample = None, stride= 1):
super(block, self).__init__()
self.expansion = 4 # in ResNet bottlenecks, the final layer of the block always has 4x the number of channels of the first two layers
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, padding=0)
self.bn1 = nn.BatchNorm2d(out_channels)
self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=stride, padding=1)
self.bn2 = nn.BatchNorm2d(out_channels)
self.conv3 = nn.Conv2d(out_channels, out_channels* self.expansion, kernel_size=1, stride=1, padding=0)
self.bn3 = nn.BatchNorm2d(out_channels* self.expansion)
self.relu = nn.ReLU()
self.identity_downsample = identity_downsample
def forward(self, x):
identity = x
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.conv2(x)
x = self.bn2(x)
x = self.relu(x)
x = self.conv3(x)
x = self.bn3(x)
# (1x1 -> 3x3 -> 1x1)
if self.identity_downsample is not None:
identity = self.identity_downsample(identity)
x += identity # residual connection
x = self.relu(x)
return x
class ResNet(nn.Module):
def __init__(self, block, layers, image_channel, num_classes):
super(ResNet, self).__init__()
self.in_channel = 64
self.conv1 = nn.Conv2d(image_channel, 64, kernel_size= 7, stride= 2, padding= 3)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU()
self.maxpool = nn.MaxPool2d(kernel_size= 3, stride= 2, padding= 1)
# ResNet layers
self.layer1 = self.make_layer(block, layers[0], out_channels= 64, stride= 1)
self.layer2 = self.make_layer(block, layers[1], out_channels= 128, stride= 2)
self.layer3 = self.make_layer(block, layers[2], out_channels= 256, stride= 2)
self.layer4 = self.make_layer(block, layers[3], out_channels= 512, stride= 2)
# standard cnn pattern when stride = 2, half the img size and double the channels
self.avgpool = nn.AdaptiveAvgPool2d((1,1))
self.fc = nn.Linear(512* 4, num_classes)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.avgpool(x)
x = x.reshape(x.shape[0], -1)
x = self.fc(x)
return x
def make_layer(self, block, num_residual, out_channels, stride):
identity_downsample = None
layers = []
if stride != 1 or self.in_channel != out_channels* 4:
identity_downsample = nn.Sequential(nn.Conv2d(self.in_channel, out_channels* 4, kernel_size=1, stride=stride),
nn.BatchNorm2d(out_channels* 4))
layers.append(block(self.in_channel, out_channels, identity_downsample, stride))
self.in_channel = out_channels* 4
for i in range(num_residual - 1):
layers.append(block(self.in_channel, out_channels)) # 256 -> 64, and then 64*4 so (256) again
return nn.Sequential(*layers)
def ResNet50(img_channels = 3, num_classes = 1000):
return ResNet(block, [3, 4, 6, 3], img_channels, num_classes)
def ResNet101(img_channels = 3, num_classes = 1000):
return ResNet(block, [3, 4, 23, 3], img_channels, num_classes)
def ResNet152(img_channels = 3, num_classes = 1000):
return ResNet(block, [3, 8, 36, 3], img_channels, num_classes)