-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_st.py
204 lines (171 loc) · 6.56 KB
/
model_st.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import torch.nn as nn
import torch.nn.functional as F
class MosquitoNet(nn.Module):
''' Original MosquitoNet Model '''
def __init__(self):
super(MosquitoNet, self).__init__()
self.layer1 = nn.Sequential(
nn.Conv2d(3, 16, kernel_size=5, stride=1, padding=2),
nn.BatchNorm2d(16),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2)
)
self.layer2 = nn.Sequential(
nn.Conv2d(16, 32, kernel_size=5, stride=1, padding=2),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2)
)
self.layer3 = nn.Sequential(
nn.Conv2d(32, 64, kernel_size=3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2)
)
self.fc1 = nn.Linear(64*15*15, 512)
self.fc2 = nn.Linear(512, 128)
self.fc3 = nn.Linear(128, 2)
self.drop = nn.Dropout2d(0.2)
def forward(self, x):
out = self.layer1(x)
out = self.layer2(out)
out = self.layer3(out)
out = out.view(out.size(0), -1) # flatten out a input for Dense Layer
out = self.fc1(out)
out = F.relu(out)
out = self.drop(out)
out = self.fc2(out)
out = F.relu(out)
out = self.drop(out)
out = self.fc3(out)
return out
class Mish(nn.Module):
'''
mish(x) = x * tanh(softplus(x)) = x * tanh(ln(1 + exp(x)))
Shape:
- Input: (N, *) where * means, any number of additional
dimensions
- Output: (N, *), same shape as the input
'''
@torch.jit.script
def mish(input):
return input * torch.tanh(F.softplus(input))
def __init__(self):
super().__init__()
def forward(self, input):
return self.mish(input)
class MosquitoNet_Mish(nn.Module):
''' Another version of MosquitoNet using Mish activation function, outperforms Original Version'''
def __init__(self):
super(MosquitoNet, self).__init__()
self.layer1 = nn.Sequential(
nn.Conv2d(3, 16, kernel_size=5, stride=1, padding=2),
nn.BatchNorm2d(16),
Mish(),
nn.MaxPool2d(kernel_size=2, stride=2)
)
self.layer2 = nn.Sequential(
nn.Conv2d(16, 32, kernel_size=5, stride=1, padding=2),
nn.BatchNorm2d(32),
Mish(),
nn.MaxPool2d(kernel_size=2, stride=2)
)
self.layer3 = nn.Sequential(
nn.Conv2d(32, 64, kernel_size=3, padding=1),
nn.BatchNorm2d(64),
Mish(),
nn.MaxPool2d(kernel_size=2, stride=2)
)
self.fc1 = nn.Linear(64*15*15, 2048)
self.fc2 = nn.Linear(2048, 1024)
self.fc3 = nn.Linear(1024, 2)
self.drop = nn.Dropout2d(0.2)
self.m=Mish()
def forward(self, x):
out = self.layer1(x)
out = self.layer2(out)
out = self.layer3(out)
out = out.view(out.size(0), -1) # flatten out a input for Dense Layer
out = self.fc1(out)
out = self.m(out)
out = self.drop(out)
out = self.fc2(out)
out = self.m(out)
out = self.drop(out)
out = self.fc3(out)
return out
class DepthwiseSepConv(nn.Module):
def __init__(self, inch, ch, kernel_size=3, stride=1, dilation=1, bias=True, BatchNorm=True):
'''
DepthwiseSepConv Module implemented in PyTorch. Divided in 2 parts:-
1) Depth Wise Convolution : using nn.Conv2d layer with groups parameter each kernel is passed for a single layer of input
2) Point Wise Convolution : 1x1 Convolution implemented using nn.Conv2d layer
BatchNorm (optional) available, requires to pass the function/module to be used.
'''
super(DepthwiseSepConv, self).__init__()
self.depthconv = nn.Conv2d(
inch, inch, kernel_size, stride, 0, dilation, groups=inch, bias=bias)
self.btn = nn.BatchNorm2d(inch)
self.pointconv = nn.Conv2d(inch, ch, 1, 1, 0, 1, 1, bias=bias)
self.dilation = dilation
self.kernel_size = kernel_size
self.BatchNorm = BatchNorm
def padding_fix(self, x, kernel_size, dilation):
kernel_size_effective = kernel_size + \
(kernel_size - 1) * (dilation - 1)
pad_total = kernel_size_effective - 1
pad_beg = pad_total // 2
pad_end = pad_total - pad_beg
padded_inputs = F.pad(x, (pad_beg, pad_end, pad_beg, pad_end))
return padded_inputs
def forward(self, x):
x = self.padding_fix(x, self.kernel_size, self.dilation)
x = self.depthconv(x)
if not self.BatchNorm:
x = self.btn(x)
x = self.pointconv(x)
return x
class MosquitoNet_V2(nn.Module):
''' Another version of MosquitoNet using Mish activation function, outperforms Original Version'''
def __init__(self):
super(MosquitoNet_V2, self).__init__()
self.layer1 = nn.Sequential(
DepthwiseSepConv(3, 16, kernel_size=5, stride=1),
Mish(),
nn.MaxPool2d(kernel_size=2, stride=2)
)
self.layer2 = nn.Sequential(
DepthwiseSepConv(16, 32, kernel_size=3, stride=1),
Mish(),
nn.MaxPool2d(kernel_size=2, stride=2)
)
self.layer3 = nn.Sequential(
DepthwiseSepConv(32, 64, kernel_size=3, stride=1),
Mish(),
nn.MaxPool2d(kernel_size=2, stride=2)
)
self.layer4 = nn.Sequential(
DepthwiseSepConv(64, 128, kernel_size=3, stride=1),
Mish(),
nn.MaxPool2d(kernel_size=2, stride=2)
)
self.fc1 = nn.Linear(128*7*7, 4096)
self.fc2 = nn.Linear(4096, 2048)
self.fc3 = nn.Linear(2048, 2)
self.drop = nn.Dropout2d(0.2)
self.m = Mish()
def forward(self, x):
out = self.layer1(x)
out = self.layer2(out)
out = self.layer3(out)
out = self.layer4(out)
# flatten out a input for Dense Layer
out = out.view(out.size(0), -1)
out = self.fc1(out)
out = self.m(out)
out = self.drop(out)
out = self.fc2(out)
out = self.m(out)
out = self.drop(out)
out = self.fc3(out)
return out