Repository navigation
Expand file tree
/
Copy pathdice.py
More file actions
179 lines (153 loc) · 7.72 KB
/
Copy pathdice.py
File metadata and controls
179 lines (153 loc) · 7.72 KB
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
import math
import torch
from torch import nn
from cnn_utils import CBR, BR, Shuffle
import torch.nn.functional as F
class DICE(nn.Module):
"""
This class implements the volume-wise separable convolutions
"""
def __init__(self, in_channel, out_channel, height, width, ksize=3, dilation=[1, 1, 1], shuffle=True):
"""
:param in_channel: # of input channels
:param out_channel: # of output channels
:param height: Height of the input volume
:param width: Width of the input volume
:param ksize: Kernel size. We use the same kernel size of 3 for each dimension. Larger kernel size would increase the FLOPs and Parameters
:param dilation: It's a list with 3 elements, each element corresponding to a dilation rate for each dimension.
:param shuffle: Shuffle the feature maps in the volume-wise separable convolutions
"""
super(DICE, self).__init__()
assert len(dilation) == 3
assert in_channel >= 4, "Please Check Input Channel, It should >= 4."
padding_1 = int((ksize - 1) / 2) * dilation[0]
padding_2 = int((ksize - 1) / 2) * dilation[1]
padding_3 = int((ksize - 1) / 2) * dilation[2]
self.conv_channel = nn.Conv2d(in_channel, in_channel, kernel_size=ksize, stride=1, groups=in_channel,
padding=padding_1, bias=False, dilation=dilation[0])
self.conv_width = nn.Conv2d(width, width, kernel_size=ksize, stride=1, groups=width, padding=padding_2,
bias=False, dilation=dilation[1])
self.conv_height = nn.Conv2d(height, height, kernel_size=ksize, stride=1, groups=height, padding=padding_3,
bias=False, dilation=dilation[2])
self.br_act = BR(3 * in_channel)
self.weight_avg_layer = CBR(3 * in_channel, in_channel, ksize=1, stride=1, groups=in_channel)
# project from in_channel to out_channel
# groups_proj = math.gcd(in_channel, out_channel)
groups_proj = 2
self.proj_layer = CBR(in_channel, out_channel, ksize=3, stride=1, groups=groups_proj)
self.linear_comb_layer = nn.Sequential(
nn.AdaptiveAvgPool2d(output_size=1),
nn.Conv2d(in_channel, in_channel // 4, kernel_size=1, bias=False),
nn.ReLU(inplace=True),
nn.Conv2d(in_channel // 4, out_channel, kernel_size=1, bias=False),
nn.Sigmoid()
)
self.vol_shuffle = Shuffle(3)
self.width = width
self.height = height
self.channel_in = in_channel
self.channel_out = out_channel
self.shuffle = shuffle
self.ksize = ksize
self.dilation = dilation
def forward(self, x):
bsz, channels, height, width = x.size()
# process across channel. Input: C x H x W, Output: C x H x W
out_ch_wise = self.conv_channel(x)
# process across height. Input: H x C x W, Output: C x H x W
x_h_wise = x.clone()
if height != self.height:
if height < self.height:
x_h_wise = F.interpolate(x_h_wise, mode='bilinear', size=(self.height, width), align_corners=True)
else:
x_h_wise = F.adaptive_avg_pool2d(x_h_wise, output_size=(self.height, width))
x_h_wise = x_h_wise.transpose(1, 2).contiguous()
out_h_wise = self.conv_height(x_h_wise).transpose(1, 2).contiguous()
h_wise_height = out_h_wise.size(2)
if height != h_wise_height:
if h_wise_height < height:
out_h_wise = F.interpolate(out_h_wise, mode='bilinear', size=(height, width), align_corners=True)
else:
out_h_wise = F.adaptive_avg_pool2d(out_h_wise, output_size=(height, width))
# process across width: Input: W x H x C, Output: C x H x W
x_w_wise = x.clone()
if width != self.width:
if width < self.width:
x_w_wise = F.interpolate(x_w_wise, mode='bilinear', size=(height, self.width), align_corners=True)
else:
x_w_wise = F.adaptive_avg_pool2d(x_w_wise, output_size=(height, self.width))
x_w_wise = x_w_wise.transpose(1, 3).contiguous()
out_w_wise = self.conv_width(x_w_wise).transpose(1, 3).contiguous()
w_wise_width = out_w_wise.size(3)
if width != w_wise_width:
if w_wise_width < width:
out_w_wise = F.interpolate(out_w_wise, mode='bilinear', size=(height, width), align_corners=True)
else:
out_w_wise = F.adaptive_avg_pool2d(out_w_wise, output_size=(height, width))
# Merge. Output will be 3C x H X W
outputs = torch.cat((out_ch_wise, out_h_wise, out_w_wise), 1)
outputs = self.br_act(outputs)
if self.shuffle:
outputs = self.vol_shuffle(outputs)
outputs = self.weight_avg_layer(outputs)
linear_wts = self.linear_comb_layer(outputs)
proj_out = self.proj_layer(outputs)
return proj_out * linear_wts
def __repr__(self):
s = '{name}(in_channels={channel_in}, out_channels={channel_out}, kernel_size={ksize}, vol_shuffle={shuffle}, ' \
'width={width}, height={height}, dilation={dilation})'
return s.format(name=self.__class__.__name__, **self.__dict__)
class StridedDICE(nn.Module):
"""
This class implements the strided volume-wise seperable convolutions
"""
def __init__(self, in_channel, out_channel, height, width, ksize=3, dilation=[1, 1, 1], shuffle=True):
"""
:param in_channel: # of input channels
:param out_channel: # of output channels
:param height: Height of the input volume
:param width: Width of the input volume
:param ksize: Kernel size. We use the same kernel size of 3 for each dimension. Larger kernel size would increase the FLOPs and Parameters
:param dilation: It's a list with 3 elements, each element corresponding to a dilation rate for each dimension.
:param shuffle: Shuffle the feature maps in the volume-wise separable convolutions
"""
super(StridedDICE, self).__init__()
assert len(dilation) == 3
self.left_layer = nn.Sequential(
CBR(in_channel, in_channel, ksize=3, stride=2, groups=in_channel),
CBR(in_channel, in_channel, ksize=1, stride=1)
)
self.right_layer = nn.Sequential(
nn.AvgPool2d(kernel_size=3, padding=1, stride=1),
DICE(in_channel, in_channel, height, width, ksize=ksize, dilation=dilation, shuffle=shuffle),
CBR(in_channel, in_channel, ksize=1, stride=1)
)
self.shuffle = Shuffle(groups=2)
self.width = width
self.height = height
self.channel_in = channel_in
self.channel_out = 2 * channel_in
self.ksize = ksize
def forward(self, x):
x_left = self.left_layer(x)
x_right = self.right_layer(x)
concat = torch.cat([x_left, x_right], 1)
return self.shuffle(concat)
def __repr__(self):
s = '{name}(in_channels={channel_in}, out_channels={channel_out}, kernel_size={ksize}, ' \
'width={width}, height={height})'
return s.format(name=self.__class__.__name__, **self.__dict__)
if __name__ == '__main__':
import numpy as np
channel_in = 4
channel_out = 30
width = 112
height = 112
bsz = 2
input = torch.Tensor(bsz, channel_in, height, width).fill_(1)
model = DICE(channel_in, channel_out, width, height, shuffle=True)
model.eval()
# input = torch.Tensor(bsz, channel_in, 56, 56).fill_(1)
out = model(input)
n_params = sum([np.prod(p.size()) for p in model.parameters()])
print('Params: {}'.format(n_params))