-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathcnn_utils.py
169 lines (143 loc) · 4.87 KB
/
cnn_utils.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
import torch.nn as nn
#============================================
__author__ = "Sachin Mehta"
__license__ = "MIT"
__maintainer__ = "Sachin Mehta"
#============================================
class CBR(nn.Module):
'''
This class defines the convolution layer with batch normalization and PReLU activation
'''
def __init__(self, nIn, nOut, kSize, stride=1, groups=1):
'''
:param nIn: number of input channels
:param nOut: number of output channels
:param kSize: kernel size
:param stride: stride rate for down-sampling. Default is 1
'''
super().__init__()
padding = int((kSize - 1) / 2)
self.conv = nn.Conv2d(nIn, nOut, kSize, stride=stride, padding=padding, bias=False, groups=groups)
self.bn = nn.BatchNorm2d(nOut)
self.act = nn.PReLU(nOut)
def forward(self, input):
'''
:param input: input feature map
:return: transformed feature map
'''
output = self.conv(input)
# output = self.conv1(output)
output = self.bn(output)
output = self.act(output)
return output
class BR(nn.Module):
'''
This class groups the batch normalization and PReLU activation
'''
def __init__(self, nOut):
'''
:param nOut: output feature maps
'''
super().__init__()
self.bn = nn.BatchNorm2d(nOut)
self.act = nn.PReLU(nOut)
def forward(self, input):
'''
:param input: input feature map
:return: normalized and thresholded feature map
'''
output = self.bn(input)
output = self.act(output)
return output
class CB(nn.Module):
'''
This class groups the convolution and batch normalization
'''
def __init__(self, nIn, nOut, kSize, stride=1, groups=1):
'''
:param nIn: number of input channels
:param nOut: number of output channels
:param kSize: kernel size
:param stride: optinal stide for down-sampling
'''
super().__init__()
padding = int((kSize - 1) / 2)
self.conv = nn.Conv2d(nIn, nOut, kSize, stride=stride, padding=padding, bias=False,
groups=groups)
self.bn = nn.BatchNorm2d(nOut)
def forward(self, input):
'''
:param input: input feature map
:return: transformed feature map
'''
output = self.conv(input)
output = self.bn(output)
return output
class C(nn.Module):
'''
This class is for a convolutional layer.
'''
def __init__(self, nIn, nOut, kSize, stride=1, groups=1):
'''
:param nIn: number of input channels
:param nOut: number of output channels
:param kSize: kernel size
:param stride: optional stride rate for down-sampling
'''
super().__init__()
padding = int((kSize - 1) / 2)
self.conv = nn.Conv2d(nIn, nOut, kSize, stride=stride, padding=padding, bias=False,
groups=groups)
def forward(self, input):
'''
:param input: input feature map
:return: transformed feature map
'''
output = self.conv(input)
return output
class CDilated(nn.Module):
'''
This class defines the dilated convolution.
'''
def __init__(self, nIn, nOut, kSize, stride=1, d=1, groups=1):
'''
:param nIn: number of input channels
:param nOut: number of output channels
:param kSize: kernel size
:param stride: optional stride rate for down-sampling
:param d: optional dilation rate
'''
super().__init__()
padding = int((kSize - 1) / 2) * d
self.conv = nn.Conv2d(nIn, nOut,kSize, stride=stride, padding=padding, bias=False,
dilation=d, groups=groups)
def forward(self, input):
'''
:param input: input feature map
:return: transformed feature map
'''
output = self.conv(input)
return output
class CDilatedB(nn.Module):
'''
This class defines the dilated convolution with batch normalization.
'''
def __init__(self, nIn, nOut, kSize, stride=1, d=1, groups=1):
'''
:param nIn: number of input channels
:param nOut: number of output channels
:param kSize: kernel size
:param stride: optional stride rate for down-sampling
:param d: optional dilation rate
'''
super().__init__()
padding = int((kSize - 1) / 2) * d
self.conv = nn.Conv2d(nIn, nOut,kSize, stride=stride, padding=padding, bias=False,
dilation=d, groups=groups)
self.bn = nn.BatchNorm2d(nOut)
def forward(self, input):
'''
:param input: input feature map
:return: transformed feature map
'''
return self.bn(self.conv(input))