-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
190 lines (172 loc) · 6.86 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import torch
from torch import nn
class MyNet(nn.Module):
def __init__(self, num_classes=17):
super(MyNet, self).__init__()
self.features = nn.Sequential(
nn.BatchNorm2d(3),
nn.Conv2d(3, 32, kernel_size=5, stride=1, padding=2),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
nn.Conv2d(32, 32, kernel_size=5, stride=1, padding=2),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2),
nn.Dropout(0.25),
nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2),
nn.Dropout(0.25),
nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True),
nn.Conv2d(128, 128, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2),
nn.Dropout(0.5),
nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2),
nn.Dropout(0.5)
)
self.classifier = nn.Sequential(
nn.Linear(4*4*256, 1024),
nn.BatchNorm1d(1024),
nn.ReLU(inplace=True),
nn.BatchNorm1d(1024),
nn.Dropout(0.5),
nn.Linear(1024, num_classes),
nn.Sigmoid()
)
def forward(self, x):
x = self.features(x)
x = x.view(x.size(0), x.size(1)*x.size(2)*x.size(3))
x = self.classifier(x)
return x
class MyNetfirst4(nn.Module):
def __init__(self, num_classes=17):
super(MyNetfirst4, self).__init__()
self.features = nn.Sequential(
nn.BatchNorm2d(3),
nn.Conv2d(3, 32, kernel_size=5, stride=1, padding=2),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
nn.Conv2d(32, 32, kernel_size=5, stride=1, padding=2),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2),
nn.Dropout(0.25),
nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2),
nn.Dropout(0.25),
nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True),
nn.Conv2d(128, 128, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2),
nn.Dropout(0.5),
nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2),
nn.Dropout(0.5)
)
self.classifier = nn.Sequential(
nn.Linear(4*4*256, 1024),
nn.BatchNorm1d(1024),
nn.ReLU(inplace=True),
nn.BatchNorm1d(1024),
nn.Dropout(0.5),
nn.Linear(1024, num_classes),
nn.Softmax()
)
def forward(self, x):
x = self.features(x)
x = x.view(x.size(0), x.size(1)*x.size(2)*x.size(3))
x = self.classifier(x)
return x
class BasicBlock(nn.Module):
def __init__(self, in_channel, out_channel, kernel_size=5, dropout=0.25):
padding = int(kernel_size / 2)
super(BasicBlock, self).__init__()
self.conv = nn.Sequential(
nn.BatchNorm2d(in_channel),
nn.Conv2d(in_channel, out_channel, kernel_size=kernel_size, stride=1, padding=padding),
nn.BatchNorm2d(out_channel),
nn.ReLU(inplace=True),
nn.Conv2d(out_channel, out_channel, kernel_size=kernel_size, stride=1, padding=padding),
nn.BatchNorm2d(out_channel),
nn.ReLU(inplace=True),
# nn.MaxPool2d(kernel_size=2),
nn.Conv2d(out_channel, out_channel, kernel_size=kernel_size, stride=2, padding=padding, bias=False),
nn.Dropout(dropout),
)
self.downsample = nn.Conv2d(in_channel, out_channel, kernel_size=kernel_size, stride=2, padding=padding, bias=False)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
out = self.conv(x)
out += self.downsample(x)
out = self.relu(out)
return out
class ProposedBlock(nn.Module):
def __init__(self, in_channel, out_channel, stride=1):
super(ProposedBlock, self).__init__()
self.conv = nn.Sequential(
nn.BatchNorm2d(in_channel),
nn.ReLU(inplace=True),
nn.Conv2d(in_channel, out_channel, kernel_size=3, stride=stride, padding=1, bias=False),
nn.BatchNorm2d(out_channel),
nn.ReLU(inplace=True),
nn.Conv2d(out_channel, out_channel, kernel_size=3, stride=1, padding=1, bias=False),
)
self.downsample = nn.Conv2d(in_channel, out_channel, kernel_size=3, stride=2,bias=False)
self.relu = nn.ReLU(inplace=True)
self.stride = stride
def forward(self, x):
out = self.conv(x)
if self.stride > 1:
out += self.downsample(x)
else:
out += x
return out
class ResNet(nn.Module):
def __init__(self, num_classes=17, block=BasicBlock):
super(ResNet, self).__init__()
self.conv = nn.Sequential(
block(3, 32, kernel_size=5, dropout=0.25),
block(32, 64, kernel_size=3, dropout=0.25),
block(64, 128, kernel_size=3, dropout=0.5),
block(128, 256, kernel_size=3, dropout=0.5),
)
self.classifier = nn.Sequential(
nn.Linear(4096, 1024),
nn.BatchNorm1d(1024),
nn.ReLU(inplace=True),
nn.Dropout(0.5),
nn.Linear(1024, num_classes),
nn.Sigmoid()
)
def forward(self, x):
x = self.conv(x)
x = x.view(x.size(0), -1)
x = self.classifier(x)
return x