-
Notifications
You must be signed in to change notification settings - Fork 0
/
inception.py
149 lines (110 loc) · 5 KB
/
inception.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"""
An implementation of GoogLeNet / InceptionNet from scratch.
Programmed by Aladdin Persson <aladdin.persson at hotmail dot com>
* 2020-04-07 Initial coding
* 2022-12-20 Update comments, code revision, checked still works with latest PyTorch version
"""
import torch
from torch import nn
class Inception(nn.Module):
def __init__(self, in_channels=3, use_auxiliary=True, num_classes=1000):
super(Inception, self).__init__()
self.conv1 = ConvBlock(in_channels, 64, kernel_size=7, stride=2, padding=3)
self.conv2 = ConvBlock(64, 192, kernel_size=3, stride=1, padding=1)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.avgpool = nn.AvgPool2d(kernel_size=7, stride=1)
self.dropout = nn.Dropout(0.4)
self.linear = nn.Linear(1024, num_classes)
self.use_auxiliary = use_auxiliary
if use_auxiliary:
self.auxiliary4a = Auxiliary(512, num_classes)
self.auxiliary4d = Auxiliary(528, num_classes)
self.inception3a = InceptionBlock(192, 64, 96, 128, 16, 32, 32)
self.inception3b = InceptionBlock(256, 128, 128, 192, 32, 96, 64)
self.inception4a = InceptionBlock(480, 192, 96, 208, 16, 48, 64)
self.inception4b = InceptionBlock(512, 160, 112, 224, 24, 64, 64)
self.inception4c = InceptionBlock(512, 128, 128, 256, 24, 64, 64)
self.inception4d = InceptionBlock(512, 112, 144, 288, 32, 64, 64)
self.inception4e = InceptionBlock(528, 256, 160, 320, 32, 128, 128)
self.inception5a = InceptionBlock(832, 256, 160, 320, 32, 128, 128)
self.inception5b = InceptionBlock(832, 384, 192, 384, 48, 128, 128)
def forward(self, x):
y = None
z = None
x = self.conv1(x)
x = self.maxpool(x)
x = self.conv2(x)
x = self.maxpool(x)
x = self.inception3a(x)
x = self.inception3b(x)
x = self.maxpool(x)
x = self.inception4a(x)
if self.training and self.use_auxiliary:
y = self.auxiliary4a(x)
x = self.inception4b(x)
x = self.inception4c(x)
x = self.inception4d(x)
if self.training and self.use_auxiliary:
z = self.auxiliary4d(x)
x = self.inception4e(x)
x = self.maxpool(x)
x = self.inception5a(x)
x = self.inception5b(x)
x = self.avgpool(x)
x = x.reshape(x.shape[0], -1)
x = self.dropout(x)
x = self.linear(x)
return x, y, z
class ConvBlock(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, **kwargs):
super(ConvBlock, self).__init__()
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, **kwargs)
self.bn = nn.BatchNorm2d(out_channels)
self.relu = nn.ReLU()
def forward(self, x):
return self.relu(self.bn(self.conv(x)))
class InceptionBlock(nn.Module):
def __init__(self, im_channels, num_1x1, num_3x3_red, num_3x3, num_5x5_red, num_5x5, num_pool_proj):
super(InceptionBlock, self).__init__()
self.one_by_one = ConvBlock(im_channels, num_1x1, kernel_size=1)
self.tree_by_three_red = ConvBlock(im_channels, num_3x3_red, kernel_size=1)
self.tree_by_three = ConvBlock(num_3x3_red, num_3x3, kernel_size=3, padding=1)
self.five_by_five_red = ConvBlock(im_channels, num_5x5_red, kernel_size=1)
self.five_by_five = ConvBlock(num_5x5_red, num_5x5, kernel_size=5, padding=2)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=1, padding=1)
self.pool_proj = ConvBlock(im_channels, num_pool_proj, kernel_size=1)
def forward(self, x):
x1 = self.one_by_one(x)
x2 = self.tree_by_three_red(x)
x2 = self.tree_by_three(x2)
x3 = self.five_by_five_red(x)
x3 = self.five_by_five(x3)
x4 = self.maxpool(x)
x4 = self.pool_proj(x4)
x = torch.cat([x1, x2, x3, x4], 1)
return x
class Auxiliary(nn.Module):
def __init__(self, in_channels, num_classes):
super(Auxiliary, self).__init__()
self.avgpool = nn.AvgPool2d(kernel_size=5, stride=3)
self.conv1x1 = ConvBlock(in_channels, 128, kernel_size=1)
self.fc1 = nn.Linear(2048, 1024)
self.fc2 = nn.Linear(1024, num_classes)
self.dropout = nn.Dropout(0.7)
self.relu = nn.ReLU()
def forward(self, x):
x = self.avgpool(x)
x = self.conv1x1(x)
x = x.reshape(x.shape[0], -1)
x = self.relu(self.fc1(x))
x = self.dropout(x)
x = self.fc2(x)
return x
# if __name__ == "__main__":
# BATCH_SIZE = 32
# x = torch.randn(BATCH_SIZE, 3, 224, 224)
# model = Inception()
# print(model)
# # print(model(x)[2].shape)
# # assert model(x)[2].shape == torch.Size([BATCH_SIZE, 6])
# # print(next(iter(model(x))))