This repository has been archived by the owner on Oct 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmodel.py
117 lines (97 loc) · 4.12 KB
/
model.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
import torch.nn as nn
import torch.nn.functional as F
class ResNet(nn.Module):
def __init__(self, n=7, res_option='A', use_dropout=False):
super(ResNet, self).__init__()
self.res_option = res_option
self.use_dropout = use_dropout
self.conv1 = nn.Conv2d(3, 16, kernel_size=3, stride=1, padding=1)
self.norm1 = nn.BatchNorm2d(16)
self.relu1 = nn.ReLU(inplace=True)
self.layers1 = self._make_layer(n, 16, 16, 1)
self.layers2 = self._make_layer(n, 32, 16, 2)
self.layers3 = self._make_layer(n, 64, 32, 2)
self.avgpool = nn.AvgPool2d(8)
self.linear = nn.Linear(64, 10)
def _make_layer(self, layer_count, channels, channels_in, stride):
return nn.Sequential(
ResBlock(channels, channels_in, stride, res_option=self.res_option, use_dropout=self.use_dropout),
*[ResBlock(channels) for _ in range(layer_count-1)])
def forward(self, x):
out = self.conv1(x)
out = self.norm1(out)
out = self.relu1(out)
out = self.layers1(out)
out = self.layers2(out)
out = self.layers3(out)
out = self.avgpool(out)
out = out.view(out.size(0), -1)
out = self.linear(out)
return out
class ResBlock(nn.Module):
def __init__(self, num_filters, channels_in=None, stride=1, res_option='A', use_dropout=False):
super(ResBlock, self).__init__()
# uses 1x1 convolutions for downsampling
if not channels_in or channels_in == num_filters:
channels_in = num_filters
self.projection = None
else:
if res_option == 'A':
self.projection = IdentityPadding(num_filters, channels_in, stride)
elif res_option == 'B':
self.projection = ConvProjection(num_filters, channels_in, stride)
elif res_option == 'C':
self.projection = AvgPoolPadding(num_filters, channels_in, stride)
self.use_dropout = use_dropout
self.conv1 = nn.Conv2d(channels_in, num_filters, kernel_size=3, stride=stride, padding=1)
self.bn1 = nn.BatchNorm2d(num_filters)
self.relu1 = nn.ReLU(inplace=True)
self.conv2 = nn.Conv2d(num_filters, num_filters, kernel_size=3, stride=1, padding=1)
self.bn2 = nn.BatchNorm2d(num_filters)
if self.use_dropout:
self.dropout = nn.Dropout(inplace=True)
self.relu2 = nn.ReLU(inplace=True)
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu1(out)
out = self.conv2(out)
out = self.bn2(out)
if self.use_dropout:
out = self.dropout(out)
if self.projection:
residual = self.projection(x)
out += residual
out = self.relu2(out)
return out
# various projection options to change number of filters in residual connection
# option A from paper
class IdentityPadding(nn.Module):
def __init__(self, num_filters, channels_in, stride):
super(IdentityPadding, self).__init__()
# with kernel_size=1, max pooling is equivalent to identity mapping with stride
self.identity = nn.MaxPool2d(1, stride=stride)
self.num_zeros = num_filters - channels_in
def forward(self, x):
out = F.pad(x, (0, 0, 0, 0, 0, self.num_zeros))
out = self.identity(out)
return out
# option B from paper
class ConvProjection(nn.Module):
def __init__(self, num_filters, channels_in, stride):
super(ResA, self).__init__()
self.conv = nn.Conv2d(channels_in, num_filters, kernel_size=1, stride=stride)
def forward(self, x):
out = self.conv(x)
return out
# experimental option C
class AvgPoolPadding(nn.Module):
def __init__(self, num_filters, channels_in, stride):
super(AvgPoolPadding, self).__init__()
self.identity = nn.AvgPool2d(stride, stride=stride)
self.num_zeros = num_filters - channels_in
def forward(self, x):
out = F.pad(x, (0, 0, 0, 0, 0, self.num_zeros))
out = self.identity(out)
return out